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”.

Sample Input

3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

0

题意:对于某种货币,能不能有正环?(任意一种即可)
题解:因为是多源,所以用floyd,将d[i][i]=1,其余赋为0,最后看d[i][i]是否>1即可。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include  <cassert>
#include   <cstdio>
#include   <vector>
#include   <string>
#include    <cmath>
#include    <queue>
#include    <stack>
#include      <set>
#include      <map>
using namespace std;
#define P(a,b,c) make_pair(a,make_pair(b,c))
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef pair<string,pair<int,int> > pii;
typedef long long ll;
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
const int maxn = 1e2+10;

double d[maxn][maxn];
int n,m;
string u,v;
double exc;
map<string , int>ma;
void floyd()//节点从1~n编号
{
    int i,j,k;
    for(k=1;k<=n;k++)
       for(i=1;i<=n;i++)
         for(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(){
	//ios::sync_with_stdio( false );
	int cnt = 0;
	while(~scanf("%d", &n)&&n){
		memset(d,0,sizeof(d));
		
		rep(i,1,n){
			d[i][i] = 1.0;
			cin>>u;
			ma[u] = i;
		}
		scanf("%d",&m);
		while(m--){
			cin>>u>>exc>>v;
			d[ma[u]][ma[v]] = exc;
		}
		floyd();
		int flag = 0;
		rep(i,1,n){
			if(d[i][i]>1){
				flag = 1;break;
			}
		}
		if(flag)printf("Case %d: Yes\n", ++cnt);
		else printf("Case %d: No\n", ++cnt);
	}
	
	return 0;
} 

发表评论

邮箱地址不会被公开。 必填项已用*标注