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. Objective-C 协议和运行时检查方法、类是否存在

    协议的声明: // // Person.h // TestOC01 // // Created by xinye on 13-10-23. // Copyright (c) 2013年 xinye. ...

  2. JAVA内存泄露分析及解决

    一,问题产生     项目采用Tomcat6.0为服务器,数据库为mysql5.1,数据库持久层为hibernate3.0,以springMVC3.0为框架,项目开发完成后,上线前夕进行稳定性拷机,测 ...

  3. How to solve the problem : &quot;You have been logged on with a temporary profile&quot;

    /*By Jiangong SUN*/ I've encountered a problem in one server, which is : Every time I login into the ...

  4. sublime自定义代码段

    打开tools>developer>new snippet 默认代码 <snippet> <content><![CDATA[ Hello, ${1:this ...

  5. 【Python 爬虫系列】从某网站下载小说《鬼吹灯》,正则解析html

    import re import urllib.request import urllib.parse import urllib.error as err import time # 下载 seed ...

  6. 指定cmd窗口或tomcat运行窗口的名称

    1. 指定cmd窗口运行时名称 1)直接执行命令:title 窗口名称 2)bat文件中直接加上命令:title 窗口名称 例子: title test_ v1 java -jar -Dfile.en ...

  7. 【delphi】delphi出现‘尚未调用CoInitialize’异常

    1. 由于使用ado等引起的异常. 2. 若是子线程或某方法操作ado可在该方法开头和结尾分别加上: function myfun begin CoInitialize(nil); // your c ...

  8. System.Web.Mvc.AJAX缺少程序集引用

    今天在构建新项目的时候,出现这个错误,实际上这个出现过很多次了,但是每次都忘记之前怎么解决的,之前博客也不记得写过没有...

  9. Material Design系列第二篇——Getting Started

    Getting Started This lesson teaches you to Apply the Material Theme Design Your Layouts Specify Elev ...

  10. 【python3】window下 vscode 配置 python3开发环境

    本文以python3.7 为例 一 下载python3 url : https://www.python.org/downloads/windows/ 提示: 安装过程中.记得勾选  添加环境变量 二 ...