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. noip数论复习总结

    (上不了p站我要死了,侵权度娘背锅) 勉勉强强算是把数论复习的差不多了. 总结一下吧. 其实数论的知识大部分是结合在一起的,勉强分类总结 组合数 求法 组合数的求法根据不同情况选用不同的方法 2.3都 ...

  2. 【微信】微信小程序 微信开发工具 创建js文件报错 pages/module/module.js 出现脚本错误或者未正确调用 Page()

    创建报错pages/module/module.js 出现脚本错误或者未正确调用 Page() 解决方法: 在js文件中添加 Page({ })

  3. android intent 跳转

    转自:http://blog.sina.com.cn/s/blog_7309444701014u2d.html 一.不需要返回值的跳转 Intent intent=new Intent(); inte ...

  4. VUE -- ejs模板的书写

    1.EJS是一个简单高效的模板语言,通过数据和模板,可以生成HTML标记文本.可以说EJS是一个JavaScript库,EJS可以同时运行在客户端和服务器端,客户端安装直接引入文件即可,服务器端用np ...

  5. golang之archive/tar包的使用

    原文地址:http://www.niu12.com/article/36 github地址:https://github.com/ZQCard/go_api_practice // tar包实现了文件 ...

  6. Storm文档详解

    1.Storm基础概念 1.1.什么是storm? Apache Storm is a free and open source distributed realtime computation sy ...

  7. 我与小娜(36):人机大战第五局,AlphaGo必胜!

    我与小娜(36):人机大战第五局,AlphaGo必胜!       小娜知道,细致阅读论文"Mastering the game of Go with deep neural network ...

  8. 在Hadoop监控页面怎样查看Hive的完整SQL

    如图.这里仅仅能看到简单的一段SQL.差点儿看不出详细在运行什么任务. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFvemhhb2t1bg==/fo ...

  9. 完美拖拽 &&仿腾讯微博效果&& 自定义多级右键菜单

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

  10. Android学习(二十三)SubMenu 子菜单

    一.SubMenu子菜单 和功能菜单相似,但是可以添加子菜单. 二.实现步骤: 1.通过onCreateOptionsMenu方法创建子菜单,可以通过代码动态创建,也可以通过xml进行创建. 2.通过 ...