hdu-2544-最短路(Floyd算法模板)

题目链接

题意很清晰,入门级题目,适合各种模板,可用dijkstra, floyd, Bellman-ford, spfa

Dijkstra链接

Floyd链接

Bellman-Ford链接

SPFA链接

 1 /*
 2     Name:HDU-2544-最短路
 3     Copyright:
 4     Author:
 5     Date: 2018/4/17 10:34:47
 6     Description:
 7 */
 8 #include <cstring>
 9 #include <cstdio>
10 #include <iostream>
11 using namespace std; 
12 const int MAXN = 105;
13 const int INF = 0x3f3f3f3f;
14 int N, g[MAXN][MAXN], M;
15 void floyd () {
16     for (int k=1; k<=N; ++k)  {
17         for (int i=1; i<=N; i++) {
18             for (int j=1; j<=N; j++) {
19                 g[i][j] = min(g[i][j], (g[i][k]+g[k][j]));
20             }
21         }
22     }
23 }
24 int main()
25 {
26 //    freopen("in.txt", "r", stdin);
27     while (~scanf("%d %d", &N, &M) && (N+M)) {
28         memset(g, 0x3f, sizeof(g));
29         for (int i=0; i<M; i++) {
30             int a, b, c;
31             scanf("%d %d %d", &a, &b, &c);
32             if (g[a][b] > c) {
33                 g[a][b] = g[b][a] = c;
34             }
35         }
36         floyd();
37         cout<<g[1][N]<<endl;
38     }
39     return 0;
40 }

 

posted @ 2018-04-17 16:42  朤尧  阅读(316)  评论(0编辑  收藏  举报