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. eclipse debug Liunx服务器上的svn项目

    1.本地项目提交到svn上,以保证本地代码与服务器代码相同 2.开启服务器debug端口 3.使用root账号重新部署服务器项目并监听catalina.out sh /home/p/deploy/gt ...

  2. error: pathspec 'master' did not match any file(s) known to git.

    问题描述: 在远程服务器上新建裸仓库git  --bare init : git clone裸仓库到本地: 本地新建并切换分支xccdev,git checkout -b xccdev: 从xccde ...

  3. selenium之 chromedriver与chrome版本映射表(更新至v2.31)

    转自:http://blog.csdn.net/huilan_same/article/details/51896672 chromedriver版本 支持的Chrome版本 v2.31 v58-60 ...

  4. 【RF库Collections测试】Keep In Dictionary

    Name:Keep In DictionarySource:Collections <test library>Arguments:[ dictionary | *keys ]Keeps ...

  5. Splash resource_timeout 属性

    resource_timeout属性用于设置加载的超时时间,单位是秒,如果设置为 0 代表不检测超时,如下,设置超时时间为 0.1 秒: function main(splash) splash.re ...

  6. 《Lua程序设计》第3章 表达式 学习笔记

    3.1 算术操作符“+”(加法).“-”(减法).“*”(乘法).“/”(除法).“^”(指数).“%”(取模).3.2 关系运算符< > <= >= == ~=3.3 逻辑操 ...

  7. XML的基本用法

    一.概述 XML全称为可扩展的标记语言.主要用于描述数据和用作配置文件. XML文档在逻辑上主要由一下5个部分组成: XML声明:指明所用XML的版本.文档的编码.文档的独立性信息 文档类型声明:指出 ...

  8. Android设计和开发系列第一篇:Notifications通知(Develop—API Guides)

    Notifications IN THIS DOCUMENT Design Considerations Creating a Notification Required notification c ...

  9. 【Spring Boot && Spring Cloud系列】那些Spring Boot中踩过的坑

    一.不连接数据库启动springboot报错 Cannot determine embedded database driver class for database type NONE 原因:Spr ...

  10. Elasticsearch学习之Java操作1

    1. Elasticsearch为Java用户提供了两种内置客户端 1.1 节点客户端(node client): 节点客户端以无数据节点(none data node)身份加入集群,换言之,它自己不 ...