#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #include<string> #include<vector> #include<queue> using namespace std; const int MAXN=40; int n,m; double Vcur[MAXN],R[MAXN][MAXN]; bool vis[MAXN]; int cntNode[MAXN]; char ss[MAXN][100]; bool SPFA(int st) { queue<int> que; int u,v; for(int i=1;i<=n;i++) Vcur[i]=0,vis[i]=0,cntNode[i]=0; Vcur[st]=1000.0;vis[st]=1;cntNode[st]=1; que.push(st); while(!que.empty()) { u=que.front();que.pop(); vis[u]=0; for(v=1;v<=n;v++) { if(Vcur[u]*R[u][v]>Vcur[v]) { Vcur[v]=Vcur[u]*R[u][v]; if(!vis[v]) { vis[v]=1; ++cntNode[v]; que.push(v); if(cntNode[v]>=n) return true; } } } } return false; } int Find(char s[]) { for(int i=1;i<=n;i++) if(strcmp(s,ss[i])==0) return i; } int main() { int Case=0; while(scanf("%d",&n)&&n) { memset(R,0,sizeof(R)); Case++; for(int i=1;i<=n;i++) scanf("%s",ss[i]); scanf("%d",&m); while(m--) { char s1[100],s2[100]; double rate; scanf("%s",s1); scanf("%lf",&rate); scanf("%s",s2); int u=Find(s1),v=Find(s2); R[u][v]=rate; } int k; for(k=1;k<=n;k++) { if(SPFA(k)) break; } if(k>n) printf("Case %d: No\n",Case); else printf("Case %d: Yes\n",Case); } return 0; }
时间: 2024-10-12 17:27:18