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

[Submit]   [Go Back]   [Status]   [Discuss]

——————————————————————————————我是分割线————————————————————————

思路好题。

最短路Bellman-Ford算法。

用图中的顶点代表货币。

若第i种货币能兑换成第j种,汇率为cij,则从顶点i至顶点j连一条有向边,权值为cij。

问题就转化成判断图中是否存在某个顶点,从它出发的某条回路上的权值乘积大于1。

大于1即有套汇。

 /*
Problem:poj 2240 Arbitrage
OJ: POJ
User: S.B.S.
Time: 797 ms
Memory: 856 kb
Length: 1754 b
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#include<functional>
#include<bitset>
#include<vector>
#include<list>
#include<map>
#define F(i,j,k) for(int i=j;i<=k;i++)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define maxn 10001
#define inf 0x3f3f3f3f
#define maxm 1001
#define mod 998244353
//#define LOCAL
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct EXCHANGE
{
int ca;
int cb;
double change;
}ex[maxn];
char a[maxn],b[maxn];
char name[maxn][];
double x;
double d[maxn];
int ca=,ans;
bool flag=false;
int n,m;
inline int init()
{
cin>>n;
if(n==) return ;
for(int i=;i<n;i++) cin>>name[i];
cin>>m;
for(int i=;i<m;i++)
{
int j,k;
cin>>a;cin>>x;cin>>b;
for(j=;strcmp(a,name[j]);j++);
for(k=;strcmp(b,name[k]);k++);
ex[i].ca=j;ex[i].change=x;ex[i].cb=k;
}
return ;
}
inline void ford(int u)
{
flag=false;M(d,);
d[u]=;
F(k,,n)F(i,,m-){
if(d[ex[i].ca]*ex[i].change>d[ex[i].cb])
{
d[ex[i].cb]=d[ex[i].ca]*ex[i].change;
}
}
if(d[u]>1.0) flag=true;
}
int main()
{
std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
while(init()){
F(i,,n-){
ford(i);
if(flag) break;
}
if(flag) cout<<"Case "<<++ca<<": "<<"Yes"<<endl;
else cout<<"Case "<<++ca<<": "<<"No"<<endl;
}
return ;
}

poj 2240

poj 2240 Arbitrage 题解的更多相关文章

  1. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

    题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...

  2. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  3. poj 2240 Arbitrage (Floyd)

    链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...

  4. POJ 2240 Arbitrage【Bellman_ford坑】

    链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  5. POJ 2240 Arbitrage(floyd)

    http://poj.org/problem?id=2240 题意 : 好吧,又是一个换钱的题:套利是利用货币汇率的差异进行的货币转换,例如用1美元购买0.5英镑,1英镑可以购买10法郎,一法郎可以购 ...

  6. POJ 2240 Arbitrage (求负环)

    Arbitrage 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/I Description Arbitrage is the ...

  7. poj 2240 Arbitrage (最短路 bellman_ford)

    题目:http://poj.org/problem?id=2240 题意:给定n个货币名称,给m个货币之间的汇率,求会不会增加 和1860差不多,求有没有正环 刚开始没对,不知道为什么用 double ...

  8. POJ 2240 Arbitrage(判正环)

    http://poj.org/problem?id=2240 题意:货币兑换,判断最否是否能获利. 思路:又是货币兑换题,Belloman-ford和floyd算法都可以的. #include< ...

  9. poj 2240 Arbitrage(Bellman_ford变形)

    题目链接:http://poj.org/problem?id=2240 题目就是要通过还钱涨自己的本钱最后还能换回到自己原来的钱种. 就是判一下有没有负环那么就直接用bellman_ford来判断有没 ...

随机推荐

  1. 记在VMware虚拟机中对网站进行性能压力测试的经历

    由于本次测试,仅仅是对静态网站首页进行的测试,所以没有涉及到MySQL数据库的性能监测 服务器基本配置 webbench测试工具 Linux上一款优秀的web性能压力测试工具.webbench最多可以 ...

  2. Python3.7.2,在Linux上跑来跑去的,是在升级打怪么?

    Python3.7.2,在Linux上跑来跑去的,是在升级打怪么?   前不久,发布了Python在Windows(程序员:Python学不学?完全没必要纠结)和Mac OS(我是Python,P派第 ...

  3. 003.FTP客户端连接

    一 命令行连接 注意: 1:命令行连接不支持目录下载,使用mget也只会将目录下文件下载,不会下载目录本身. 2:命令行连接不支持断点续传. ftp [服务端IP] -help #获取帮助 - -mg ...

  4. cv2 与 matplotlib 的 Bug 记录

    cv2 的 imread 无法读取中文路径 解决方案: img = cv2.imdecode(np.fromfile(image_path,dtype=np.uint8),cv2.IMREAD_COL ...

  5. WEP自动破解工具wesside-ng

    WEP自动破解工具wesside-ng   wesside-ng是aircrack-ng套件提供的一个概念验证工具.该工具可以自动扫描无线网络,发现WEP加密的AP.然后,尝试关联该AP.关联成功后, ...

  6. Ubuntu 18.04 更新源

    [原因] 使用国外的源,在更新软件的时候会很慢,换成国内的源会快很多. [命令] 1.备份源文件 sudo cp /etc/apt/sources.list /etc/apt/sources.list ...

  7. pygame系列_小球完全弹性碰撞游戏_源码下载

    之前做了一个基于python的tkinter的小球完全碰撞游戏: python开发_tkinter_小球完全弹性碰撞游戏_源码下载 今天利用业余时间,写了一个功能要强大一些的小球完全碰撞游戏: 游戏名 ...

  8. API测试利器postMan 使用教程

    自从开始做API开发之后,我就在寻找合适的API测试工具.一开始不是很想用Chrome扩展,用的 WizTools 的工具,后来试过一次 Postman 之后就停不下来了,还买了付费的Jetpacks ...

  9. CentOS 7下启动postfix服务报错:fatal: parameter inet_interfaces: no local interface found for ::1

    sed -i 's/inet_interfaces = localhost/inet_interfaces = all' /etc/postfix/main.cf service postfix re ...

  10. JSP Servlet学习笔记——使用fileupload上传文件

    关键代码如下: index.jsp <body> <center> <h3>文件上传</h3> <font color="red&quo ...