链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217
Arbitrage
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4430 Accepted Submission(s): 2013
Problem 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 file 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
----------------------------------------------------------------------------
================================================
题意自己看吧,Floyd的变形,求出每个点到自己的最大权值,并且用乘法不是加法
用了map容器便于操作
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <algorithm> 6 #include <map> 7 8 #define MAXX 35 9 #define INF 1000000000 10 double d[MAXX][MAXX]; 11 using namespace std; 12 13 void Flody(int n) 14 { 15 int i,j,k; 16 for(k=0; k<n; k++) 17 for(i=0; i<n; i++) 18 for(j=0; j<n; j++) 19 if(d[i][k] * d[k][j] > d[i][j]) 20 d[i][j] = d[i][k] * d[k][j]; 21 } 22 23 int main() 24 { 25 int n,i,j,t,tmp=0; 26 while(scanf("%d",&n)!=EOF&&n) 27 { 28 char str[105],str1[105],str2[105]; 29 int cas=0; 30 double rate; 31 map<string,int> m; 32 map<string,int>::iterator it; 33 for(i=0; i<n; i++) 34 for(j=0; j<n; j++) 35 d[i][j] = 0; 36 for(i=0; i<n; i++) 37 { 38 scanf("%s",str); 39 m[str]=cas++; 40 } 41 scanf("%d",&t); 42 for(i=0; i<t; i++) 43 { 44 scanf("%s%lf%s",str1,&rate,str2); 45 d[m[str1]][m[str2]]=rate; 46 } 47 Flody(n); 48 bool flag=false; 49 for(i=0; i<n; i++) 50 { 51 if(d[i][i]>1.0) 52 { 53 flag=true; 54 break; 55 } 56 } 57 if(flag) 58 { 59 printf("Case %d: Yes\n",++tmp); 60 } 61 else 62 { 63 printf("Case %d: No\n",++tmp); 64 } 65 } 66 return 0; 67 }
hdu 1217 (Floyd变形)