hdu2544 dijkstra

 1 #include<stdio.h>
 2 #define max 100000000;
 3 int main()
 4 {
 5     int n,m,i,j,k,min,a,b,c,hash[1005],ar[1005],length[105][105];
 6     while (~scanf("%d%d",&n,&m)&&(n!=0||m!=0))
 7     {
 8         for (i=1;i<=n;i++)
 9             for (j=1;j<=n;j++) length[i][j]=max;
10         for (i=1;i<=m;i++)
11             {scanf("%d%d%d",&a,&b,&c); length[a][b]=length[b][a]=c; }
12         for (i=1;i<=n;i++) {ar[i]=length[1][i]; hash[i]=0; }
13         for (i=1;i<n;i++)
14         {
15             min=max;
16             for (j=1;j<=n;j++)
17                 if ((hash[j]==0)&&(ar[j]<min)) {k=j; min=ar[j]; }
18             hash[k]=1;
19             for (j=1;j<=n;j++)
20                 if (min+length[k][j]<ar[j]) ar[j]=min+length[k][j];
21         }
22         printf("%d\n",ar[n]);
23     }
24 
25 
26 
27     return(0);
28 }

posted on 2014-07-16 16:51  xiao_xin  阅读(83)  评论(0编辑  收藏  举报

导航