其实就是找到乘积最大的一条回路,如果大于1就是YES否则就是NO,用bellman N4都能过……
/* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #include <map> #define INF 1E9 using namespace std; map<string,int> hash; double dis[31][31]; double d[31]; int n,m; bool input() { hash.clear(); scanf("%d",&n); if(!n)return 0; int i; string a,b; double t; for(i=0;i<n;i++) { cin>>a; hash[a]=i; } scanf("%d",&m); for(i=0;i<m;i++) { cin>>a>>t>>b; dis[hash[a]][hash[b]]=t; } return 1; } bool bellman(int V0) { memset(d,0,sizeof(d)); d[V0]=1; int v,u,i; double t; for(i=0;i<n;i++) for(v=0;v<n;v++) for(u=0;u<n;u++) if(d[v]<(t=d[u]*dis[u][v])) d[v]=t; if(d[V0]>1)return 1; return 0; } bool solve() { int i,v,u; for(i=0;i<n;i++) if(bellman(i))return 1; return 0; } int main() { int T=0; while(input()) printf("Case %d: %s\n",++T,solve()?"Yes":"No"); }