hdu1599 find the mincost route
floyd找最小环
很好理解
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<cmath> 8 #include<ctime> 9 #include<queue> 10 #include<stack> 11 #include<map> 12 #include<set> 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--) 14 #define re(i,l,r) for(int i=(l);i<=(r);i++) 15 #define Clear(a,b) memset(a,b,sizeof(a)) 16 #define inout(x) printf("%d",(x)) 17 #define douin(x) scanf("%lf",&x) 18 #define strin(x) scanf("%s",(x)) 19 #define LLin(x) scanf("%lld",&x) 20 #define op operator 21 #define CSC main 22 typedef unsigned long long ULL; 23 typedef const int cint; 24 typedef long long LL; 25 using namespace std; 26 cint inf=214748364; 27 void inin(int &ret) 28 { 29 ret=0;int f=0;char ch=getchar(); 30 while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} 31 while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar(); 32 ret=f?-ret:ret; 33 } 34 int n,m,t,dis[111][111],mp[111][111]; 35 int floyd() 36 { 37 int ret=inf; 38 re(k,1,n) 39 { 40 re(i,1,n)if(i!=k) 41 re(j,1,n)if(j!=k&&j!=i) 42 if(dis[i][j]<inf&&mp[i][k]!=inf&&mp[k][j]!=inf) 43 ret=min(ret,mp[i][k]+mp[k][j]+dis[i][j]); 44 re(i,1,n) 45 re(j,1,n) 46 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 47 } 48 return ret; 49 } 50 int main() 51 { 52 while(scanf("%d%d",&n,&m)!=EOF) 53 { 54 re(i,1,n)re(j,1,m)dis[i][j]=mp[i][j]=inf; 55 re(i,1,m) 56 { 57 int q,w,e;inin(q),inin(w),inin(e); 58 dis[q][w]=dis[w][q]=mp[q][w]=mp[w][q]=min(dis[q][w],e); 59 } 60 int ans=floyd(); 61 if(ans==inf)printf("It's impossible.\n"); 62 else printf("%d\n",ans); 63 } 64 return 0; 65 }