Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17374   Accepted: 7312

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
这题较简单,使用bellman-ford算法就可以了,注意输出,我因为输出WA几次
 #include <iostream>
#include<map>
#include<string.h>
using namespace std;
struct edge{
int u,v;
float rate;
} e[*];
int cur_num,edge_num;
float dis[];
map<string,int> mp;
int Bellman_ford(int c){
memset(dis,,*sizeof(float));
dis[c]=1.0;
for(int i=;i<cur_num;i++){
for(int j=;j<edge_num;j++){
if(dis[e[j].v]<dis[e[j].u]*e[j].rate){
dis[e[j].v]=dis[e[j].u]*e[j].rate;
}
}
}
if(dis[c]>1.0)
return ;
else
return ;
}
int main() {
int count=;
cin>>cur_num;
while(cur_num){
mp.clear();
for(int i=;i<cur_num;i++){
string s;
cin>>s;
mp[s]=i;
}
cin>>edge_num;
for(int i=;i<edge_num;i++){
string s1,s2;
float rate;
cin>>s1>>rate>>s2;
e[i].u=mp[s1];
e[i].v=mp[s2];
e[i].rate=rate;
}
int flag=;
for(int i=;i<cur_num;i++){
flag=Bellman_ford(i);
if(flag)
break;
} if(flag)
cout<<"Case "<<++count<<": Yes"<<endl;
else
cout<<"Case "<<++count<<": No"<<endl;
cin>>cur_num;
}
return ;
}

Arbitrage - poj 2240 (Bellman-ford)的更多相关文章

  1. Arbitrage POJ - 2240

    题目链接:https://vjudge.net/problem/POJ-2240 思路:判正环,Bellman-ford和SPFA,floyd都可以,有正环就可以套利. 这里用SPFA,就是个板子题吧 ...

  2. poj 2240 Arbitrage 题解

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

  3. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

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

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

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

  5. poj 2240 Arbitrage (Floyd)

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

  6. ACM/ICPC 之 最短路径-Bellman Ford范例(POJ1556-POJ2240)

    两道Bellman Ford解最短路的范例,Bellman Ford只是一种最短路的方法,两道都可以用dijkstra, SPFA做. Bellman Ford解法是将每条边遍历一次,遍历一次所有边可 ...

  7. poj1860 bellman—ford队列优化 Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22123   Accepted: 799 ...

  8. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  9. Bellman—Ford算法思想

    ---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...

随机推荐

  1. Java面向对象内测

    功能要求 开发基于控制台的试题信息管理系统.具体要求如下: (1)显示试题信息管理系统主菜单,包括: 1)列出所有试题信息 2)按科目查询 3)按题干查询 4)添加试题 5)删除试题 6)退出系统 p ...

  2. 记录在Spring-Boot中使用Fegin调用RESTfull的PATCH方法设置

    使用了ZooKeeper,设置 spring.cloud.zookeeper.dependency.headers.enabled=false 参考: https://github.com/sprin ...

  3. Eclipse常用小知识汇总

    原文:http://blog.csdn.net/jinzhencs/article/details/50462370 1.修改注释 自动出来的author

  4. A folder failed to be renamed or moved--安装Android SDK的问题

    对于Android是一直想学却一直未学,行动跟不上想法.现在,终于付诸于行动了. 首先,我找的第一个Android的资料是大话企业级Android,前阵子刚看完大话设计模式,通俗易懂,还是比较喜欢这一 ...

  5. Java 堆内存模型

    堆内存 Java 中的堆是 JVM 所管理的最大的一块内存空间,主要用于存放各种类的实例对象. 在 Java 中.堆被划分成两个不同的区域:新生代 ( Young ).老年代 ( Old ).新生代 ...

  6. Python开发easy忽略的问题

    这篇文章主要介绍了Python程序猿代码编写时应该避免的17个"坑",也能够说成Python程序猿代码编写时应该避免的17个问题,须要的朋友能够參考下 一.不要使用可变对象作为函数 ...

  7. PHP面试题遇到的几个坑。...面壁ing

    1.指针悬挂问题 $array = [1, 2, 3]; echo implode(',', $array), "\n"; foreach ($array as &$val ...

  8. 【ecshop---新增包邮卡功能】

    一:需求分析 项目组要求新增类似虚拟卡的包邮卡,用户获得包邮卡的方式包括后台发送和前台自助充值.包邮卡有使用期限.订单使用包邮卡免除邮费.可以和其他优惠活动同时进行! 二:开发功能点 后台:新增包邮卡 ...

  9. 服务器和java程序的桥梁--jdbc/hibernate

    现实的应用程序都是用户通过可视化界面发出指令从而修改数据库.本篇文章以Oracle为例,模拟怎么通过java代码实现数据库的增删改查. 新建一个Java项目,要建好桥梁,首先要拷入驱动Jar包放在项目 ...

  10. android:Cordova Android, hello Cordova ,PhoneGap android

    文章来自:http://blog.csdn.net/intbird 官方文档: http://cordova.apache.org/docs/en/5.0.0//index.html intbird的 ...