题目:http://acm.hdu.edu.cn/showproblem.php?pid=1217
/************************************************************************/
/*
hdu Arbitrage
floyd求最短路径
题目大意:floyd算法,求得两点之间最短距离,(u,v) = min( (u,v),(u,w)+(w,v) );
此题,是求其能够赚钱,即最大生成树,且过程是相乘的,公式:( u, v) = max( (u,v), (u,w)*(w,v) );
*/
/************************************************************************/ #include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
#include <cmath> using namespace std; #define MIN(a,b) a<b?a:b
#define MAX 0xfffffff const int N = ;
double maps[N][N];
int num,key;
map<string,int> hash_map; void build_map()
{
char name[],name1[];
double rate;
int m;
for (int i = ; i <= num; i++)
{
scanf("%s",name);
hash_map[name] = i;
}
memset(maps,,sizeof(maps));
scanf("%d",&m);
for (int i = ; i < m; i++)
{
scanf("%s%lf%s",name,&rate,name1);
maps[hash_map[name]][hash_map[name1]] = rate;
} } void fload()
{
for (int k = ; k <= num; k++)
{
for (int i = ; i <= num; i++)
for (int j = ; j <= num; j++)
maps[i][j] = max(maps[i][j],maps[i][k] * maps[k][j]);
}
int j;
for (j = ; j <= num; j++)
if (maps[j][j] > )break;
if (j>num)printf("Case %d: No\n",key);
else printf("Case %d: Yes\n",key);
hash_map.clear();
} int main()
{
key = ;
while(scanf("%d",&num) && num != )
{
key++;
build_map();
fload();
}
return ;
}