0

每次我执行程序(顺便说一句没有错误)它都会给我一个分段错误。它说“分段故障核心转储”。所以我添加了一些 cout<< 语句来找出发生这种情况的确切位置,结果它发生在“Hives 结束循环完成”之后。我不知道为什么或如何调整我的程序以使其正常工作。我在 RedHat 上使用 emacs。这是我的错误:

[[A[rsridhar@stud13-h56 ~]$ ./ants
1
3 3 3 1 2
2 0 3 3 3
loope started
loop ended
loope started
loop ended
loope started
loop ended
2 1 4 4 4
loope started
loop ended
loope started
loop ended
loope started
loop ended
0 1 2 2 2
loope started
loop ended
loope started
loop ended
loope started
loop ended
hives started
0 1 2
hives ended
hives started
hives ended
hives started
hives ended
Segmentation fault (core dumped)

我根本不明白为什么。我写了一个在 ym 计算机上运行良好的类似程序(这是 univs comp)。这是我的程序:

int main()
{
  typedef adjacency_list< vecS, vecS, undirectedS, property<vertex_distance_t,int, property< vertex_index_t, int> >, property< edge_weight_t, int> > Graph;

  int no_trees,no_edges,start_tree,finish_tree,no_species,w,min_edge=0,answers[100];
  int hives[10];
  for(int i=0;i<10;i++)
    hives[i]=0;

  int test=0;
  cin>>test;
  int m=0;
  while(m!=test)
    {

      cin>>no_trees>>no_edges>>no_species>>start_tree>>finish_tree;   //get input
      Graph g(no_trees);

      property_map< Graph,vertex_distance_t>:: type distance;
      property_map< Graph,vertex_index_t>:: type index ;
      property_map<Graph, edge_weight_t>::type weights[10];
      for(int i=0;i<no_species;i++)   //constructor for weights property
    {
          weights[i]=get(edge_weight,g);
    }
      typedef graph_traits<Graph>::edge_descriptor edge_desc;


      for(int i=0;i<no_edges;i++)   //input weights into weights array
    {
      int a=0,b=0;
      cin>>a>>b;
      edge_desc e;
      bool bol;
      tie(e,bol)=add_edge(a,b,g);
      for(int j=0;j<no_species;j++)
        {
              cout<<"loope started"<<endl;
          w=0;
          cin>>w;
              weights[j][e]=w;
              cout<<"loop ended"<<endl;
        }
    }
      for(int k=0;k<no_species;k++)
    { cout<<"hives started"<<endl;
      w=0;
      cin>>w;
      hives[k]=w;
      cout<<"hives ended"<<endl;
    }

      //find the minimum spanning tree for each of the weights in the graph
      cout<<"section 1";
      vector < vector< graph_traits < Graph >::vertex_descriptor> > p(10, vector<graph_traits < Graph >::vertex_descriptor>(num_vertices(g),0)) ;//predecessor vector

      cout<<"section2";
      //calling prim function on all diff prop_edges
      for(int i=0;i<no_species;i++)
    {
      cout<<"section3";
        prim_minimum_spanning_tree( g, hives[i], &p[i][0], distance, weights[i], index, default_dijkstra_visitor());
    cout<<"section4";
        }
      Graph g1(no_trees);
      property_map<Graph, edge_weight_t>:: type weight=get(edge_weight, g1);
      //join all the spanning trees into one
      int vertice_1,vertice_2;
      typedef graph_traits<Graph>::edge_iterator edge_itr;
      edge_itr ei,ee;

      for(tie(ei,ee)=edges(g);ei!=ee;++ei)
    { min_edge=0;
      cout<<"section5";
      vertice_1=source(*ei,g);
      vertice_2=target(*ei,g);
          for(int j=0;j<no_species;j++)
        { 
          if(p[j][vertice_1]==vertice_2 || p[j][vertice_2]==vertice_1)
        {
          if(weights[j][*ei]<min_edge)
            min_edge=weights[j][*ei];
        }
        }
      weight[*ei]=min_edge;
      bool bl;
      edge_desc c;
      cout<<"section6";
      tie(c,bl)=add_edge(vertice_1,vertice_2,g1);
    }
      std::vector<graph_traits<Graph>::vertex_descriptor> predecessors(num_vertices(g1));
std::vector<int> distances(num_vertices(g1));


      //call dijkstra algorithm()
      dijkstra_shortest_paths(g1,start_tree,predecessor_map(&predecessors[0]).distance_map(&distances[0]));

      answers[m]=distances[finish_tree];
      m++;
    }
  for( int i=0;i<test;i++)
    cout<<answers[i]<<endl;
      return 0;
}

非常感谢您!

4

0 回答 0