2

This program gives me the weight of the minimum spanning tree and the longest distance from starting node..But after inputing the number of testcases and the vertice number and edge number,it take two edges and their weights and it gives some junk value.why?

#include<iostream>
#include<boost/config.hpp>
#include<boost/graph/adjacency_list.hpp>
#include<utility>
#include<boost/graph/prim_minimum_spanning_tree.hpp>
#include<vector>


using namespace std;
using namespace boost;

int main()
{
  typedef adjacency_list < vecS, vecS, undirectedS,property < vertex_distance_t, int>, property < edge_weight_t, int > > Graph;
  int no_test=0,v,e,m,a,b,c,w,d;
  cin>>no_test;
  int array_weights[100],array_distances[100],i,j;
  m=0;
  while(m!=no_test)
  {
    w=0;
    d=0;
    cin>>v>>e;//take input

    Graph g(v);//create graph g

    property_map < Graph,edge_weight_t > ::type weightMap;
    bool b;
    typedef graph_traits < Graph> ::edge_descriptor edge11;

    for(i=0;i<e;i++)  //add edges into g from i/p
    {
      edge11 ed;
      cin>>a>>b>>c;
      tie(ed, b)=add_edge(a, b, g);
      weightMap[ed]=c;
    }
    typedef graph_traits < Graph> ::vertex_descriptor vertex11;
    property_map<Graph,vertex_distance_t>::type distanceMap=get(vertex_distance,g);
    property_map<Graph,vertex_index_t>::type indexMap=get(vertex_index,g);
    vector < vertex11 > pred(v);
    prim_minimum_spanning_tree(g,*vertices(g).first,&pred[0],distanceMap,weightMap,indexMap,default_dijkstra_visitor());
    typedef graph_traits<Graph>::edge_iterator edge1;
    typedef graph_traits<Graph>::vertex_iterator vertex1;
    pair <edge1, edge1> edg;

    for(edg=edges(g);edg.first!=edg.second;++edg.first)
    {
      w=w+weightMap[*edg.first];
    }


    pair<vertex1,vertex1> vtx;
    for(vtx=vertices(g);vtx.first!=vtx.second;++vtx.first)
    {
      if(distanceMap[*vtx.first]>d)
      d=distanceMap[*vtx.first];
    }

    array_weights[m]=w;
    array_distances[m]=d;

    m++;
   }

  for(j=0;j<no_test;j++)
  {
    cout<<array_weights[j]<<" "<<array_distances[j]<<endl;
  }
return 0;
}

the program compiles perfectly.it gives problems for more than two edges.I just dont know why.Thank you

rama
  • 45
  • 1
  • 1
  • 6

1 Answers1

1

The problem with your program is that it declares two variables with name b. At the beginning the program declares a variable with name b of type int. Later it declares a variable of name b of type bool. The second declaration shadows the first declaration.

When the program does cin>>a>>b>>c; it will use the b of type bool. When you enter a value other than 0 or 1 for b, this will set the failbit for cin, because the value cannot be parsed as a bool (reference). After this, cin will not take input until cin.clear() is called, which resets the failbit. Since your program does not call cin.clear(), it will no longer take input and run past all the read operations.

To fix this, change the declaration of bool b; to bool inserted; and change the assignment tie(ed, b) = add_edge(a, b, g); to tie(ed, inserted) = add_edge(a, b, g);.

Additionally, you could add further error checking each time the program asks for input. This can be done by checking the result of cin.fail() after each input. Without such checks, your problem would also occur in case the user enters an invalid value that cannot be parsed as an integer (e.g. some string such as abc).

As a side note, I would recommend compiling with increased compiler warnings. This can help you detecting problems such as the one above. For example, compiling your program with g++ or clang++ using the flag -Wall to enable warnings will result in a warning about the first b being unused.