题意:题意很好理解,就是从起点带终点的最短距离。

连接:http://acm.hdu.edu.cn/showproblem.php?pid=2544

View Code
 1 #include <iostream>
 2 using namespace std;
 3 #define INF 0x3fffffff
 4 #include <queue>
 5 #define MAX 1000+10
 6 int map[MAX][MAX];
 7 int n;
 8 int m;
 9 int dis[MAX];
10 void spfa(int k)
11 {
12     int used[MAX];
13     queue<int >q;
14     memset(used,0,sizeof(used));
15     for(int i=1;i<=n;i++)
16     {
17         dis[i]=INF;
18     }
19     dis[k]=0;
20     q.push(k);
21     used[k]=1;
22     while(!q.empty())
23     {
24         int head=q.front();
25         q.pop();
26         used[head]=0;
27         for(int j=1;j<=n;j++)
28         {
29             if(map[head][j]!=0&&dis[j]>dis[head]+map[head][j])//这里判断 时候要注意是否为0
30             {
31                 dis[j]=dis[head]+map[head][j];
32                 if(!used[j])
33                 {
34                     used[j]=1;
35                     q.push(j);
36                 }
37             }
38         }
39     }
40     return;
41 }
42 int main()
43 {
44     while(scanf("%d%d",&n,&m),n+m)
45     {
46         memset(map,0,sizeof(map));//我的初始化为0 所有 要判断 用的时候 是否 是大于0 的
47         while(m--)
48         {
49             int a;
50             int b;
51             int value;
52             scanf("%d%d%d",&a,&b,&value);
53             map[a][b]=value;
54             map[b][a]=value;
55         }
56         spfa(1);
57         printf("%d\n",dis[n]);
58     }
59     return 0;
60 }