Time Limit: 1000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u   Java class name: Main

[Submit] [Status] [Discuss]

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
 #include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
#define maxx 35
#define maxn 1000
char name[maxx][],a[],b[]; ///链表进行存储
double maxdis[maxn]; ///类似于dis[]数组 不过这次求最大回路
double x; ///兑换率
int n,t;
int falg; struct exchange
{
int ci,cj;
double cij;
}ex[maxn]; void Bellman(int v0)
{
falg=;
memset(maxdis,,sizeof(maxdis));
maxdis[v0]=;
for(int k=;k<=n;k++) ///要寻找回路 所以从maxdis[0]递推maxdis[1]....maxdis[n]
{
for(int i=;i<t;i++) ///每条边加入是否值变大使最大
{
if(maxdis[ex[i].ci]*ex[i].cij>maxdis[ex[i].cj]) ///是程的关系了~~~
{
maxdis[ex[i].cj]=maxdis[ex[i].ci]*ex[i].cij; ///求最大的 遇到大的就更新
}
}
}
if(maxdis[v0]>)
falg=;
} int main()
{
int num;
int casee=;
while(scanf("%d",&n),n)
{
for(int i=;i<n;i++)
{
cin>>name[i];
//scanf("%s",name[num]);
}
int i,j,k;
scanf("%d",&t);
for(i=;i<t;i++)
{
cin>>a>>x>>b;
//scanf("%s%if%s",a,&x,b);
for(j=;strcmp(a,name[j]);j++); ///,是空循环 没有循环语句的意思
for(k=;strcmp(b,name[k]);k++); ///将字符串变成了数字 对应关系
ex[i].ci=j;
ex[i].cij=x;
ex[i].cj=k;
///cout<<i<<' '<<j<<' '<<k<<"!!!!!!!"<<endl; 输出一次啊 挺神奇的东东
}
for(int i=;i<n;i++)
{
Bellman(i); ///遍历每一个点的回路
if(falg) ///如果出现兑换率〉1的现象 标记直接退出就好
break;
}
if(falg)
printf("Case %d: Yes\n",++casee);
else
printf("Case %d: No\n",++casee);
}
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. 【转】Using Gamma 2.2

    This is a detailed description of the work with Gamma 2.2. If you are only interested in exact instr ...

  2. COOKIE&&SESSION

    ---------------------------------------------------------------------------COOKIE------------------- ...

  3. CentOS 6.5下搭建LAMP环境详细步骤

    1.确认搭建LAMP所需的环境是否已经安装: [root@localhost ~]#rpm -q make gcc gcc-c++ zlib-devel libtool libtool-ltdl li ...

  4. Reactjs 入门基础(一)

    实例中我们引入了三个库: react.min.js .react-dom.min.js 和 browser.min.js: 1,react.min.js -React 的 核心库 2,react-do ...

  5. java集合类(二)

    第十六天知识点总结 一.泛型 泛型:java jdk 1.5 新特性. 泛型的好处: 1.运行时的错误提前到编译时. 2.避免无谓的强制类型转换 自定义方法泛型:自定义泛型就是一个数据类型的占位或一个 ...

  6. Redis安装及主从配置

    一.何为Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有 ...

  7. TortoiseSVN文件夹及文件状态图标不显示解决方法

    win8 64位系统,原本svn是好用的,安装了klive金山快盘后,svn图标都不显示了.最后通过修改注册表解决: win+R调出运行框,输入regedit,打开注册表编辑器. HKEY_LOCAL ...

  8. super

    [super] Return a proxy object that delegates method calls to a parent or sibling class of type. This ...

  9. 国内外著名B2C系统介绍兼比较ASP.NET和PHP

    国内外著名B2C系统介绍兼比较ASP.NET和PHP   2010-06-08 11:44  来源:  我来投稿  我要评论    中介交易 SEO诊断淘宝客 站长团购 云主机 A5外包  国内外著名 ...

  10. ssh搭建后的简化

    关于ssh如何搭建还有不懂得朋友可以参考以下网址:http://www.cnblogs.com/LarryBlogger/p/5841446.html 在这里我就不重复再讲了! ssh搭建后的简化 简 ...