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. lintcode-【中等】数飞机

    题目: 给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机? 样例: 对于每架飞机的起降时间列表:[[1,10],[2,3],[5,8],[4,7]], ...

  2. dedecms代码研究三

    上次,我们从dedecms的index.PHP文件中了解到了很多信息,也提出了一些问题: 1)加载了/include/common.inc.php,里面做了哪些工作? 2)/include/arc.p ...

  3. win10内网外网智能访问

    当电脑同时连接有线和WiFi时(有线连接为内网,WiFi为外网),会出现内网和外网内容无法同时访问的情况. 本方法实现内网和外网的同时访问. 第一步: 输入指令 “route print ” 查看路由 ...

  4. 去掉hive字段中的tab

    去除空格用trim 去除tab用如下方法 select regexp_replace(secdomainname,'\\s+','') from dwb_cndns_node_secdomain_d ...

  5. Java串口通信详解

    http://blog.csdn.net/kabini/article/details/1601324 ———————————————————————————————————————————————— ...

  6. CRC

    #define POLY 0x1021 /** * Calculating CRC-16 in 'C' * @para addr, start of data * @para num, length ...

  7. CSS3凹凸字

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  8. python 循环定时器

    有时候需要循环执行某个任务,最简单的就是用thread.Timer. 谷歌了一下,发现大家竟然用sleep 来实现循环,也不知道谁想的这个方法,竟然很少有人想到join一下,很奇怪. # -*- co ...

  9. Blob(二进制)、byte[]、long、date之间的类型转换

    String转成byte[]类型存入数据库,数据库字段对应byte[]的类型为Blob类型 String value = this.getParamNotNnll("bgvalue" ...

  10. Spinal Tap Case

    function spinalCase(str) { // "It's such a fine line between stupid, and clever." // --Dav ...