原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=2544
最短路
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 43669 Accepted Submission(s): 19216
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& lt;=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在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
1 #include <cstdio> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 const int N = 200, INF = 0x3f3f3f3f; 6 int edge[N][N]; 7 int Dijkstra(int n) 8 { 9 int D[N], tag; 10 bool vis[N] = {0}; 11 memcpy(D, edge[0], sizeof(D)); 12 vis[0] = true; 13 for(int i=0; i<n; ++i) 14 { 15 tag = 0; 16 for(int j=0; j<n; ++j) 17 if(!vis[j]) 18 tag = D[j] < D[tag] ? j : tag; 19 vis[tag] = true; 20 for(int j=0; j<n; ++j) 21 if(!vis[j]) 22 D[j] = min(D[j], D[tag] + edge[tag][j]); 23 } 24 return D[n-1]; 25 } 26 int main(void) 27 { 28 int n, m, from, to, d; 29 while(scanf("%d %d", &n, &m), n+m) 30 { 31 memset(edge, INF, sizeof(edge)); 32 for(int i=0; i<m; ++i) 33 { 34 scanf("%d %d %d", &from, &to, &d); 35 --from, --to; 36 edge[from][to] = edge[to][from] = min(edge[from][to], d); 37 } 38 printf("%d\n", Dijkstra(n)); 39 } 40 }
Dijkstra
1 #include <cstdio> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 const int N = 200, INF = 0x3f3f3f3f; 6 int edge[N][N]; 7 int floyd(int n) 8 { 9 for(int k=0; k<n; ++k) 10 for(int i=0; i<n; ++i) 11 for(int j=0; j<n; ++j) 12 edge[i][j] = min(edge[i][j], edge[i][k] + edge[k][j]); 13 return edge[0][n-1]; 14 } 15 int main(void) 16 { 17 int n, m, from, to, d; 18 while(scanf("%d %d", &n, &m), n+m) 19 { 20 memset(edge, INF, sizeof(edge)); 21 for(int i=0; i<m; ++i) 22 { 23 scanf("%d %d %d", &from, &to, &d); 24 --from, --to; 25 edge[from][to] = edge[to][from] = min(edge[from][to], d); 26 } 27 printf("%d\n", floyd(n)); 28 } 29 }
Floyd
1 #include <cstdio> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 const int N = 200, M = 11000, INF = 0x3f3f3f3f; 7 int from[M], to[M], edge[M]; 8 int bellman(int n, int m) 9 { 10 int D[N], a, b; 11 memset(D, INF, sizeof(D)); 12 D[0] = 0; 13 for(int i=1; i<n; ++i) 14 { 15 for(int j=0; j<m; ++j) 16 { 17 a = from[j]; 18 b = to[j]; 19 D[b] = min(D[b], D[a] + edge[j]); 20 } 21 } 22 return D[n-1]; 23 } 24 int main(void) 25 { 26 int n, m, num, a, b, c; 27 while(scanf("%d %d", &n, &m), n+m) 28 { 29 num = 0; 30 for(int i=0; i<m; ++i) 31 { 32 scanf("%d %d %d", &a, &b, &c); 33 --a, --b; 34 from[num] = a; 35 to[num] = b; 36 edge[num++] = c; 37 from[num] = b; 38 to[num] = a; 39 edge[num++] = c; 40 } 41 printf("%d\n", bellman(n, num)); 42 } 43 }
Bellman-Ford
1 #include <cstdio> 2 #include <string.h> 3 #include <deque> 4 using namespace std; 5 const int N = 200, M = 1e4+100, INF = 0x3f3f3f3f; 6 int u[M], v[M], w[M], Next[M], first[N]; 7 int SPFA(int n) 8 { 9 int D[N], x, t; 10 bool cnt[N] = {0}; 11 memset(D, INF, sizeof(D)); 12 D[0] = 0; 13 cnt[0] = true; 14 deque<int> dq; 15 dq.push_front(0); 16 while(!dq.empty()) 17 { 18 t = dq.front(); 19 dq.pop_front(); 20 for(int x=first[t]; ~x; x=Next[x]) 21 { 22 if(D[v[x]] > D[u[x]] + w[x]) 23 { 24 D[v[x]] = D[u[x]] + w[x]; 25 if(!cnt[v[x]]) 26 { 27 if(!dq.empty() && D[v[x]] < D[dq.front()]) 28 dq.push_front(v[x]); 29 else 30 dq.push_back(v[x]); 31 cnt[v[x]] = true; 32 } 33 } 34 } 35 cnt[t] = false; 36 } 37 return D[n-1]; 38 } 39 int main(void) 40 { 41 int n, m, num; 42 while(scanf("%d %d", &n, &m), n+m) 43 { 44 memset(first, -1, sizeof(first)); 45 num = 0; 46 for(int i=0; i<m; ++i) 47 { 48 scanf("%d %d %d", u+num, v+num, w+num); 49 --u[num], --v[num]; 50 Next[num] = first[u[num]]; 51 first[u[num]] = num++; 52 u[num] = v[num-1], v[num] = u[num-1], w[num] = w[num-1]; 53 Next[num] = first[u[num]]; 54 first[u[num]] = num++; 55 } 56 printf("%d\n", SPFA(n)); 57 } 58 }
SPFA
时间: 2024-12-28 23:54:27