Arbitrage
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5610 Accepted Submission(s): 2610
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.
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.
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
Source
Recommend
Eddy
RE: 感觉题目比较抽象, 开始以为能用并查集, 想了想行不通 . THEN → → 然后就懂了。
套汇 是利润比相乘, 可以看做存在负权, Dij 不可用。 本题数据不是很多, 比较适合Floyd. Spfa也可以;
SPFA:
1 #include <map> 2 #include <queue> 3 #include <cmath> 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 using namespace std; 8 const int INF = 0x3f3f3f3f; 9 double ap[33][33], dis[33]; int vis[33]; 10 map<string, int> mmap; 11 int n; 12 bool Spfa(int src) 13 { 14 memset(vis, 0, sizeof(vis)); 15 for(int i = 1; i <= n; i++) 16 dis[i] = 0.0; 17 dis[src] = 1.0; vis[src] = 1; //****************// 18 queue<int> q; 19 q.push(src); 20 while(!q.empty()) 21 { 22 int temp = q.front(); 23 q.pop(); 24 vis[temp] = 0; 25 for(int j = 1; j <= n; j++) 26 { 27 if(dis[j] < dis[temp] * ap[temp][j]) 28 { 29 dis[j] = dis[temp] * ap[temp][j]; 30 if(!vis[j]) 31 q.push(j); 32 vis[j] = 1; 33 if(dis[src] > 1.0) 34 return true; 35 } 36 } 37 } 38 return false; 39 } 40 int main() 41 { 42 int Q = 1; 43 while(~scanf("%d", &n), n) 44 { 45 memset(ap, 0, sizeof(ap)); 46 mmap.clear(); string mon; 47 for(int i = 1; i <= n; i++) //有向图边权关系存储, 按照无向图来存也可。 48 { 49 cin >> mon; 50 mmap[mon] = i; 51 } 52 int m; 53 scanf("%d", &m); 54 string a, b; double oo; 55 for(int i = 1; i <= m; i++) 56 { 57 cin >> a >> oo >>b; 58 ap[mmap[a]][mmap[b]]=oo; 59 } 60 if(Spfa(1)) 61 printf("Case %d: Yes ", Q++); 62 else 63 printf("Case %d: No ", Q++); 64 } 65 return 0; 66 }
Fyold:
1 #include <map> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 const int INF = 0x3f3f3f3f; 7 map<string, int> mmap; 8 double ap[33][33]; 9 int n; 10 bool Floyd() 11 { 12 for(int k = 1; k <= n; k++) 13 for(int i = 1; i <= n; i++) 14 for(int j = 1; j <= n; j++) 15 if(ap[i][j] < ap[i][k] * ap[k][j]){ 16 ap[i][j] = ap[i][k] * ap[k][j]; 17 // printf("%.4lf ", ap[i][j]); 18 } 19 for(int i = 1; i <= n; i++) //*******************// 20 if(ap[i][i] > 1) 21 return true; 22 return false; 23 } 24 int main() 25 { 26 int Q = 1; 27 while(~scanf("%d", &n), n) 28 { 29 mmap.clear(); 30 for(int i = 1; i <= n; i++) 31 for(int j = 1; j <= n; j++) 32 ap[i][j]=(i==j?0:INF); 33 memset(ap, 0, sizeof(ap)); 34 string mon; int k = 1; 35 for(int i = 0; i < n; i++){ 36 cin >> mon; 37 mmap[mon] = k++; 38 } 39 int m; 40 scanf("%d", &m); 41 for(int i = 0; i < m; i++){ 42 string a, b; double oo; 43 cin >>a >> oo >> b; 44 ap[mmap[a]][mmap[b]]=oo; 45 if(ap[mmap[a]][mmap[b]] > oo) 46 ap[mmap[a]][mmap[b]]=ap[mmap[b]][mmap[a]]=oo; 47 } 48 if(Floyd()) 49 printf("Case %d: Yes ", Q++); 50 else 51 printf("Case %d: No ", Q++); 52 } 53 return 0; 54 }