Poj2240 arbitrage Bellman-Ford

Source: Internet
Author: User

The deformation of the shortest path is equivalent to finding the maximum path. Using Bellman-Ford

Arbitrage

Time limit:1000 ms

 

Memory limit:65536 K

Total submissions:10848

 

Accepted:4574

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

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
# Include <iostream> # include <map> # include <vector> # include <string> # include <cmath> using namespace STD; struct line {int S, E; double rate ;};# define EPS 1e-8 # define maxn 50 line L [maxn * maxn]; Map <string, int> hash; int n, m; double dis [maxn]; int sig (Double K) {If (FABS (k) <EPS) return 0; return K> 0? 1:-1;} double bellman_ford (INT s) {for (INT I = 1; I <= N; I ++) {dis [I] = 0 ;} dis [s] = 1; // normal Bellman-Ford here the nth loop should be executed separately, however, the point in the question will be self-defeating, that is, the value of the exchange edge between A and A is actually greater than 1, so it is executed once more to prevent for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= m; j ++) {If (DIS [L [J]. s]! = 0 & L [J]. rate * Dis [L [J]. s]> dis [L [J]. e]) dis [L [J]. e] = L [J]. rate * Dis [L [J]. s] ;}} return dis [s];} void Init () {string S, E; double rate; For (INT I = 1; I <= N; I ++) {CIN> S; hash [s] = I;} CIN> m; For (INT I = 1; I <= m; I ++) {CIN> S> rate> E; L [I]. S = hash [s]; L [I]. E = hash [E]; L [I]. rate = Rate ;}} void solve () {for (INT I = 1; I <= N; I ++) {If (SIG (bellman_ford (I)-1)> 0) {cout <"yes" <Endl; return ;}} cout <"no" <Endl ;}int main () {INT cases = 1; while (CIN> N & N) {Init (); cout <"case" <cases ++ <":"; solve ();} 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.