E-arbitrageTime
limit:1000MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64 U SubmitStatusPracticePOJ 2240Appoint Description:System Crawler (2015-11-24)
Description
Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one UN It's 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, MA King a profit of 5 percent.
Your job is to write a program this takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.
Input
The input would contain one or more test cases. Om the first line of all test case there was an integer n (1<=n<=30), representing the number of different Currencie S. The next n lines each contain the name of one currency. Within a name no spaces would appear. The next line contains one integer m, representing the length of the table to follow. The last m lines all 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 is impossible.
Test cases is separated from 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 are possible or not in the format "case Case:yes" Respectivel Y "Case Case:no".
Sample Input
3usdollarbritishpoundfrenchfranc3usdollar 0.5 britishpoundbritishpound 10.0 Frenchfrancfrenchfranc 0.21 Usdollar3usdollarbritishpoundfrenchfranc6usdollar 0.5 britishpoundusdollar 4.9 frenchfrancbritishpound 10.0 Frenchfrancbritishpound 1.99 usdollarfrenchfranc 0.09 Britishpoundfrenchfranc 0.19 USDollar0
Sample Output
Case 1:yescase 2:no
Enter the type of currency first, then the exchange relationship of each currency, and ask if there is a currency that can add value, that is, through some kind of exchange relationship is G[i][i] >1
Initialize the idiot to set G to INF, in vain contributed 4 times WA
1#include <iostream>2#include <cstring>3#include <algorithm>4#include <cstdio>5#include <map>6 using namespacestd;7 Const intMAX = -;8 Const intINF =1<< the;9 intN;Ten DoubleG[max][max]; Onemap<string,int>m; A voidFloyd () - { - for(intK =1; K <= N; k++) the { - for(inti =1; I <= N; i++) - { - for(intj =1; J <= N; J + +) + { - if(G[i][k]! =0&& G[k][j]! =0&& G[i][j] < g[i][k]*G[k][j]) + { AG[I][J] = g[i][k]*G[k][j]; at } - } - } - } - } - intMain () in { - intnum =0; to while(SCANF ("%d", &n)! = EOF &&N) + { - intT; the Chartemp[ -],str[ -]; * DoubleRat; $ m.clear ();Panax Notoginseng for(inti =1; I <= N; i++) - { the for(intj =1; J <= N; J + +) +G[I][J] =0; A } the for(inti =1; I <= N; i++) + { -scanf"%s", temp); $M[temp] =i; $ } -scanf"%d", &t); - for(inti =1; I <= t; i++) the { -scanf"%s%lf%s",temp,&rat,str);Wuyig[m[temp] [m[str]] =Rat; the } - Floyd (); Wu intFlag =0; - for(inti =1; I <= N; i++) About { $ if(G[i][i]! =0&& G[i][i] >1.0) - { -Flag =1; - Break; A } + } theprintf"Case %d:",++num); - if(flag) $printf"yes\n"); the Else theprintf"no\n"); the } the return 0; -}
View Code
POJ 2240Arbitrage (Floyd)