原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217
分析:floyd.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<string> 7 #include<queue> 8 #include<vector> 9 #include<map> 10 #define ll long long 11 #define maxn 33 12 #define inf 0xfffffff 13 using namespace std; 14 int n;char cur[30]; 15 map<string,int>m; 16 double mapp[maxn][maxn]; 17 void floyd() 18 { 19 for(int k=1;k<=n;k++) 20 for(int i=1;i<=n;i++) 21 for(int j=1;j<=n;j++) 22 if(mapp[i][j]<mapp[i][k]*mapp[k][j]) 23 mapp[i][j]=mapp[i][k]*mapp[k][j]; 24 } 25 int main() 26 { 27 int cas=1; 28 while(~scanf("%d",&n),n) 29 { 30 memset(mapp,0,sizeof(mapp)); 31 for(int i=1;i<=n;i++) 32 { 33 scanf("%s",cur); 34 m[cur]=i; 35 } 36 int q; 37 scanf("%d",&q); 38 while(q--) 39 { 40 char s1[30],s2[30];double e; 41 scanf("%s %lf %s",s1,&e,s2); 42 mapp[m[s1]][m[s2]]=e; 43 } 44 floyd(); 45 bool flag=false; 46 for(int i=1;i<=n;i++) 47 if(mapp[i][i]>1.0) 48 { 49 flag=true;break; 50 } 51 printf("Case %d: ",cas++); 52 if(flag)printf("Yes "); 53 else printf("No "); 54 } 55 return 0; 56 }