这一题重点是建图,,越来越发现建图是解决问题的核心啊,,,,,,
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <string.h> #include <math.h> int main() { int n,m,x,y,i,j,k; char name[32][100],temp1[100],temp2[100]; double map[32][32]; int count = 0; while( ~scanf("%d",&n)!=EOF && n ) { for(i=1; i<=n; i++) scanf("%s",name[i]); scanf("%d",&m); for(i=1; i<=n; i++) { for(j=1;j<=n; j++) map[i][j] = 0.0; map[i][i] = 1.0; } while(m--) { double price; int x=0,y=0; scanf("%s%lf%s",temp1,&price,temp2); for(i=1;i<=n; i++) { if(x&&y) break; if( strcmp(temp1,name[i]) == 0 ) x = i; if( strcmp(temp2,name[i]) == 0 ) y = i; } map[x][y] = price; } for(k=1; k<=n; k++) for(i=1; i<=n; i++) for(j=1; j<=n; j++) if( map[i][j] < map[i][k] * map[k][j] ) map[i][j] = map[i][k] * map[k][j]; int success = 0; for(k=1; k<=n; k++) if( map[k][k] > 1 ) {success = 1;break; } if( success == 0 ) printf("Case %d: No\n",++count); else printf("Case %d: Yes\n",++count); } return 0; }