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 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
这题较简单,使用bellman-ford算法就可以了,注意输出,我因为输出WA几次
 #include <iostream>
#include<map>
#include<string.h>
using namespace std;
struct edge{
int u,v;
float rate;
} e[*];
int cur_num,edge_num;
float dis[];
map<string,int> mp;
int Bellman_ford(int c){
memset(dis,,*sizeof(float));
dis[c]=1.0;
for(int i=;i<cur_num;i++){
for(int j=;j<edge_num;j++){
if(dis[e[j].v]<dis[e[j].u]*e[j].rate){
dis[e[j].v]=dis[e[j].u]*e[j].rate;
}
}
}
if(dis[c]>1.0)
return ;
else
return ;
}
int main() {
int count=;
cin>>cur_num;
while(cur_num){
mp.clear();
for(int i=;i<cur_num;i++){
string s;
cin>>s;
mp[s]=i;
}
cin>>edge_num;
for(int i=;i<edge_num;i++){
string s1,s2;
float rate;
cin>>s1>>rate>>s2;
e[i].u=mp[s1];
e[i].v=mp[s2];
e[i].rate=rate;
}
int flag=;
for(int i=;i<cur_num;i++){
flag=Bellman_ford(i);
if(flag)
break;
} if(flag)
cout<<"Case "<<++count<<": Yes"<<endl;
else
cout<<"Case "<<++count<<": No"<<endl;
cin>>cur_num;
}
return ;
}

Arbitrage - poj 2240 (Bellman-ford)的更多相关文章

  1. Arbitrage POJ - 2240

    题目链接:https://vjudge.net/problem/POJ-2240 思路:判正环,Bellman-ford和SPFA,floyd都可以,有正环就可以套利. 这里用SPFA,就是个板子题吧 ...

  2. poj 2240 Arbitrage 题解

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 Descri ...

  3. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

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

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

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

  5. poj 2240 Arbitrage (Floyd)

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

  6. ACM/ICPC 之 最短路径-Bellman Ford范例(POJ1556-POJ2240)

    两道Bellman Ford解最短路的范例,Bellman Ford只是一种最短路的方法,两道都可以用dijkstra, SPFA做. Bellman Ford解法是将每条边遍历一次,遍历一次所有边可 ...

  7. poj1860 bellman—ford队列优化 Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22123   Accepted: 799 ...

  8. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  9. Bellman—Ford算法思想

    ---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...

随机推荐

  1. Stage3D 中的PerspectiveMatrix3D

    PerspectiveMatrix3D继承自Matrix3D.表示投影矩阵的功能类. 公式:用4X4矩阵向z=d的平面投影 public function perspectiveFieldOfView ...

  2. ife2015-task2-javascript-util.js

    util.js/** * Created by Administrator on 2016/12/14. *///判断是否为数组function isArray(arr){ return (arr i ...

  3. Ubuntu中彻底修改用户名及密码

    转自:http://blog.csdn.net/sailor201211/article/details/52305591 方案二:修改与用户和组相关的配置文件 这种方法更加本质,直接修改与用户和组相 ...

  4. Spring获取properties文件中的属性

    1.前言 本文主要是对这两篇blog的整理,感谢作者的分享 Spring使用程序方式读取properties文件 Spring通过@Value注解注入属性的几种方式 2.配置文件 applicatio ...

  5. Linux内核性能测试工具全景图

    1.Linux性能监控工具及对应的内核层 2.Linux性能基础测试工具及对应内核层 3.Linux性能监控工具Sar及对应内核层 4.Linux性能调优工具及对应的内核层

  6. 模拟服务器MockServer之Moco详细介绍

    转载:http://blog.csdn.net/vite_s/article/details/54583243 前面一篇介绍了如何用mockito来测试我们的一些异步任务,例如网络请求时候的异步回调. ...

  7. HTML图片热区

    文章来源于:https://www.cnblogs.com/mq0036/p/3337327.html <!DOCTYPE html> <html lang="en&quo ...

  8. Django的Form、CSRF、cookie和session

    Django是一个大而全的web框架,为我们提供了很多实用的功能,本文主要介绍Form.CSRF.cookie和session 一.Form 在web页面中form表单是重要的组成部分,为了数据安全和 ...

  9. 原生JS实现的h5小游戏-植物大战僵尸

    代码地址如下:http://www.demodashi.com/demo/12755.html 项目介绍 本项目是利用原生js实现的h5小游戏-植物大战僵尸,主要结合了一下自己对于h5小游戏的理解,结 ...

  10. 不得不看的Java代码性能优化总结

    原文:https://blog.csdn.net/mr_smile2014/article/details/50112723 前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么 ...