Description
Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent.
Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.
Input
The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible.
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line telling whether arbitrage is possible or not in the format “Case case: Yes” respectively “Case case: No”.
Analysis
看起来很diāo但是很水的floyd
如果能够通过一条路径使得map[i][i]的权值
>1
那么就是Yes
注意map[i][i]=1
Code
/*
ID:wjp13241
PROG:arbitrage
LANG:C++
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#define fo(i,a,b) for (int i=a;i<=b;i++)
#define fil(x,t) memset(x,t,sizeof(x))
#define STP system("pause")
#define min(x,y) x<y?x:y
#define max(x,y) x>y?x:y
#define ll long long
#define INF 0x7f7f7f7f
#define EPS 1e-4
#define N 31
#define E N*N+1
using namespace std;
double map[N][N];
string s[N];
int num[N];
int main()
{
ios::sync_with_stdio(false);
int n,Case=0;
while (cin>>n&&n&&++Case)
{
fil(map,0);
fo(i,1,n)
{
cin>>s[i];
num[i]=i;
map[i][i]=1;
}
int m;
cin>>m;
fo(i,1,m)
{
string a,b;
double w;
cin>>a>>w>>b;
int x,y;
fo(j,1,n)
{
if (s[j]==a)
x=j;
if (s[j]==b)
y=j;
}
map[x][y]=w;
}
fo(k,1,n)
fo(i,1,n)
fo(j,1,n)
if (map[i][k]*map[k][j]>map[i][j])
map[i][j]=map[i][k]*map[k][j];
bool flag=false;
fo(i,1,n)
if (map[i][i]-1>EPS)
{
flag=true;
break;
}
if (flag)
cout<<"Case "<<Case<<": Yes"<<endl;
else
cout<<"Case "<<Case<<": No"<<endl;
}
return 0;
}