hdu 1217 Arbitrage
Flody多源最短路
#include<cstdio> #include<cstring> #include<string> #include<cmath> #include<map> #include<algorithm> #include<iostream> using namespace std; const int maxn=35; map<string,int>zh; string s,s1,s2; double jz[maxn][maxn],A[maxn][maxn]; int u,v,n,m; void floyd() { int i,j,k; for(i=0;i<=n;i++) for(j=0;j<=n;j++) A[i][j]=jz[i][j]; for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(k==i||k==j) continue; if(A[i][k]*A[k][j]>A[i][j]) A[i][j]=A[i][k]*A[k][j]; } } } } int main() { int i,j,_=1; double cost; while(~scanf("%d",&n)) { if(!n) break; zh.clear(); for(i=1;i<=n;i++) {cin>>s;zh[s]=i;} scanf("%d",&m); memset(jz,0,sizeof(jz)); for(i=0;i<m;i++) { cin>>s1; scanf("%lf",&cost); cin>>s2; u=zh[s1];v=zh[s2]; jz[u][v]=cost; } int flag=0; floyd(); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(A[i][j]*A[j][i]>1.0) {flag=1;break;} } if(flag) break; } printf("Case %d: ",_++); if(flag) printf("Yes\n"); else printf("No\n"); } return 0; }