poj-------(2240)Arbitrage(最短路)

Arbitrage

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15640   Accepted: 6563

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

感觉自己这种算法还是太弱了......唉! 没有用floy之间用bellman_floy来做的,bellman算法其实总结起来就是三点:

第一: 初始化

第二: 循环优化求解最大或者最小

第三 : 检测是否存在负环

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<string>
 5 #include<iostream>
 6 #include<map>
 7 #include<iterator>
 8 using namespace std;
 9 const double inf =-100;
10 const int maxn = 105;
11 struct node
12 {
13  int u,v;
14  double val;
15 };
16 node edge[1000];
17 double dist[maxn];
18 /*松弛状态判别*/
19 bool relax(int u,int v,double val){
20     if(dist[v]<dist[u]*val){
21         dist[v]=dist[u]*val;
22         return 1;
23     }
24     return 0;
25 }
26 bool Bellman(int st,int n,int m){
27     for(int i=1;i<=n;i++){    //初始化
28         dist[i]=inf;
29     }
30     dist[st]=1;
31     bool flag;
32     /*循环优化部分*/
33     for(int i=1; i<n;i++) {
34           flag=false;
35       for(int j=1;j<=m;j++){
36          if(relax(edge[j].u,edge[j].v,edge[j].val))
37              flag=true;
38          }
39        if(!flag) break;
40     }
41     /*检验部分*/
42     for(int i=1;i<=m;i++){
43        if(relax(edge[i].u,edge[i].v,edge[i].val))
44           return 1;   //有负圈
45     }
46     return 0;
47 }
48
49 int main()
50 {
51     int n,m;
52     map<string ,int> sac;
53     string temp;
54     int test=1;
55     while(scanf("%d",&n)==1&&n!=0){
56             if(!sac.empty())sac.clear();
57         for(int i=1;i<=n;i++){
58            cin>>temp;
59         // sac.insert(pair<string ,int>(temp,i));
60            sac[temp]=i;
61         }
62          cin>>m;
63          double ss;
64          string aa,bb;
65         map<string,int>::iterator p1,p2;
66         for(int i=1 ; i<=m ; i++ ){
67            cin>>aa>>ss>>bb;
68            p1=sac.find(aa);
69            p2=sac.find(bb);
70            edge[i].u=p1->second;
71            edge[i].v=p2->second;
72            edge[i].val=ss;
73         }
74
75         if(Bellman(1,n,m)) printf("Case %d: Yes\n",test);
76            else printf("Case %d: No\n",test);
77         test++;
78         // printf("%lf\n",dist[1]);
79     }
80     return 0;
81 }

poj-------(2240)Arbitrage(最短路),布布扣,bubuko.com

时间: 2024-12-28 23:48:24

poj-------(2240)Arbitrage(最短路)的相关文章

POJ 2240 Arbitrage(最短路 套汇)

题意  给你n种币种之间的汇率关系  判断能否形成套汇现象  即某币种多次换为其它币种再换回来结果比原来多 基础的最短路  只是加号换为了乘号 #include<cstdio> #include<cstring> #include<string> #include<map> using namespace std; map<string, int> na; const int N = 31; double d[N], rate[N][N], r;

poj 2240 Arbitrage 题解

Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 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 currenc

POJ 2240 -- Arbitrage(Bellman-Ford)

POJ 2240 -- Arbitrage(Bellman-Ford) 题意: 已知n种货币,以及m种货币汇率及方式,问能否通过货币转换,使得财富增加. Bellman-ford 算法: 一个具有n个顶点的图如果不存在环,则从顶点x,到顶点y,最多经过n-1条边(要考虑连通性,每个顶点最多经过 1 次),因此 x 到 y 的最短路 最多经过 n - 1 次松弛操作(就是更新长度)就应该出现,如果第 n 次松弛还可以得到最优,那么这个图就肯定是存在环了(直接用Dijkstra 就无法得到最优的,环

POJ 2240 Arbitrage

Bellman 求最大环. 询问货币是否纯在套汇. 假如给你 1 元,通过兑换之后 超过 1 元就是存在套汇了. 用 map 映射比较方便. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<queue> #include<map> #include<iostream>

poj 2240 Arbitrage (Floyd)

链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 BritishPound. 问在这N种货币中是否存在货币经过若干次兑换后,兑换成原来的货币能够使货币量添加. 思路:本题事实上是Floyd的变形.将变换率作为构成图的路径的权值.只是构成的图是一个有向图. 最后将松弛操作变换为:if(dis[i][j]<dis[i][k]*dis[k][j]). #includ

[ An Ac a Day ^_^ ][kuangbin带你飞]专题四 最短路练习 POJ 2240 Arbitrage spfa求负环

题意就是问倒腾外币能不能升值 不用spfa 用其他的最短路算法也可以 松弛条件换成dist[v]<dist[u]*e[u][i].value 当然 貌似只有spfa有这个坑…… 有A  (value>1.0) A 这种情况……我的天 用Dij Floyd都只用判断如果松弛到了自己 那么一定有环 直接跳出就行 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<

POJ 2240 - Arbitrage(最短路)

题意: 给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加. 举例说就是1美元经过一些兑换之后,超过1美元.可以输出Yes,否则输出No. 分析: 首先我们要把货币之间的关系转化成一张图.转化时,用STL里面的map很方便. 为每种货币分配一个序列号,一个序列号代表了一个图中间的NODE,而node之间的edge用汇率表示. 一开始用Dijkstra算法做,死活AC不了,网友给的理由是: 由于Dijkstra算法不能处理带有负权值的最短路,但此题中,两种货币之间

poj 2240 Arbitrage(bellman-ford 判断正环)

http://poj.org/problem?id=2240 基本和poj 1860相同 只是把单点变成了任意点 做完1860再做这题就完全把思路套上就过了 做完才发现网上的题解都用的是floyd 不过整体思路都是大同小异吧 不过在效率上好像就低下了太多= = #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<stack> #include<

poj 2240 Arbitrage(Bellman-Ford算法学习)

Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15806   Accepted: 6648 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 currenc

POJ 2240 Arbitrage (spfa判环)

Arbitrage 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 Frenc