Description
Input
Output
Sample Input
Sample Output
1 # include<iostream> 2 # include<cstdio> 3 # include<cstring> 4 # include<algorithm> 5 using namespace std; 6 const int INF=1<<30; 7 int mp[105][105],n,m; 8 int dis[105]; 9 void bellman_ford() 10 { 11 int i,j; 12 for(i=1;i<=n;++i) 13 dis[i]=INF; 14 dis[1]=0; 15 bool update=true; 16 while(update){ 17 update=false; 18 for(i=1;i<=n;++i){ 19 for(j=1;j<=n;++j){ 20 if(mp[i][j]!=INF&&dis[j]>dis[i]+mp[i][j]){ 21 dis[j]=dis[i]+mp[i][j]; 22 update=true; 23 } 24 } 25 } 26 } 27 printf("%d\n",dis[n]); 28 } 29 int main() 30 { 31 int i,j,k,a,b,c; 32 while(scanf("%d%d",&n,&m),n+m) 33 { 34 for(i=1;i<=n;++i) 35 for(j=1;j<=n;++j) 36 mp[i][j]=INF; 37 while(m--) 38 { 39 scanf("%d%d%d",&a,&b,&c); 40 mp[a][b]=mp[b][a]=min(mp[a][b],c); 41 } 42 bellman_ford(); 43 } 44 return 0; 45 }