博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2240——Arbitrage(Bellman-Ford算法)
阅读量:2343 次
发布时间:2019-05-10

本文共 2580 字,大约阅读时间需要 8 分钟。

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

和poj1860类似的题,找权值为正的环路。就是节点的处理有些技巧,我这里用了哈希表

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 5005#define mod 1000000007using namespace std;map
mp;int n,m,e;double dis[MAXN];struct Node{ int beg,end; double r;};Node edge[MAXN<<1];void add(int b,int en,double r){ edge[e].beg=b; edge[e].end=en; edge[e].r=r; e++;}bool relax(int p){ double t=dis[edge[p].beg]*edge[p].r; if(dis[edge[p].end]
1) return true; if(!flag) return false; } for(int j=0; j
>a; mp[a]=i; } scanf("%d",&m); double r; e=0; while(m--) { cin>>a>>r>>b; add(mp[a],mp[b],r); } if(bellman()) printf("Case %d: Yes\n",cnt++); else printf("Case %d: No\n",cnt++); } return 0;}

转载地址:http://iscvb.baihongyu.com/

你可能感兴趣的文章
Ubuntu下apt-get与pip安装命令的区别
查看>>
linux CMakeLists.txt 语法
查看>>
cmake 简介
查看>>
CMake学习笔记(1)——用CMake编译一个hello world程序
查看>>
cmake使用总结---工程主目录CMakeList文件编写
查看>>
CMake学习之路
查看>>
cmake学习笔记6-catkin的CmakeList.txt讲解
查看>>
cmake手册详解
查看>>
Maplab框架介绍(一)
查看>>
Maplab开源VI-SLAM框架介绍
查看>>
maplab(1):安装
查看>>
陀螺仪随机误差的Allan方差分析
查看>>
Ubuntu 64位安装Adobe Reader 9.5.5
查看>>
Ubuntu 下如何查看已安装的软件
查看>>
Linux 系统下可以注释标注的pdf阅读器安装、比较和推荐
查看>>
福昕阅读器foxit reader Linux版
查看>>
Ubuntu 安装百度云客户端
查看>>
每天一个linux命令:locate
查看>>
Linux 环境下载百度云资源,Firefox插件(百度网盘助手)
查看>>
ubuntu Firefox/chrome adobe flash 插件安装
查看>>