[关闭]
@Archger 2017-03-18T16:09:20.000000Z 字数 3486 阅读 740

最短路算法包

ACM 最短路


  1. #include<iostream>
  2. #include<string>
  3. #include<string.h>
  4. #include<algorithm>
  5. #include<iomanip>
  6. #include<cstdio>
  7. #include<queue>
  8. #include<stack>
  9. #include<vector>
  10. #include<functional>
  11. #define INF 99999
  12. using namespace std;
  13. typedef pair<int, int> P;
  14. struct edge {
  15. int from, to, cost;
  16. };
  17. int n, m; //n为顶点数,m为边数
  18. int a[100][100];
  19. void warshall()
  20. {
  21. int b[100][100] = { 0 };
  22. for (int i = 0; i <= n; i++)
  23. for (int j = 0; j <= n; j++)
  24. b[i][j] = a[i][j];
  25. cout << "Floyd-warshall: \n";
  26. for (int i = 1; i <= n; i++)
  27. {
  28. for (int j = 1; j <= n; j++)
  29. {
  30. for (int k = 1; k <= n; k++)
  31. {
  32. if (b[j][k] > b[j][i] + b[i][k])
  33. {
  34. b[j][k] = b[j][i] + b[i][k];
  35. }
  36. }
  37. }
  38. }
  39. for (int i = 1; i <= n; i++)
  40. {
  41. for (int j = 1; j <= n; j++)
  42. {
  43. cout << b[i][j] << " ";
  44. }
  45. cout << endl;
  46. }
  47. }
  48. void Dijkstra()
  49. {
  50. int dis[100] = { 0 };
  51. bool book[100] = { 0 };
  52. int b[100][100] = { 0 };
  53. for (int i = 0; i <= n; i++)
  54. for (int j = 0; j <= n; j++)
  55. b[i][j] = a[i][j];
  56. for (int i = 1; i <= n; i++)
  57. {
  58. dis[i] = a[1][i];
  59. }
  60. book[1] = 1;
  61. for (int i = 1; i <= n - 1; i++)
  62. {
  63. int minv = INF, min = 0;
  64. for (int i = 1; i <= n; i++)
  65. {
  66. if (!book[i] && minv > dis[i])
  67. {
  68. minv = dis[i]; //在取最小值的过程中可以用堆优化
  69. min = i;
  70. }
  71. }
  72. book[min] = 1;
  73. for (int j = 1; j <= n; j++)
  74. {
  75. if (dis[j] > dis[min] + a[min][j])
  76. {
  77. dis[j] = dis[min] + a[min][j]; //用最短的边对其他所有点进行优化
  78. }
  79. }
  80. }
  81. for (int i = 1; i <= n; i++)
  82. {
  83. cout << dis[i] << " ";
  84. }
  85. cout << endl;
  86. }
  87. void Bellman()
  88. {
  89. int dis[100] = { 0 };
  90. int b[100][100] = { 0 };
  91. for (int i = 0; i <= n; i++)
  92. for (int j = 0; j <= n; j++)
  93. b[i][j] = a[i][j];
  94. for (int i = 1; i <= n; i++)
  95. dis[i] = INF;
  96. dis[1] = 0;
  97. /*for (int i = 1; i <= n; i++)
  98. {
  99. dis[i] = a[1][i];
  100. }*/
  101. for (int i = 1; i <= n - 1; i++)
  102. {
  103. for (int j = 1; j <= n; j++)
  104. {
  105. for (int k = 1; k <= n; k++)
  106. {
  107. if (dis[k] > dis[j] + a[j][k])
  108. {
  109. dis[k] = dis[j] + a[j][k];
  110. }
  111. }
  112. }
  113. }
  114. for (int i = 1; i <= n; i++)
  115. {
  116. cout << dis[i] << " ";
  117. }
  118. cout << endl;
  119. }
  120. void BellmanQueue()
  121. {
  122. int dis[100] = { 0 };
  123. int b[100][100] = { 0 };
  124. for (int i = 0; i <= n; i++)
  125. for (int j = 0; j <= n; j++)
  126. b[i][j] = a[i][j];
  127. for (int i = 1; i <= n; i++)
  128. dis[i] = INF;
  129. dis[1] = 0;
  130. queue<int> que;
  131. bool book[100] = { 0 };
  132. que.push(1);
  133. book[1] = 1;
  134. while (que.size())
  135. {
  136. int k = que.front();
  137. que.pop();
  138. for (int i = 1; i <= n; i++)
  139. {
  140. if (dis[i] > dis[k] + b[k][i])
  141. {
  142. dis[i] = dis[k] + b[k][i];
  143. if (!book[i])
  144. {
  145. que.push(i);
  146. book[i] = 1;
  147. }
  148. }
  149. }
  150. book[k] = 0;
  151. }
  152. for (int i = 1; i <= n; i++)
  153. {
  154. cout << dis[i] << " ";
  155. }
  156. cout << endl;
  157. }
  158. void warshall2()
  159. {
  160. vector<edge> G[100];
  161. int n, m;
  162. cin >> n >> m;
  163. for (int i = 0; i < m; i++)
  164. {
  165. int p;
  166. edge q;
  167. cin >> p >> q.to >> q.cost;
  168. G[p].push_back(q);
  169. }
  170. for (int i = 1; i <= n; i++)
  171. {
  172. for (int k = 1; k <= n; k++)
  173. {
  174. for (int j = 0; j < G[k].size(); j++)
  175. {
  176. edge e = G[k][j];
  177. }
  178. }
  179. }
  180. cout << "不会\n";
  181. }
  182. void Dijkstra2()
  183. {
  184. vector<edge> G[100];
  185. int n, m;
  186. cin >> n >> m;
  187. int dis[100] = { 0 };
  188. fill(dis, dis + 100, INF);
  189. dis[1] = 0;
  190. for (int i = 0; i < m; i++)
  191. {
  192. int p;
  193. edge q;
  194. cin >> p >> q.to >> q.cost;
  195. G[p].push_back(q);
  196. }
  197. priority_queue<P, vector<P>, greater<P> >que;
  198. que.push(P(0, 1));
  199. while (que.size())
  200. {
  201. P p= que.top(); que.pop();
  202. for (int i = 0; i < G[p.second].size(); i++)
  203. {
  204. edge e = G[p.second][i];
  205. if (dis[e.to] > dis[p.second] + e.cost)
  206. {
  207. dis[e.to] = dis[p.second] + e.cost;
  208. que.push(P(dis[e.to], e.to));
  209. }
  210. }
  211. }
  212. for (int i = 1; i <= n; i++)
  213. {
  214. cout << dis[i] << " ";
  215. }
  216. cout << endl;
  217. }
  218. void Bellman2()
  219. {
  220. edge G[100];
  221. int n, m;
  222. cin >> n >> m;
  223. int dis[100] = { 0 };
  224. fill(dis, dis + 100, INF);
  225. dis[1] = 0;
  226. for (int i = 0; i < m; i++)
  227. {
  228. cin >> G[i].from >> G[i].to >> G[i].cost;
  229. }
  230. for (int i = 1; i <= n - 1; i++)
  231. {
  232. for (int j = 0; j < m; j++)
  233. {
  234. dis[G[j].to] = min(dis[G[j].to], dis[G[j].from] + G[j].cost);
  235. }
  236. }
  237. for (int i = 1; i <= n; i++)
  238. {
  239. cout << dis[i] << " ";
  240. }
  241. cout << endl;
  242. }
  243. int main()
  244. {
  245. cin >> n >> m;
  246. for(int i=0;i<=n;i++)
  247. for (int j = 0; j <= n; j++)
  248. {
  249. if (i == j) a[i][j] = 0;
  250. else a[i][j] = INF;
  251. }
  252. for (int i = 1; i <= m; i++)
  253. {
  254. int p, q, t;
  255. cin >> p >> q >> t;
  256. a[p][q] = t;
  257. }
  258. cout << "邻接矩阵:\n";
  259. cout << "1:Floyd-warshall 2:Dijkstra 3:Bellman-Ford 4:Bellman队列优化(SPFA)\n";
  260. cout << "邻接表\n";
  261. cout << "5:Floyd-warshall 6:Dijkstra(堆优化) 7:Bellman-Ford\n";
  262. int t;
  263. while (cin >> t)
  264. {
  265. if (t == 1)
  266. {
  267. warshall();
  268. }
  269. else if (t == 2)
  270. {
  271. cout << "Dijkstra:\n";
  272. Dijkstra();
  273. }
  274. else if (t == 3)
  275. {
  276. cout << "Bellman-Ford:\n";
  277. Bellman();
  278. }
  279. else if (t == 4)
  280. {
  281. cout << "Bellman队列优化:\n";
  282. BellmanQueue();
  283. }
  284. else if (t == 5)
  285. {
  286. cout << "Floyd-warshall(邻接表):\n";
  287. warshall2();
  288. }
  289. else if (t == 6)
  290. {
  291. cout << "Dijkstra(堆优化) \n";
  292. Dijkstra2();
  293. }
  294. else if (t == 7)
  295. {
  296. cout << "Bellman-Ford:\n";
  297. Bellman2();
  298. }
  299. }
  300. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注