又是各种钱币换来换去,问能不能赚钱。
Floyd算法判环。
//Memory Time
//228K 32MS
#include<iostream>
#include<stdio.h>
using namespace std;
char dollarName[30][30];
float edge[30][30];
int outMatch(char src[30], char dst[][30], int dollarType);
bool match(char src[], char dst[], int length);
void floyd(float edge[][30], int dollarType);
int main()
{
int dollarType;
int caseNum = 0;
while(cin >> dollarType && dollarType != 0)
{
caseNum++;
for(int i = 0; i < dollarType; i++)
{
for(int j = 0; j < dollarType; j++)
edge[i][j] = 0;
edge[i][i] = 1;
}
for(int i = 0; i < dollarType ; i++)
{
scanf("%s", dollarName[i]);
}
int M;
scanf("%d", &M);
for(int i = 0; i < M; i++)
{
char tempName[30];
scanf("%s",tempName);
int src = outMatch(tempName, dollarName, dollarType);
float cost; scanf("%f", &cost);
scanf("%s",tempName);
int dst = outMatch(tempName, dollarName, dollarType);
edge[src][dst] = cost;
}
floyd(edge,dollarType);
bool flag = 0;
for(int i = 0; i < dollarType; i++)
{
if(edge[i][i] > 1)
{
flag = 1;
break;
}
}
cout << "Case " << caseNum << ": ";
if(flag)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
int outMatch(char src[30], char dst[][30], int dollarType)
{
int tempSrc;
for(tempSrc = 0; tempSrc < dollarType; tempSrc++)
{
int temp1 = strlen(src);
int temp2 = strlen(dst[tempSrc]);
if(temp1 == temp2 && match(src, dst[tempSrc], temp1))
{
return tempSrc;
}
}
return tempSrc;
}
bool match(char src[], char dst[], int length)
{
bool flag = 1;
for(int j = 0; j < length; j++)
{
if(src[j] != dst[j])
{
flag = 0;
return flag;
}
}
return flag;
}
void floyd(float edge[][30], int dollarType)
{
for(int i = 0; i < dollarType; i++)
for(int j = 0; j < dollarType; j++)
for(int k = 0; k < dollarType; k++)
{
if(edge[j][i]*edge[i][k] > edge[j][k])
edge[j][k] = edge[j][i]*edge[i][k];
}
return;
}