HDU 1217 arbitrage

Source: Internet
Author: User

Problem description

Arbitrage is the use ofdiscrepancies in currency exchange rates to transform one unit of a currencyinto more than one unit of the same currency. For example, suppose that 1 usdollar 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 clevertrader 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 asinput and then determines whether Arbitrage is possible or not.

 

Input

The input file will containone or more test cases. om the first line of each test case there is an integern (1 <= n <= 30), representing the number of different currencies. the next nlines each contain the name of one
Currency. within a name no spaces willappear. the next line contains one integer m, representing the length of thetable to follow. the last M lines each contain the name Ci of A sourcecurrency, a real number rij which represents the exchange rate from CI
Cjand a name CJ of the destination currency. exchanges which do not appear in thetable are impossible.
Test Cases are separated from each other by a blank line. input is terminatedby a value of zero (0) for N.

 

Output

For each test case, print oneline telling whether Arbitrage is possible or not in the format "casecase: yes" respectively "case: No ".

 

Sample Input

3

Usdollar

Britishpound

Frenchfranc

3

Usdollar 0.5 briishpound

Britishpound 10.0 frenchfranc

Frenchfrankc 0.21 usdollar

 

3

Usdollar

Britishpound

Frenchfranc

6

Usdollar 0.5 briishpound

Usdollar 4.9 frenchfranc

Britishpound 10.0 frenchfranc

Britishpound 1.99 usdollar

Frenchfranc 0.09 britishpound

Frenchfrankc 0.19 usdollar

 

0

 

Sample output

Case 1: Yes

Case 2: No

 

 

Introduction: Give N types of money and enter the names of N types of currencies. Exchange M types. Then enter the exchange rate between the two currencies in the m row.

Method: the shortest path.

# Include <stdio. h> # include <stdlib. h> # include <string. h> int main () {int n, m, I, J, K, R, S, T, Count = 1, flag = 0; double D [31] [31] = {0}, C; char a [31] [100] = {null}, P [100] = {null }, Q [100] = {null}; while (scanf ("% d", & N )! = EOF, N)/* Total number of currencies */{memset (D, 0, sizeof (d);/* initialize the exchange rate between currencies */for (I = 1; I <= N; I ++) {scanf ("% s", & A [I]);}/* record currency name */scanf ("% d ", & M);/* Total Number of currency exchanges */for (I = 1; I <= m; I ++) {scanf ("% s", & P ); /* currency 1 */T = strlen (p); For (j = 1; j <= N; j ++) {If (strcmp (A [J], p) = 0) {r = J; break ;}/ * replace the currency name with a number */For (j = 0; j <t; j ++) {P [J] = NULL;}/* initialize P to facilitate next use of */scanf ("% lf", & C ); /* currency 1-to-currency 2 exchange rate */scanf ("% s", & Q);/* currency 2 */T = strlen (Q ); for (j = 1; j <= N; j ++) {If (strcmp (A [J], q) = 0) {S = J; break ;}} /* replace the currency name with a number */For (j = 0; j <t; j ++) {q [J] = NULL;}/* initialize Q, easy to use next time */d [r] [s] = C;/* record the exchange rate of currency 1 against currency 2 */}/* Floyd */For (k = 1; k <= N; k ++) {for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {If (d [I] [J] <D [I] [k] * d [k] [J]) {d [I] [J] = d [I] [k] * d [k] [J] ;}}/ * Judge */flag = 0; for (I = 1; I <= N; I ++) {If (d [I] [I]> 1) /* determine if there is any arbitrage */{flag = 1; break;} printf ("case % d:", Count ++ ); if (flag = 0) printf ("NO \ n"); elseprintf ("Yes \ n"); getchar (); // process the empty rows in each group after the input} system ("pause"); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.