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. jQuery最佳编程实践

    加载jQuery 1.坚持使用CDN来加载jQuery,这种别人服务器免费帮你托管文件的便宜干嘛不占呢.点击查看使用CDN的好处,点此查看一些主流的jQuery CDN地址. <script t ...

  2. C++学习基础十——子类构造函数与析构函数的执行

    1.子类构造函数的执行: 先执行父类的构造函数,再执行成员对象的构造函数,最后执行自身的构造函数. 当继承多个类时,构造函数的 执行顺序与继承时的顺序 相同,而与子类构造函数调用父类构造函数的顺序无关 ...

  3. git 放弃本地修改 强制更新

    git reset --hard origin/master

  4. 一天天的sql总结

    一. 多张表之间的查询: join/inner join  on inner join 是比较运算符,只返回符合条件的行. left/outer join on 左外连接包含left join左表所有 ...

  5. maven 打包 spring 项目

    在程序中使用到了springframework控件(主要是为了使用Mybatis-spring操作数据库,省事). 使用maven管理项目的构建,现在需要生成一个jar包,包含所有依赖的jar包,并可 ...

  6. zip和zippartition总结

    最近在搞spark机器学习项目时,碰到了一些小问题.两个数据集要联到一起.比如rdd1=("abc","cde","dfc"),rdd2=( ...

  7. 快速升级php5.6

    !!yum list installed | grep phpcd /etc/yum.repos.drpm -Uvh https://mirror.webtatic.com/yum/el6/lates ...

  8. python 排序

    python 写的排序,实现起来还是比较简单 #快速排序 def qsort(L): if len(L)>1: return qsort([i for i in L[1:] if i<L[ ...

  9. 【BootStrap】 基础

    [BootStrap] 基础 一. 自适应(针对不同设备如手机平板笔电,使页面的宽度适应设备宽度) <meta name="viewport" content="w ...

  10. jdk1.6 反射性能对比

    ReflectPerformance.java package aaa.bbb.ccc; import java.lang.reflect.Method; public class ReflectPe ...