1 #include <iostream> 2 #include <boost/config.hpp> 3 //图(矩阵实现) 4 #include <boost/graph/adjacency_matrix.hpp> 5 #include <boost\graph\graph_utility.hpp> 6 #include <boost/graph/graph_traits.hpp> 7 //图(链表实现) 8 #include <boost/graph/adjacency_list.hpp> 9 //求最小生成树 10 #include <boost/graph/kruskal_min_spanning_tree.hpp> 11 using namespace std; 12 using namespace boost; 13 14 //顶点名称 15 enum { A, B, C, D, E, F }; 16 //顶点个数 17 #define N 6 18 const char *name = "ABCDEF"; 19 20 //无向图 21 void main() 22 { 23 //图,每个结点是vec来实现,无向图,有边长与权重的属性 24 adjacency_list<vecS, vecS, undirectedS, no_property, property<edge_weight_t, int>> myg; 25 add_edge(A, B,13, myg); 26 add_edge(B, C,23 ,myg); 27 add_edge(A, C,1, myg); 28 add_edge(A, D,11, myg); 29 add_edge(C, D,10, myg); 30 add_edge(B, D,11, myg); 31 32 //链表 33 typedef adjacency_list<vecS, vecS, undirectedS, property<edge_weight_t, int>> mygraph; 34 //创建边的链表 35 list<mygraph::edge_descriptor> mylist; 36 //生成的结果尾插到mylist 37 kruskal_minimum_spanning_tree(myg, back_inserter(mylist)); 38 39 //创建边与权重的映射(weight是函数指针) 40 auto weight= get(edge_weight,myg); 41 //property_map<mygraph, edge_weight_t>::type weight = get(edge_weight, myg); 42 43 for (auto ib = mylist.begin(); ib != mylist.end(); ib++) 44 { 45 //输出边 46 //cout << *ib << endl; 47 cout << source(*ib, myg) << "->" << target(*ib, myg) << "权重" << weight(*ib) << endl; 48 } 49 cin.get(); 50 }