最小环用floyd改编。
hdu1599特殊一些。要求至少有三个不同的点,并且除了起点与终点重合外,中间不能有环。有点很奇怪,最大值不能为0x3f3f3f3f。
poj1374就没那么讲究。
1 //hdu1599 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<algorithm> 6 using namespace std; 7 const int N = 110, INF=1000000; 8 int Map[N][N], dist[N][N], pre[N][N]; 9 int mc; 10 void fc(int n) 11 { 12 int i,j,k; 13 mc=INF; 14 for(i=1;i<=n;i++) 15 { 16 for(j=1;j<=n;j++) 17 { 18 dist[i][j]=Map[i][j]; 19 pre[i][j]=i; 20 } 21 } 22 for(k=1;k<=n;k++) 23 { 24 for(i=1;i<k;i++) 25 { 26 for(j=1;j<i;j++) 27 { 28 if(dist[i][j]+Map[k][j]+Map[i][k]<mc) 29 mc=min(mc,dist[i][j]+Map[k][j]+Map[i][k]); 30 } 31 } 32 for(i=1;i<=n;i++) 33 { 34 for(j=1;j<=n;j++) 35 { 36 if(dist[k][j]!=INF&&dist[i][k]!=INF&&dist[i][j]>dist[i][k]+dist[k][j]) 37 { 38 dist[i][j]=dist[i][k]+dist[k][j]; 39 pre[i][j]=pre[k][j]; 40 } 41 } 42 } 43 } 44 } 45 void init(int n) 46 { 47 for(int i=0;i<=n;i++) 48 { 49 Map[i][i]=0; 50 for(int j=0;j<i;j++) 51 Map[j][i]=Map[i][j]=INF; 52 } 53 } 54 int main() 55 { 56 //freopen("test.txt","r",stdin); 57 int n,m,i,j,k; 58 while(scanf("%d%d",&n,&m)!=EOF) 59 { 60 init(n); 61 while(m--) 62 { 63 scanf("%d%d%d",&i,&j,&k); 64 if(i==j) continue; 65 Map[i][j]=Map[j][i]=min(Map[i][j],k); 66 } 67 fc(n); 68 if(mc!=INF) printf("%d\n",mc); 69 else printf("It's impossible.\n"); 70 } 71 return 0; 72 }