hdu 2544 最短路
Posted 我只有一件白T恤
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 2544 最短路相关的知识,希望对你有一定的参考价值。
最短路
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 80757 Accepted Submission(s): 34969
Problem Description
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?
Input
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。
输入保证至少存在1条商店到赛场的路线。
Output
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
Sample Input
2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
3
2
思路:最短路水题,练习算法。
- 不知道什么名字的算法:
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
24454955 | 2018-04-16 21:04:36 | Accepted | 2544 | 62MS | 2024K | 808 B | C++ | zqiangda |
#include<cstdio> #include<vector> #include<cstring> #include<iostream> using namespace std; struct Node{ int s, e, w; Node(int a, int b, int c) :s(a), e(b), w(c){} }; int main() { int n, m; long len[105]; vector<Node>vec; while (cin >> n >> m, n+m){ vec.clear(); for (int i = 0; i < m; i++){ int a, b, c; cin >> a >> b >> c; vec.push_back(Node(a, b, c)); } memset(len, 125, sizeof(len)); len[1] = 0; int flag = 1; while (flag){ flag = 0; for (int i = 0; i < vec.size(); i++){ int s = vec[i].s, e = vec[i].e, w = vec[i].w; if (len[e]>len[s] + w){ len[e] = len[s] + w; flag = 1; } if (len[s] > len[e] + w){ len[s] = len[e] + w; flag = 1; } } } cout << len[n] << endl; } return 0; }
- SPFA算法:
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
24455250 | 2018-04-16 21:19:34 | Accepted | 2544 | 62MS | 2068K | 974 B | C++ | zqiangda |
#include<cstdio> #include<vector> #include<cstring> #include<queue> #include<iostream> using namespace std; struct Node{ int e, w; Node(int b, int c) :e(b), w(c){} }; int n, m; long len[105], vis[105]; vector<Node>vec[105]; void spfa(int s) { memset(len, 125, sizeof(len)); memset(vis, 0, sizeof(vis)); len[s] = 0; queue<int>que; que.push(s); while (!que.empty()){ int p = que.front(); que.pop(); vis[p] = 0; for (int i = 0; i < vec[p].size(); i++){ int e = vec[p][i].e, w = vec[p][i].w; if (len[e]>len[p] + w){ len[e] = len[p] + w; if (!vis[e]){ que.push(e); vis[e] = 1; } } } } } int main() { while (cin >> n >> m, n+m){ for (int i = 1; i <= n;i++) vec[i].clear(); for (int i = 0; i < m; i++){ int a, b, c; cin >> a >> b >> c; vec[a].push_back(Node(b, c)); vec[b].push_back(Node(a, c)); } spfa(1); cout << len[n] << endl; } return 0; }
- dijkstra算法:
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
24455452 | 2018-04-16 21:32:54 | Accepted | 2544 | 46MS | 2064K | 1123 B | C++ | zqiangda |
#include<cstdio> #include<vector> #include<cstring> #include<climits> #include<iostream> using namespace std; struct Node{ int e, w; Node(int b, int c) :e(b), w(c){} }; int n, m; long len[105], vis[105]; vector<Node>vec[105]; void dijkstra(int s) { memset(len, 125, sizeof(len)); memset(vis, 0, sizeof(vis)); for (int i = 0; i < vec[s].size(); i++){ int e = vec[s][i].e, w = vec[s][i].w; len[e] = w; } len[s] = 0; vis[s] = 1; for (int i = 0; i < n; i++){ int min = INT_MAX, p = 1; for (int j = 1; j <= n; j++){ if (len[j] < min&&vis[j] == 0){ min = len[j]; p = j; } } vis[p] = 1; for (int j = 0; j < vec[p].size(); j++){ int e = vec[p][j].e, w = vec[p][j].w; if (len[e]>len[p] + w&&vis[e] == 0){ len[e] = len[p] + w; } } } } int main() { while (cin >> n >> m, n+m){ for (int i = 1; i <= n;i++) vec[i].clear(); for (int i = 0; i < m; i++){ int a, b, c; cin >> a >> b >> c; vec[a].push_back(Node(b, c)); vec[b].push_back(Node(a, c)); } dijkstra(1); cout << len[n] << endl; } return 0; }
- Floyd算法:
#include<cstdio> #include<vector> #include<cstring> #include<climits> #include<iostream> using namespace std; int main() { int n, m; long map[105][105]; while (cin >> n >> m, n+m){ //memset(map, 125, sizeof(map)); for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++) map[i][j] = INT_MAX; map[i][i] = 0; } for (int i = 0; i < m; i++){ int a, b, c; cin >> a >> b >> c; map[a][b] = map[b][a] = c; } for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++){ if (map[i][k] + map[k][j] < map[i][j]) map[i][j] = map[i][k] + map[k][j]; // printf("%10d\t", map[i][j]); } cout << map[1][n] << endl; } return 0; }
最后一个待调试~
以上是关于hdu 2544 最短路的主要内容,如果未能解决你的问题,请参考以下文章