标签:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int maxn = 32; const int INF = 99999999; int map[maxn][maxn],n,m,dis[maxn][maxn]; int max(int x,int y) { return x>y?x:y; } int floyd() { int i,j,k; for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(dis[i][j]>dis[i][k]+dis[k][j]) dis[i][j]=dis[i][k]+dis[k][j]; } } } return dis[1][n]; } int del(int u) { int i,j; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==j)dis[i][j]=INF; if(i==u||j==u) dis[i][j]=INF; else if(map[i][j]==0) dis[i][j]=INF; else dis[i][j]=map[i][j]; } } return floyd(); } int main() { int i,j; while(scanf("%d%d",&n,&m)!=EOF) { if(!n&&!m)break; memset(map,0,sizeof(map)); for(i=0;i<m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); if(map[x][y]==0||map[x][y]>z) map[x][y]=map[y][x]=z; } int flag=del(0); if(flag>=INF) { printf("Inf\n"); continue; } //printf("%d\n",flag); int ans=0; for(i=2;i<n;i++) { int temp=del(i); ans=max(ans,temp); } if(ans>=INF) printf("Inf\n"); else printf("%d\n",ans); } }
标签:
原文地址:http://www.cnblogs.com/sweat123/p/4876116.html