POJ 2240:Arbitrage:folyd最短路算法变形求有向图的盈利环存在
来源:互联网 发布:mac装win10鼠标不能用 编辑:程序博客网 时间:2024/06/05 15:58
Arbitrage
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15069 Accepted: 6333
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.
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.
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
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
Source
Ulm Local 1996
将货币的转换率看做路径的花费,使用folyd算法求得两两货币之间的最大值,通过a到b的最大值乘以b到a的最大值大于1,判断存在盈利环。该题目是有向图的盈利环问题,仍然可以使用最短路算法的变形来求解。
#include<stdio.h>#include<stdlib.h>#include<string.h>int n,lu;double a[35][35],d[35][35];char ming[35][105];char ta[105],tb[105];int tta,ttb;int result;void find(){int i,j;for(i=1;i<=n;i++){if(strcmp(ta,ming[i])==0){tta=i;break;}}for(i=1;i<=n;i++)if(strcmp(tb,ming[i])==0){ttb=i;break;}}void init(){int i,j;double tc;for(i=1;i<=n;i++)scanf("%s",ming[i]);scanf("%d",&lu);for(i=1;i<=n;i++)for(j=1;j<=n;j++)d[i][j]=a[i][j]=0;while(lu--){scanf("%s%lf%s",ta,&tc,tb);find();d[tta][ttb]=a[tta][ttb]=tc;}result=0;}void folyd(){int i,j,k;for(k=1;k<=n;k++){for(i=1;i<=n;i++){for(j=1;j<=n;j++){if(d[i][k]&&d[k][j]&&d[i][j]<d[i][k]*d[k][j])d[i][j]=d[i][k]*d[k][j];}}}}void search(){int i,j;for(i=1;i<=n;i++){if(result)break;for(j=1;j<=n;j++)if(d[i][j]*d[j][i]>1){result=1;break;}}}int main(){int i,j;int c=1;while(scanf("%d",&n)!=EOF&&n){init();folyd();search();if(result)printf("Case %d: Yes\n",c);elseprintf("Case %d: No\n",c);c++;}return 0;}
0 0
- POJ 2240:Arbitrage:folyd最短路算法变形求有向图的盈利环存在
- POJ 2240 Arbitrage (Floyd最短路变形)
- poj 2240 Arbitrage 最短路变形 spfa
- poj 3615 folyd变形(求有向图多对最大瓶颈路)
- POJ 2240Arbitrage 最短路正环存在问题
- poj 3268(dijkstra变形---有向图来回最短路)
- Bellman变形得来的SPFA最短算法(不能含负环);Floyd求两点间最短路,求有向图的闭包;
- POJ 题目2240 Arbitrage(最短路变形)
- Arbitrage(最短路-floyd算法变形求正权)
- poj 2240 Arbitrage 最短路
- POJ 2240 Arbitrage(最短路)
- POJ-2240 Arbitrage(最短路)
- POJ 1125 Stockbroker Grapevine 最短路folyd算法
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
- POJ 1860:Currency Exchange:bellman最短路变形进行盈利判环
- poj 1062 dijkstra求最短路变形
- poj 2240 Arbitrage(最短路+Bellman-Ford)
- POJ 2240 Arbitrage(最短路 Floyd)
- const修饰的类成员函数
- Android Browser学习三 多窗口: 展示第一个Tab的过程
- iOS开发 导航栏 UINavigationBar 属性
- 面试算法题
- Android Design - 设计风格
- POJ 2240:Arbitrage:folyd最短路算法变形求有向图的盈利环存在
- third_party/
- 优秀开源项目网站分享
- POJ 1017 Packets (贪心)
- (*(volatile unsigned int *))详解-数值常量如何转化为内存地址_转阿拉丁神丢新浪博
- spring mvc 给action添加事务不成功的原因
- 从四大维度看O2O与传统电商的不同 作者: 黄渊普
- 5.7.4 非静态数据初始化
- Block使用中的一些疑问解答(包括循环引用问题的解决)