Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13800   Accepted: 5815

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

Source

 //766 MS    772 KB    GNU C++
/* 题意:
给出一个图,n个点,m条边,每条边有一个权值,求是否存在一条回路,使边的权值积大于1 最短路径:
floyd小变异。数据比较小,直接用floyd遍历一遍,然后判断是否存在可行解 */
#include<iostream>
#include<map>
#include<string>
#include<stdio.h>
using namespace std;
double g[][];
int n,m;
void floyd()
{
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(g[i][k]!=- && g[k][j]!=-)
if(g[i][j]==- || g[i][k]*g[k][j]>g[i][j])
g[i][j]=g[i][k]*g[k][j];
int flag=;
for(int i=;i<=n;i++){
//printf("%lf\n",g[i][i]);
if(g[i][i]>1.0)
flag=;
}
if(flag) puts("Yes");
else puts("No");
}
int main(void)
{
string a,b;
double c;
int k=;
while(cin>>n,n)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
g[i][j]=-;
map<string,int>M;
for(int i=;i<=n;i++){
cin>>a;
M[a]=i;
}
scanf("%d",&m);
for(int i=;i<m;i++){
cin>>a>>c>>b;
g[M[a]][M[b]]=c;
}
printf("Case %d: ",k++);
floyd();
}
return ;
}

poj 2240 Arbitrage (最短路径)的更多相关文章

  1. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

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

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

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

  3. poj 2240 Arbitrage 题解

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

  4. poj 2240 Arbitrage (Floyd)

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

  5. POJ 2240 Arbitrage【Bellman_ford坑】

    链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  6. POJ 2240 Arbitrage(floyd)

    http://poj.org/problem?id=2240 题意 : 好吧,又是一个换钱的题:套利是利用货币汇率的差异进行的货币转换,例如用1美元购买0.5英镑,1英镑可以购买10法郎,一法郎可以购 ...

  7. poj 2240 Arbitrage (最短路 bellman_ford)

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

  8. POJ 2240 Arbitrage(判正环)

    http://poj.org/problem?id=2240 题意:货币兑换,判断最否是否能获利. 思路:又是货币兑换题,Belloman-ford和floyd算法都可以的. #include< ...

  9. poj 2240 Arbitrage(Bellman_ford变形)

    题目链接:http://poj.org/problem?id=2240 题目就是要通过还钱涨自己的本钱最后还能换回到自己原来的钱种. 就是判一下有没有负环那么就直接用bellman_ford来判断有没 ...

随机推荐

  1. 1、SpringBoot+MybatisPlus整合

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  2. XML DTD约束 对xml文件的crud的查询Read Retrieve操作 xml递归遍历

    本地的dtd文档 xml中引入dtd文档 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE 书 ...

  3. mysql5.6 配置 文件

    mysql 3306 主库配置文件 [client] port = 3306 default-character-set=utf8mb4 socket = /ssd/mysql/3306/tmp/my ...

  4. spring cloud 学习之服务消费者(Feign)

    一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注 ...

  5. PHP中json_encode后,在json字符串中依然显示中文的解决方案

    <?php header("Content-Type:text/html;charset=utf-8;"); $arr = array ('Version_code'=> ...

  6. [BZOJ4196]软件包管理器(树链剖分)

    [BZOJ4196] install x-> 询问根节点到x路径上0的个数,然后全变1 uninstall x-> 询问x子树(包括x)中1的个数,然后全边0 Code #include ...

  7. salt 通信及其安全

    salt 通信及其安全 模型架构 server-agent通信模型: server就是salt master; agent就是salt-minion salt也可以作为一个单点服务器管理工具使用,或者 ...

  8. c语言printf()输出格式大全(转载)

    1.转换说明符      %a(%A)     浮点数.十六进制数字和p-(P-)记数法(C99)      %c             字符      %d             有符号十进制整 ...

  9. (1)strchr

    const char * strchr ( const char * str, int character ); char * strchr ( char * str, int character ) ...

  10. Adobe Photoshop Lightroom 5.3和序列号

    Adobe Photoshop Lightroom是一款针对专业摄影师开发的专业照片管理和处理软件.12.11发布了Lightroom 5.3正式版,这个版本支持RAW格式(相机原始数据格式),镜头配 ...