题目链接:HDU - 1599
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #define inf 0x7fffffff 8 using namespace std; 9 const int maxn=100+10; 10 11 int n,m; 12 int dist[maxn][maxn],edge[maxn][maxn]; 13 14 void floyd() 15 { 16 int ans=1000100; 17 for (int i=1 ;i<=n ;i++) 18 { 19 for (int j=1 ;j<=n ;j++) 20 dist[i][j]=edge[i][j]; 21 } 22 for (int k=1 ;k<=n ;k++) 23 { 24 for (int i=1 ;i<k ;i++) 25 { 26 for (int j=i+1 ;j<k ;j++) 27 { 28 if (dist[j][i]+edge[i][k]+edge[k][j]<1000100) 29 ans=min(ans,dist[j][i]+edge[i][k]+edge[k][j]); 30 } 31 } 32 for (int i=1 ;i<=n ;i++) 33 { 34 for (int j=1 ;j<=n ;j++) 35 { 36 if (dist[i][j]>dist[i][k]+dist[k][j]) 37 dist[i][j]=dist[i][k]+dist[k][j]; 38 } 39 } 40 } 41 if (ans==1000100) printf("It‘s impossible.\n"); 42 else printf("%d\n",ans); 43 } 44 45 int main() 46 { 47 while (scanf("%d%d",&n,&m)!=EOF) 48 { 49 for (int i=1 ;i<=n ;i++) 50 { 51 for (int j=1 ;j<=n ;j++) 52 edge[i][j]=1000100; 53 edge[i][i]=0; 54 } 55 int x,y,cost; 56 for (int i=0 ;i<m ;i++) 57 { 58 scanf("%d%d%d",&x,&y,&cost); 59 if (edge[x][y]>cost) edge[x][y]=edge[y][x]=cost; 60 } 61 floyd(); 62 } 63 return 0; 64 }
hdu 1599 find the mincost route 最小环
原文:http://www.cnblogs.com/huangxf/p/4491317.html