Time Limit: 1000MS Memory Limit: 65536K

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算法的应用。

请参考:http://www.cnblogs.com/freezhan/p/3238968.html

至于里面说的:

注意:

          这里的松弛操作要循环 N 次才能过,
       书上的松弛操作一直都是 N-1 次
       对于为什么是 N 或者 N-1 次一直没有理解清楚

是因为这题目会测试很坑的样例,比如:

input:
1
a
1
a 1.5 a
output:
Yes

这样是一条头尾都是自己的节点的边,如果不循环n次,会直接忽略这条边……这样答案就会出错。

 #include<cstdio>
#include<iostream>
#include<map>
using namespace std;
struct Edge{
int u,v;
double r;
}edge[];
int n,m;
double d[];
void init(int st)
{
for(int i=;i<=n;i++) d[i]=;
d[st]=;
}
void relax(int u,int v,double r){if(d[v] < d[u]*r) d[v]=d[u]*r;}
bool bellman_ford(int st)
{
init(st);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++) relax(edge[j].u,edge[j].v,edge[j].r);
}
if(d[st]>1.0) return false;
return true;
}
int main()
{
int kase=;
while(scanf("%d",&n) && n!=)
{
map<string,int> currency;
for(int i=;i<=n;i++)
{
string str;
cin>>str;
currency[str]=i;
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
string str1,str2;double r_tmp;
cin>>str1>>r_tmp>>str2;
edge[i].u=currency[str1];
edge[i].v=currency[str2];
edge[i].r=r_tmp;
}
bool flag=true;
for(int i=;i<=n;i++)
{
if(bellman_ford(i)==false)
{
flag=false;
break;
}
}
if(flag==false) printf("Case %d: Yes\n",++kase);
else printf("Case %d: No\n",++kase);
}
}

POJ 2240 - Arbitrage - [bellman-ford求最短路]的更多相关文章

  1. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

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

  2. poj 2240 Arbitrage 题解

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

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

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

  4. POJ 2240 Arbitrage (求负环)

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

  5. poj 2240 Arbitrage (Floyd)

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

  6. POJ 2240 Arbitrage (Bellman Ford判正环)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:27167   Accepted: 11440 Descri ...

  7. poj 2240 Arbitrage (最短路 bellman_ford)

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

  8. POJ 2240 Arbitrage(最短路 套汇)

    题意  给你n种币种之间的汇率关系  推断是否能形成套汇现象  即某币种多次换为其他币种再换回来结果比原来多 基础的最短路  仅仅是加号换为了乘号 #include<cstdio> #in ...

  9. poj 2240 Arbitrage

    Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   Java class name ...

随机推荐

  1. SpringBoot------JPA连接数据库

    步骤: 1.在pom.xml文件下添加相应依赖包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...

  2. 解决app频繁更新方案

    目前由于我们项目的特定场景,app的主要问题在于如何做到可以频繁更新而不需要频繁发布,尤其是ios,发布app store的周期基本上是2周左右,虽然可以通过企业号解决,但是后期的打包,分发给用户仍较 ...

  3. 对象克隆技术Object.clone()

    Java中对象的创建 clone顾名思义就是复制, 在Java语言中, clone方法被对象调用,所以会复制对象. 所谓的复制对象,首先要分配一个和源对象同样大小的空间,在这个空间中创建一个新的对象. ...

  4. 8 -- 深入使用Spring -- 2...5 Spring 3.0 新增的注解

    8.2.5 Spring 3.0 新增的注解 @DependsOn @Lazy @DependsOn :用于强制初始化其他Bean.修饰Bean类或方法,可以指定一个字符串数组作为参数,每个数组元素对 ...

  5. 给button添加边框和圆角

    button是我们经常用到的控件,我把它的属性罗列一下: UIButton *Button = [[UIButton alloc] initWithFrame:CGRectMake(, , , )]; ...

  6. PHP代码层防护与绕过

    0x01 前言 在一些网站通常会在公用文件引入全局防护代码进行SQL注入.XSS跨站脚本等漏洞的防御,在一定程度上对网站安全防护还是比较有效的. 这里讨论一下关键字过滤不完善及常见正则匹配存在的问题, ...

  7. Android文件系统编译出错记录

    错误1: 注意:external/protobuf/java/src/main/java/com/google/protobuf/GeneratedMessageLite.java 使用了未经检查或不 ...

  8. PHP 二叉树 二叉排序树实现

    <?php /** * PHP 二叉树 * @author : xiaojiang 2014-01-01 * */ class Tree { protected $k = null; prote ...

  9. 删除SQL注入的一些方法总结

    sql替换法: ); set @myStr='oa_20121026new.bak</title><style>.alx2{position:absolute;clip:rec ...

  10. 虚拟机可以ping同宿主机,宿主机ping不通虚拟机

    虚拟机里能ping同本机,而本机却ping不通虚拟机,或者虚拟机不能ping通本机,可能有如下原因: 如果是桥接模式,那么可能性1:虚拟机防火墙禁ping,请关闭虚拟机防火墙重试:root 状态下se ...