hdu 1599 最小环模板
最小环模板
floyed
http://acm.hdu.edu.cn/showproblem.php?pid=1599
#include <bits/stdc++.h> #define inf 2333333333333 #define N 210 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) //by war //2020.8.4 using namespace std; long long n,m,x,y,v,ans; long long f[N][N],val[N][N]; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } signed main(){ while(cin>>n>>m){ ans=inf; For(i,1,n) For(j,1,n) f[i][j]=val[i][j]=inf; For(i,1,m){ in(x);in(y);in(v); if(v<f[x][y]){ f[x][y]=f[y][x]=v; val[x][y]=val[y][x]=v; } } For(k,1,n){ For(i,1,k-2) For(j,i+1,k-1) ans=min(ans,f[i][j]+val[i][k]+val[k][j]); For(i,1,n) For(j,1,n) f[i][j]=min(f[i][j],f[i][k]+f[k][j]); } if(ans!=inf) o(ans),p('\n'); else puts("It's impossible."); } return 0; }