天天看點

Arbitrage(floyd)

F - Arbitrage Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  Status  Practice  POJ 2240 Appoint description:  System Crawler  (2015-10-04)

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

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
      

Sample Output

Case 1: Yes
Case 2: No      
這題第二次做了,但是還是有些細節方面沒注意,Wa了幾次。這題用floyd就能a了。隻要floyd一次後,看看對角線是否存在大于1      
的數就OK了。(代碼有點挫,王桂平的圖論書有這題,寫的很好)      
AC代碼:      
<pre name="code" class="cpp">#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
#define T 105
#define inf 0x3f3f3f3f
#define CRL(a) memset(a,0,sizeof(a))
typedef long long ll;
map<string,int> a;
int n;
double t[T][T];
int main()
{
	/*freopen("input.txt","r",stdin);*/
	int m,i,j,k,c,flag,cnt=0,cc;
	char s[500];
	string ss[3];
	while(cin >> n&&n)
	{
		c=0;flag=1;
		CRL(t);
		for(i=0;i<n;++i){
			scanf("%s",&s);
			a[s]=c++;
		}
       scanf("%d",&m);
	   cin.get();
		for(i=0;i<m;++i){
			gets(s);
			for(j=0,cc=0;s[j];++j){
				if(s[j]!=' '){
					ss[cc]+=s[j];
				}
				else{
					cc++;
				}
			}
			for(j=0;j<ss[1].size();++j)
				s[j]=ss[1][j];
			s[j]='\0';
			t[a[ss[0]]][a[ss[2]]]=atof(s);
			ss[0].clear(),ss[1].clear(),ss[2].clear();
		}
		for(i=0;i<c&&flag;++i)
			for(j=0;j<c&&flag;++j)
				for(k=0;k<c&&flag;++k){
					if(t[j][k]<t[j][i]*t[i][k])
                                      //這裡原來寫的是後面的兩個t都不為0就進行,結果Wa了
					t[j][k]=t[j][i]*t[i][k];
					if(j==k&&t[j][k]>1)
						flag=0;
				}
				if(!flag)
					cout << "Case "<< ++cnt << ": Yes\n";
				else
					cout << "Case "<< ++cnt << ": No\n";
				a.clear();
	}
	return 0;
}
           

繼續閱讀