HDU_2544——最短路,Dijkstra模版

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条商店到赛场的路线。
 

 

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 <cstring>
 3 const int MAXN = 101;
 4 const int INF = 1<<20;
 5 
 6 int n,m;
 7 int map[MAXN][MAXN], mark[MAXN];
 8 int main()
 9 {
10     while(~scanf("%d%d",&n,&m), n|m)
11     {
12         for(int i = 1; i <= n; i++)
13         {
14             for(int j = 1; j <= n; j++)
15             {
16                 map[i][j] = INF;
17             }
18             mark[i] = 0;
19         }
20         mark[1] = 1;
21         
22         for(int i = 1, a, b, c; i <= m; i++)
23         {
24             scanf("%d%d%d",&a,&b,&c);
25             if(map[a][b] > c)
26             {
27                 map[a][b] = map[b][a] = c;
28             }
29         }
30         
31         m = n - 1; 
32         while(m--)
33         {
34             int max = INF, x;
35             for(int i = 2; i <= n; i++)
36             {
37                 if(!mark[i] && map[1][i] <= max)
38                 {
39                     max = map[1][x=i];
40                 }
41             }
42             mark[x] = 1;
43             for(int i = 2; i <= n; i++)
44             {
45                 if(!mark[i])
46                 {
47                     map[1][i] <?= map[1][x] + map[x][i];
48                 }
49             }
50         }
51         printf("%d\n",map[1][n]);
52     }
53     return 0;
54 }

 

posted @ 2013-07-24 14:41  瓶哥  Views(231)  Comments(0Edit  收藏  举报