HDU 2544 最短路

参考:https://blog.csdn.net/shuangde800/article/details/7987134

https://blog.csdn.net/gfvod/article/details/51506901

https://blog.csdn.net/u011466175/article/details/11906001

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int N=110;
 6 const int inf=0x3f3f3f3f;
 7 int in[N][N];
 8 int n,m;
 9 void read()
10 {
11     memset(in,0x3f,sizeof(in));
12     for (int i=0;i<m;i++)
13     {
14         int a,b,c;
15         cin>>a>>b>>c;
16         in[a][b]=in[b][a]=c;
17     }
18 }
19 void floyd()
20 {
21     for (int k=1;k<=n;k++)
22     {
23         for (int i=1;i<=n;i++)
24         {
25             for (int j=1;j<=n;j++)
26             {
27                 if (in[i][k]+in[k][j]<in[i][j])
28                 {
29                     in[i][j]=in[i][k]+in[k][j];
30                 }
31             }
32         }
33     }
34 }
35 int main()
36 {
37     while (cin>>n>>m,n+m)
38     {
39         read();
40         floyd();
41         cout<<in[1][n]<<endl;
42     }
43 
44     return 0;
45 }

 

posted @ 2018-08-08 17:33  hemeiwolong  阅读(106)  评论(0编辑  收藏  举报