题解:按照第一个样例可得,d[1][2] = 0.5; d[2][1] = 10*0.21; d[2][3] = 10; d[3][2] = 0.21*5; d[1][3] = 0.5*10; d[3][1] = 0.21;
由题意可得 d[x][y] > 1/d[y][x] 则可套利。 即d[x][y] * d[y][x] > 1 ----> d[x][x] > 1,用floyd算法松弛之后,遍历对角线即可
AC代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<map>
const int MAX_V = 35;
using namespace std;
typedef pair<string, int> pa;
map<string, int> mp;
int n,m;
double d[MAX_V][MAX_V];
void init()
{
mp.clear();
memset(d, 0, sizeof(d));
}
void Floyd()
{
for(int k = 1; k <= n; k++)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(d[i][j] < d[i][k]*d[k][j])
d[i][j] = d[i][k]*d[k][j];
}
}
}
}
int main()
{
int flag,num = 0;
while(cin>>n && n)
{
flag = 0;
num++;
init();
pa p;
for(int i = 1; i <= n; i++)
{
cin>>p.first;
p.second = i;
mp.insert(p);
}
cin>>m;
string from,to;
double w;
for(int i = 0; i < m; i++)
{
cin>>from>>w>>to;
d[mp[from]][mp[to]] = w;
}
Floyd();
for(int i = 1; i <= n; i++)
{
if(d[i][i] > 1)
flag = 1;
}
if(flag)
cout<<"Case "<<num<<": Yes"<<endl;
else
cout<<"Case "<<num<<": No"<<endl;
}
return 0;
}