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. Shiro集成Spring

    本篇博客主要讲述的是两者的集成.不涉及到各自的详细细节和功能. 因为官方给出的文档不够具体,对新手而言通过官方文档还不可以非常快的搭建出SpringShiro的webproject.本博客将通过实际的 ...

  2. linux_开发软件安装=命令步骤

    1.Linux 操作系统软件安装以及redis 学习    JDK ----- Java开发运行环境    Tomcat -- WEB程序的服务器    MySQL --- 持久化存储数据    Re ...

  3. [AX2012]关于财务默认维度

    和以前的版本一样,AX2012中很多地方都使用财务维度,比如客户.销售订单.销售订单行等,根据相应的财务维度设置,生成的相应财务分录将带有财务维度,方便后续对财务分录交易的分析.下图是在客户记录上设置 ...

  4. vuejs中使用echart图表

    首先安装echart npm i echarts -S 加下来以使用这个图表为例 在vue组件中像这样使用: <template> <div :class="classNa ...

  5. 基于github+hexo搭建个人博客(window)

    0x01 环境搭建 1.Node.js环境 下载Node.js安装文件:https://nodejs.org/en/download/ 根据系统选择相应安装包下载,安装过程一路Next,默认设置即可. ...

  6. nmap 中的idle scan

    http://www.offensive-security.com/metasploit-unleashed/Port_Scanning http://blog.csdn.net/dong976209 ...

  7. 微信小程序源码案例大全

    微信小程序demo:足球,赛事分析 小程序简易导航 小程序demo:办公审批 小程序Demo:电魔方 小程序demo:借阅伴侣 微信小程序demo:投票 微信小程序demo:健康生活 小程序demo: ...

  8. Linux调试分析诊断利器——strace

    strace是个功能强大的Linux调试分析诊断工具,可用于跟踪程序执行时进程系统调用(system call)和所接收的信号,尤其是针对源码不可读或源码无法再编译的程序. 在Linux系统中,用户程 ...

  9. 剑指offer——49

    丑数 因子只含2,3,5的数称为丑数. 怎么求第K大的丑数呢.K可以为10^7 最简单的做法是,对每个数判断是否为丑数. 复杂度为O( n * log(n) ),理论上是不行的. uglys[i] 来 ...

  10. photobeamer

    NOKIA出品的photobeamer https://www.photobeamer.com/你打开这个网站,会生成的二维码手机上打开photobeamer这个软件,选择要显示的相片,再扫描刚才网页 ...