Time limit:1000 MSMemory limit:65536 KB
64-bit integer Io format:% I64d, % i64uJava class name:Main
[Submit] [Status] [discuss]
Descriptionarbitrage 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. inputthe input will contain in 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. outputfor each test case, print one line telling whether Arbitrage is possible or not in the format "case: yes" respectively "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
Whether the loop exchange rate of each vertex is greater than 1
# Include <iostream> # include <string. h> # include <stdio. h> using namespace STD; # define Maxx 35 # define maxn 1000 char name [Maxx] [20], a [20], B [20]; /// the chain table stores double maxdis [maxn]; // similar to the DIS [] array, but this time the maximum loop Double X is obtained; // The exchange rate int N, T; int falg; struct exchange {int CI, CJ; double CIJ;} ex [maxn]; void Bellman (INT V0) {falg = 0; memset (maxdis, 0, sizeof (maxdis )); maxdis [V0] = 1; for (int K = 1; k <= N; k ++) // to find the loop Recursive maxdis [1] From maxdis [0]... maxdis [N] {for (INT I = 0; I <t; I ++) /// Add the value of each edge to change the ambassador's maximum {If (maxdis [ex [I]. CI] * Ex [I]. CIJ> maxdis [ex [I]. CJ]) // the link between the processes ~~~ {Maxdis [ex [I]. CJ] = maxdis [ex [I]. CI] * Ex [I]. CIJ; // if the maximum value is found, update it.} If (maxdis [V0]> 1) falg = 1;} int main () {int num; int casee = 0; while (scanf ("% d", & N), n) {for (INT I = 0; I <n; I ++) {CIN> name [I]; // scanf ("% s", name [num]);} int I, j, k; scanf ("% d ", & T); for (I = 0; I <t; I ++) {CIN> A> x> B; // scanf ("% S % if % s", A, & X, B); For (j = 0; strcmp (A, name [J]); j ++); //, meaning that there is no loop statement in the empty loop. For (k = 0; strcmp (B, name [k]); k ++ ); /// convert the string The number ing ex [I]. ci = J; Ex [I]. CIJ = x; Ex [I]. cj = K; // cout <I <''<j <'' <k <"!!!!!!! "<Endl; an amazing output} For (INT I = 0; I <n; I ++) {Bellman (I ); /// traverse the loop of each vertex if (falg) /// if the exchange rate is greater than 1, it is good to exit directly break;} If (falg) printf ("case % d: yes \ n ", ++ casee); else printf (" case % d: NO \ n ", ++ casee);} return 0 ;}
Poj 2240 arbitrage