Original title link
floyd deformation, there's really nothing
to lose, it costs too much food or gas waited a long time. .
Code
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
double map[50][50];
int n,inf=9999999,flag;
void floyd()
{
int i,j,k;
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];
}
}
}
for(i=1;i<=n;i++)
{
if(map[i][i]>1)
{
flag=1;
break;
}
}
}
int main()
{
int i,j,q=1,m;
double cos;
char s[30][30],s1[30],s2[30];
while(~scanf("%d",&n)&&n)
{
getchar();
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
scanf("%s",s[i]);
/* for(i=1;i<=n;i++)
printf("%s\n",s[i]);*/
scanf("%d",&m);
while(m--)
{
getchar();
scanf("%s %lf %s",s1,&cos,s2);
for(i=1;i<=n;i++)
{
if(strcmp(s[i],s1)==0)
break;
}
for(j=1;j<=n;j++)
{
if(strcmp(s[j],s2)==0)
break;
}
map[i][j]=cos;//printf("%d %lf %d\n",i,cos,j);
}
/* for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%f ",map[i][j]);
}printf("\n");
}*/
flag=0;
floyd();
printf("Case %d: ",q++);
if(flag) printf("Yes\n");
else printf("No\n");
}
return 0;
}