POJ 2240 Arbitrage 最短路径 Floyed-Warshall
2015-10-24 12:01
519 查看
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 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
Sample Output
Source
Ulm Local 1996
题目大意:
不同货币间有不同的汇率,问是否存在货币a-->货币b--->.......----> 货币a实现获利
可以转化为加权有向图货币对应节点,货币兑换关系对应边
则货币i经由货币k兑换至货币j的比率为 dist[i][k]*dist[k][j]
\因为本题含回路所以要加条件 i!=j&&k!=j&&k!=i 防止死循环
最后判断 dist[i][j]*dist[j][i]>1 表示可以套汇
ACcode:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18416 | Accepted: 7800 |
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
题目大意:
不同货币间有不同的汇率,问是否存在货币a-->货币b--->.......----> 货币a实现获利
可以转化为加权有向图货币对应节点,货币兑换关系对应边
则货币i经由货币k兑换至货币j的比率为 dist[i][k]*dist[k][j]
\因为本题含回路所以要加条件 i!=j&&k!=j&&k!=i 防止死循环
最后判断 dist[i][j]*dist[j][i]>1 表示可以套汇
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 1005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI acos(-1.0)
#define E exp(1)
#define esp 1e-6
using namespace std;
char money[maxn][55],x[55],y[55];
double dist[maxn][maxn],t;
int n,m;
int get_id(char *str){
FOR(i,1,n)if(!strcmp(money[i],str))return i;
}
int main(){
int cnt=1;
while(rd(n)&&n){
MT(dist,0);
FOR(i,1,n)rds(money[i]);
rd(m);FOR(i,1,m){
scanf("%s%lf%s",&x,&t,&y);
dist[get_id(x)][get_id(y)]=t;
}
FOR(k,1,n)FOR(i,1,n)FOR(j,1,n)
if(i!=j&&k!=j&&k!=i)
dist[i][j]=max(dist[i][k]*dist[k][j],dist[i][j]);
bool flag=false;
FOR(i,1,n)FOR(j,1,n)
if(dist[i][j]*dist[j][i]>1+esp){
flag=true;break;
}
printf("Case %d: %s",cnt++,flag?"Yes\n":"No\n");
}
return 0;
}
/*
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*/
相关文章推荐
- bzoj4102【Usaco2015 Open】Bessie
- android studio制作9-patch图片
- 我读Fine-Grained Change Detection of Misaligned Scenes with Varied Illuminations
- 自定义虚拟键盘-光标不随自己点在哪里而移动
- 九度OJ 1135:字符串排序 (排序)
- [CareerCup] 12.2 Find the Reason of Crash 找到程序崩溃的原因
- 九度OJ 1135:字符串排序 (排序)
- 三条坐标轴为母线的圆锥面方程
- [软考]之软件过程模型I
- 九度OJ 1134:密码翻译 (翻译)
- 九度OJ 1134:密码翻译 (翻译)
- 项目目标文档
- POJ1338 Ugly Numbers
- 一波Linux下常用的文件相关命令小整理
- equals、hashCode、toString 方法重写
- iOS动画效果和实现
- 九度OJ 1133:学分绩点 (加权平均数)
- C程序内存布局
- webrtc_ns模块android手机音频处理
- MVC4.0 利用IActionFilter实现单一Action返回多种结果