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