2011-10-21 6 views
2

Boost :: Graphで私の最初のステップを実行していて、予期しない動作が発生しました。ブーストグラフの外部プロパティが異常に動作していますか?

私が望むのは、一連のedge_weightプロパティ(実行時にしか数値がわかりません)を持ち、一定の制約を満たすすべての重みの最小値を使用することです。まず、typedef宣言は次のように

typedef adjacency_list<vecS, vecS, undirectedS, property<vertex_distance_t, int>, property<edge_weight_t, int> > Graph; 
typedef graph_traits<Graph>::edge_descriptor Edge; 
typedef property_map<Graph, edge_weight_t>::type WeightMap; 
typedef property_map<Graph, vertex_distance_t>::type DistanceMap; 

私は、グラフを初期化します。

void testcase() { 
    int t, e, s, a, b; 
    cin >> t >> e >> s >> a >> b; 
    Graph g(t); 
    WeightMap fastestLinkWeight = get(edge_weight, g); 
    vector<WeightMap> weightMaps(s); 
    for (int i=0;i<e;i++) { 
     int u, v; 
     cin >> u >> v; 

     Edge edge; bool worked; 
     tie(edge, worked) = add_edge(u, v, g); 
     for (int j=0;j<s;j++) { 
      cin >> weightMaps[j][edge]; 
     } 
     fastestLinkWeight[edge] = INT_MAX; 

     cout << weightMaps[0][edge] << "\n"; 
    } 
} 

をそして、それは何度もINT_MAXを出力します。 (外部)weightMaps[j]はすべて同じで、内部プロパティーfastestLinkWeightと同じです。しかし、なぜ?別のマップを使用するにはどうすればよいですか?

答えて

4

私はそれを修正することができました。重要な観測事項:

WeightMapは単なるインターフェイスタイプです。質問のコードのように初期化されている場合、その動作は未定義です。

代わりに、コンテナにデータを格納し、それが(the documentation on property mapsが説明しているようにそれは、get()put()operator[]方法である)によるインターフェースを実装して確認する必要があります。次のように私の場合は

、問題を解決することができる。

は、ベクトル要素のインデックスにエッジ記述子を変換するために使用されるEdgeIndexMapを定義します。

typedef property_map<Graph, edge_index_t>::type EdgeIndexMap; 

iterator_property_map上記EdgeIndexMapタイプ使用:

typedef iterator_property_map<int*, EdgeIndexMap, int, int&> IterWeightMap; 

一つは次にをインスタンス化することができvector<vector<int> >に提供されたデータを使用して:edge_indexプロパティが(自然に)内部プロパティとして格納されていることを

EdgeIndexMap eim = get(edge_index, g); 
vector<vector<int> > weights(s, vector<int>(e)); 
vector<IterWeightMap> weightMaps(s); 
for (int j=0;j<s;j++) { 
    weightMaps[j] = make_iterator_property_map(&(weights[j][0]), eim); 
} 

注意。

このように、異なるedge_weight特性はBGLアルゴリズムで使用することができる例えば、として通常呼び出し:

kruskal_minimum_spanning_tree(g, std::back_inserter(privateNetwork), weight_map(weightMaps[j]));