标签:
Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3273 Accepted Submission(s):
1320
#include<stdio.h> #include<string.h> #define INF 0x3f3f3f #define min(x,y)(x<y?x:y) #define MAX 1010 int n,m; int map[MAX][MAX]; int dis[MAX][MAX]; void init() { int i,j; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { map[i][j]=i==j?0:INF; dis[i][j]=i==j?0:INF; } } void getmap() { int i,j; for(i=1;i<=m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); if(map[a][b]>c) map[a][b]=map[b][a]=dis[a][b]=dis[b][a]=c; } } void floyd() { int i,j,k; int sum=INF; for(k=1;k<=n;k++) { for(i=1;i<k;i++) //求最小环 for(j=1;j<i;j++) sum=min(sum,map[i][k]+map[k][j]+dis[j][i]); for(i=1;i<=n;i++)//更新最短路径 for(j=1;j<=n;j++) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); } if(sum==INF) printf("It‘s impossible.\n"); else printf("%d\n",sum); } int main() { while(scanf("%d%d",&n,&m)!=EOF) { init(); getmap(); floyd(); } return 0; }
hdoj 1599 find the mincost route【floyd+最小环】
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4728324.html