Arbitrage(bellman_ford)

Arbitrage

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16652   Accepted: 7004

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 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 case: Yes" respectively "Case case: No".

Sample Input

3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

0

Sample Output

Case 1: Yes
Case 2: No

Source

Ulm Local 1996

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 using namespace std;
 5 const int M = 40 , inf = 0x3f3f3f3f;
 6 struct Arbitrage
 7 {
 8     int u , v ;
 9     double r ;
10 }e[M * M];
11 int n , m ;
12 char cur[M][20] ;
13 char a[20] , b[20] ;
14 double d[M] ;
15
16 void init (char a[20] , char b[20] , int no)
17 {
18     for (int i = 1 ; i <= n ; i++) {
19         if (strcmp (cur[i] , a) == 0)
20             e[no].u = i ;
21         if (strcmp (cur[i] , b) == 0)
22             e[no].v = i ;
23     }
24 }
25
26 int bellman_ford (int o)
27 {
28     for (int i = 0 ; i <= n ; i++)
29         d[i] = 0 ;
30     d[o] = 1.0 ;
31     double temp = 1.0 ;
32     bool flag ;
33     for (int i = 1 ; i <= n ; i++) {
34         flag = 1 ;
35         for (int j = 0 ; j < m ; j++) {
36             if (d[e[j].v] < d[e[j].u] * e[j].r) {
37                 d[e[j].v] = d[e[j].u] * e[j].r ;
38                 flag = 0 ;
39             }
40             if (d[o] > temp) {
41                  return true ;
42             }
43         }
44     }
45     return false ;
46 }
47
48 int main ()
49 {
50     //freopen ("a.txt" , "r" , stdin) ;
51     int ans = 1 ;
52     while (~ scanf ("%d" , &n)) {
53         if (n == 0)
54             break ;
55         getchar () ;
56         for (int i = 1 ; i <= n ; i++) {
57             gets (cur[i]) ;
58         }
59         scanf ("%d" , &m) ;
60         for (int i = 0 ; i < m ; i++) {
61             cin >> a >> e[i].r >> b ;
62             init (a , b , i) ;
63         }
64       /*  for (int i = 0 ; i < m ; i++) {
65             printf ("u = %d , v = %d , r = %.2f\n" , e[i].u , e[i].v , e[i].r) ;
66         }*/
67         int i ;
68         for (i = 1 ; i <= n ; i++) {
69             if (bellman_ford (i)) {
70                 printf ("Case %d: Yes\n" , ans++) ;
71              //   printf ("Arbitrage num : %d\n" , i) ;
72                 break ;
73             }
74             /*printf ("%d team: \n" , i) ;
75             for (int j = 1 ; j <= n ; j++)
76                 printf ("%.2f " ,d[j]) ;
77             puts ("") ; */
78         }
79         if (i == n + 1)
80             printf ("Case %d: No\n" , ans++) ;
81     }
82     return 0 ;
83 }

时间: 2024-08-10 04:23:30

Arbitrage(bellman_ford)的相关文章

[ACM] hdu 1217 Arbitrage (bellman_ford最短路,判断是否有正权回路或Floyed)

Arbitrage Problem 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 p

[ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)

Arbitrage Problem 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 p

POJ 2240 Arbitrage Bellman_ford 判读是否存在正环

和POJ1860差不多,就是用bellmanford判读是否存在正环,注意的是同种货币之间也可以交换,就是说:A货币换A货币汇率是2的情况也是存在的. #include<stdio.h> #include<string.h> #include<cstring> #include<string> #include<math.h> #include<queue> #include<algorithm> #include<

Arbitrage - poj 2240 (Bellman-ford)

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17374   Accepted: 7312 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 exa

hdu 1217 Arbitrage 两种算法AC代码,Floyd+Bellman-Ford 大水题一枚 注意是有向图~~

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4998    Accepted Submission(s): 2286 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform

poj2240(Arbitrage)

题目大意: 给你各国的货币名称,和国家与国家之间兑换的汇率,问你通过一系列的兑换能否是自己的财富增加. 解题思路: 先建图,因为给的是字符串不是阿拉伯数之间的联系,所以建图可以用map<string,int>mp 建图.赋值给结构体中的u,v.然后利用bellman—ford算法,因为如果财富能够通过兑换增值说明会有一条正权回路.所以利用bellman—ford算法 判断是否出现正权回路说明可以增值否则NO. 代码: #include <algorithm> #include &l

POJ 2240 Bellman_Ford

Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16429 Accepted: 6909 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. F

kuangbin专题四 : 最短路 I 题 Arbitrage

kuangbin专题四 : 最短路 I 题  Arbitrage POJ 2240 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,

poj2240——Arbitrage(Bellman-Ford算法)

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 Fre