算法第四版 图论 广度优先搜索最短路径(所含边数最少, C++)

算法第四版 图论 广度优先搜索最短路径(所含边数最少, C++)

  1 #include <iostream>
  2 #include <forward_list>
  3 #include <vector>
  4 #include <queue>
  5 #include <stack>
  6 
  7 #define MAX_S 100
  8 /* 
  9  * 先将起点加入队列,然后重复以下步骤直到队列为空:
 10  * 1.取队列中的下一个顶点v并标记它;
 11  * 2.将与v相邻的所有未被标记过的顶点加入队列。
 12  */
 13 
 14 using namespace std;
 15 
 16 class Graph{
 17         int v;
 18         int e;
 19         vector<forward_list<int>> adjt;
 20 
 21         public:
 22                 //两个构造函数
 23                 Graph(int V ): v(V){
 24                         e = 0;
 25                         vector<forward_list<int>> adjt1(MAX_S);
 26                         adjt = adjt1;
 27                 }
 28 
 29                 Graph(int V,int E): v(V), e(E)
 30                 {
 31                         vector<forward_list<int>> adjt1(MAX_S);
 32                         adjt = adjt1;
 33 
 34                         for (int i = 0; i < e; ++i)
 35                         {
 36                                 int v, w;
 37                                 cin >> v >> w;
 38                                 addEdge(v,w);
 39                         }
 40                 }
 41                 int V(){
 42                         return v;
 43                 }
 44                 int E(){
 45                         return e;
 46                 }
 47                 //加入新边
 48                 void addEdge(int v, int w)
 49                 {
 50                         adjt[v].push_front(w);
 51                         adjt[w].push_front(v);
 52                 }
 53                 //给定顶点的邻接顶点    
 54                 forward_list<int> adj(int v)
 55                 {
 56                         return adjt[v];
 57                 }
 58 };
 59 
 60 class BreadthFirstPaths{
 61         vector<int> edgeTo;
 62         vector<bool> marked;
 63         int s;
 64 
 65         public:
 66                 BreadthFirstPaths(Graph G, int s): s(s)
 67         {
 68                 vector<int> edgeTo1(MAX_S);
 69                 vector<bool> marked1(MAX_S);
 70 
 71                 edgeTo = edgeTo1;
 72                 marked = marked1;
 73                 bfs(G, s);
 74         }
 75 
 76                 void bfs(Graph G, int s)
 77                 {
 78                         queue<int> Que;
 79                         marked[s] = true;
 80                         Que.push(s);
 81 
 82                         while (!Que.empty())
 83                         {
 84                                 int v = Que.front();
 85                                 Que.pop();
 86                                 for(auto &c : G.adj(v))
 87                                 {
 88                                         if (!marked[c])
 89                                         {
 90                                                 marked[c] = true;
 91                                                 edgeTo[c] = v;
 92                                                 Que.push(c);
 93                                         }
 94                                 }
 95                         }
 96                 }
 97 
 98                 bool hasPathTo(int v)
 99                 {
100                         return marked[v];
101                 }
102                 stack<int>  pathTo(int v)
103                 {
104                         stack<int> path;
105                         if (!hasPathTo(v))
106                                 return path;
107                         for (int x = v; x != s; x = edgeTo[x])
108                                 path.push(x);
109                         path.push(s);
110                         return path;
111                 }
112 };
113 
114 int main()
115 {
116         int v, e;
117         cout << "Please enter two integer to vertices and edges: ";
118         cin >> v >> e;
119 
120         cout << "Please enter edges: ";
121         Graph G(v, e);
122 
123         char ch;
124         int s;
125 
126         cout << "Do you want to enter ?(Y/N): ";
127         while (cin >> ch)
128         {
129                 if (ch != 'Y' && ch != 'N')
130                 {
131                         cerr << "error, please input again!" << endl;
132                         continue;
133                 }
134                 if (ch == 'Y')
135                 {
136                         cout << "Please enter the sources of vertices: " ;
137                         cin >> s;
138                         BreadthFirstPaths Paths(G, s);
139 
140                         for (int i = 0; i < G.V(); ++i)
141                         {
142                                 if (Paths.hasPathTo(i))
143                                 {
144                                         cout << s << " to " << i << ": ";
145                                         stack<int> st = Paths.pathTo(i);
146                                         int size = st.size();
147                                         for (int j = 0; j < size; ++j)
148                                         {
149                                                 cout << st.top() << ' ';
150                                                 st.pop();
151                                         }
152                                         cout << endl;
153                                 }
154                         }
155                 }
156                 if (ch == 'N')
157                         break;
158 
159                 cout << "Do you want to enter ?(Y/N): ";
160         }
161 }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值