Poj2240 -- arbitrage (Floyd algorithm deformation)

Source: Internet
Author: User

Arbitrage

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.
Input
The 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.
Output
For each test case, print one line telling whether Arbitrage is possible or not in the format "case: 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

Question:

There are n currencies, which give the exchange rates of some of these currencies. Eg :( A 2 B) indicates that a can replace two blocks of B, but does not mean that two blocks of B can replace a (directed !)

Determine whether money can be made through currency conversion. In example, one usdollar unit can be converted into 1.05 usdollar units, making money!

Ideas:

Floyd algorithm deformation,Edge [I] [J] = max (edge [I] [J], edge [I] [m] * edge [m] [J])

Edge [I] [I] = 1 during initialization. After Floyd, determine whether edge [I] [I] can earn money by determining whether edge [I] [I] is greater than 1!

Code:

1 # include <string> 2 # include <iostream> 3 # include <stdio. h> 4 # include <cstring> 5 # define maxn 100 6 using namespace STD; 7 double edge [maxn + 10] [maxn + 10]; 8 string name [maxn + 10]; 9 int N; 10 void Floyd () 11 {12 for (int m = 1; m <= N; m ++) 13 For (INT I = 1; I <= N; I ++) 14 for (Int J = 1; j <= N; j ++) 15 if (edge [I] [m]! = Int_max & edge [m] [J]! = Int_max & // Floyd deformation formula 16 edge [I] [J] <edge [I] [m] * edge [m] [J]) 17 edge [I] [J] = edge [I] [m] * edge [m] [J]; 18} 19 int main () 20 {21 int m, times = 0; 22 while (CIN> N) 23 {24 times ++; 25 for (INT I = 1; I <= N; I ++) 26 For (Int J = 1; j <= N; j ++) 27 if (I! = J) edge [I] [J] = int_min; 28 else edge [I] [J] = 1; 29 If (n = 0) break; 30 For (INT I = 1; I <= N; I ++) 31 CIN> name [I]; 32 CIN> m; 33 for (INT I = 1; I <= m; I ++) 34 {35 string tmp2, tmp1; 36 double DIS; 37 CIN> tmp1> dis> tmp2; 38 int x1, x2; 39 For (x1 = 1; X1 <= N; X1 ++) // converts a string to a 40 if (name [X1] = tmp1) break; 41 for (x2 = 1; X2 <= N; x2 ++) 42 if (name [X2] = tmp2) break; 43 edge [X1] [X2] = DIS; 44} 45 Floyd (); 46 int CNT = 0; 47 for (INT I = 1; I <= N; I ++) 48 if (edge [I] [I]> 1) // determine whether the conversion can earn 49 CNT ++; 50 if (CNT> = 1) printf ("case % d: Yes \ n", times); 51 else printf ("case % d: NO \ n", times); 52} 53 return 0; 54}

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.