运行结果:
代码示例:
1 #include <iostream> 2 #include <vector> 3 #include <deque> 4 #include <boost/config.hpp> 5 #include <boost/graph/adjacency_matrix.hpp> 6 #include <boost/graph/graph_utility.hpp>//打印顶点,边长 7 #include <boost/graph/adjacency_list.hpp> 8 //拓扑排序 9 #include <boost/graph/topological_sort.hpp> 10 11 using namespace std; 12 using namespace boost; 13 14 void main() 15 { 16 vector<string> task; 17 task.push_back("幼儿园接小孩"); 18 task.push_back("买菜"); 19 task.push_back("去ATM取钱"); 20 task.push_back("送孩子去看书"); 21 task.push_back("煮饭"); 22 task.push_back("去图书馆接孩子"); 23 task.push_back("吃饭"); 24 25 typedef pair<int, int>pint; 26 pint ea[] = { pint(0,3),pint(1,3),pint(1,4), pint(2,1), pint(3,5), pint(4,6), pint(5,6) }; 27 //初始化 28 vector<pint> myv(ea, ea + 7); 29 //生成图 30 adjacency_list<listS, vecS, directedS> myg(myv.begin(), myv.end(), myv.size()); 31 32 deque<int> top_order; 33 //拓扑排序 34 topological_sort(myg, front_inserter(top_order), vertex_index_map(identity_property_map())); 35 for (auto ib = top_order.begin(); ib != top_order.end(); ib++) 36 { 37 cout << task[*ib] << endl; 38 } 39 cin.get(); 40 }