Arbitrage
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 5679 Accepted Submission (s): 2630
problem DescriptionArbitrage 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.
InputThe input file is 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.
OutputFor 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
Test instructions: Arbitragethe use of different forex marketsforex spreads, buying a certain currency on a foreign exchange marketand sell the item on another forex marketto earn a profit.. This profit is called arbitrage. For example, 1 dollars can buy 0.5 pounds, and 1 pounds can buy 10 francs, 1 francs can buy 0.21 usYuanThere is a arbitrage that can be used to buy $1.05 for $1 through arbitrage. The types and names of each currency are given below, and some currencies are givenexchange rate, can I ask if there is arbitrage? Solution thinking: can be regarded as the shortest-circuit problem, but the weight of the calculation is multiplied rather than added. The multiplication of weights with less than 1 leads to a decrease in weight, that is, there is a negative weight problem. Therefore, this problem can not be used Dijkstra algorithm, the problem may be used Floyd algorithm. The code is as follows:
<span style= "FONT-SIZE:12PX;" > #include <cstdio> #include <cstring> #define MAXN 32double map[maxn][maxn];int n,t;void Floyd () {int i,j, K;for (k=0;k<n;++k) {for (i=0;i<n;++i) {for (j=0;j<n;++j) {if (Map[i][j]<map[i][k]*map[k][j]) map[i][ J]=MAP[I][K]*MAP[K][J];}}} int Sign=0;for (i=0;i<n;++i) {if (map[i][i]>1)//its own exchange rate is 1, if the exchange rate is greater than 1 indicates can arbitrage {sign=1;break;} } if (sign) printf ("yes\n"), Else printf ("no\n");} int main () {int m,i,j,a,b,t=1;; Double C;char Str[32][32],s1[32],s2[32];while (scanf ("%d", &n) &&n) {for (i=0;i<n;++i) {for (j=0;j<n; ++J) map[i][j]=0;} for (I=0;i<n;++i) scanf ("%s", Str[i]), scanf ("%d", &m), while (m--) {scanf ("%s%lf%s", s1,&c,s2); for (i=0;i <n;++i) {if (strcmp (S1,str[i]) ==0) a=i;if (strcmp (S2,str[i]) ==0) b=i;} Map[a][b]=c;} printf ("Case%d:", t++); Floyd ();} return 0;} </span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdoj 1217 Arbitrage (Quasi shortest path, Floyd algorithm)