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 题意:给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加。
   举例说就是1美元经过一些兑换之后,超过1美元。可以输出Yes,否则输出No。
 #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define INF 9999999
#define MAX 300
int n;
float g[MAX][MAX];
char name[][];
int fun(char *str)
{
int i;
for(i=; i<=n; i++)
{
if(strcmp(name[i],str)==)
return i;
}
}
void FLOYD()
{
int i,j,k;
double t;
for(k=; k<=n; k++)
{
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
{
t=g[i][k]*g[k][j];
if(t>g[i][j])
g[i][j]=t;
if(g[i][j]>&&i==j)
{
printf("Yes\n");
return ;
}
}
}
}
printf("No\n");
}
int main()
{
int i,k=,m;
float t;
char str1[],str2[];
while(~scanf("%d",&n),n)
{
memset(g,,sizeof(g));
for(i=; i<=n; i++)
scanf("%s",&name[i]);
scanf("%d",&m);
for(i=; i<=m; i++)
{
scanf("%s %llf %s",str1,&t,str2);
g[fun(str1)][fun(str2)]=t;
}
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. IntelliJ+Maven+Spring+Tomcat项目搭建(MAC)

    1.新建项目 打开idea,通过File->new->project,会弹出如下的信息: 接下来点击下一步,创建项目,点击“下一步”: 选择默认的Maven以及setting文件,点击“下 ...

  2. Ubuntu1.6安装Go【小白版】

    [安装golang,并配置环境变量]1.将go下载到Home目录并解压 一键解压会 自动会解压到 Home/go目录. 2.设置环境变量 nano是一种文本编辑器,也可以用其他的编辑器. 输入以下命令 ...

  3. 面试题集锦;有关作用域和this的指向

    作用域面试题: 1. fn() function fn () { console.log(12) } var as = function () { console.log(45) } 2. var a ...

  4. js数据类型和变量

    Number JavaScript不区分整数和浮点数,统一用Number表示: 123 0.345 -99 NaN 当无法计算结果时用NaN表示 Infinity 表示无限大,当数值超过js的Numb ...

  5. Java 8.9 游戏:井字游戏(C++&Java)

    思路框架同8.20 : C++: #include<iostream> #include<string> using namespace std; class Chess { ...

  6. numpy常见属性、创建数组

      1.几种常见numpy的属性 ndim:维度 shape:行数和列数 size:元素个数 >>> import numpy as np #导入numpy模块,np是为了使用方便的 ...

  7. 使用开源的工具解析erspan流量

    Decapsulation ERSPAN Traffic With Open Source Tools Posted on May 3, 2015 by Radovan BrezulaUpdated ...

  8. python多线程下载网页图片并保存至特定目录

    #!python3 #multidownloadXkcd.py - Download XKCD comics using multiple threads. import requests impor ...

  9. eclipse集成svn进行项目开发

    在用eclipse进行项目开发的时候,报了一个错误:switch不支持String的参数.这个问题的原因是因为jre版本低于1.7,而当前的eclipse版本最高只能选1.6,无奈,我只能考虑换ecl ...

  10. CF Round #510 (Div. 2)

    前言:没想到那么快就打了第二场,题目难度比CF Round #509 (Div. 2)这场要难些,不过我依旧菜,这场更是被\(D\)题卡了,最后\(C\)题都来不及敲了..最后才\(A\)了\(3\) ...