poj 2240 Arbitrage bellman-ford算法
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 13434 | Accepted: 5657 |
Description
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 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
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
题目大意:给你几种货币的名字,还有货币之间的汇率,问你是否通过兑换使货币数量增加
bellmanford或者spfa计算是否有负圈回路
#include<stdio.h>
#include<string>
#include<map>
#include<iostream>
using namespace std;
double g[31][31];
double dis[31];
int n;
bool bellman()
{
int i, j, k;
for(i = 0; i < 31; i++)
dis[i] = 1;
for(i = 1; i < n; i++)
{
for(j = 1; j <= n; j++)
{
for(k = 1; k <= n; k++)
{
if(dis[k] < dis[j] * g[j][k])
dis[k] = dis[j] * g[j][k];
}
}
}
for(j = 1; j <= n; j++)
{
for(k = 1; k <= n; k++)
{
if(dis[k] < dis[j] * g[j][k])
return 0;
}
}
return 1;
}
int main()
{
int m, t = 1;
while(scanf("%d", &n), n != 0)
{
int i;
string str;
map<string, int> ma;
for(i = 1; i <= n; i++)
{
cin>>str;
ma[str] = i;
}
scanf("%d", &m);
double rate;
string str2;
while(m--)
{
cin>>str>>rate>>str2;
g[ma[str]][ma[str2]] = rate;
}
if(bellman() == 0)
printf("Case %d: Yes\n", t);
else
printf("Case %d: No\n", t);
ma.clear();
t++;
}
return 0;
}
poj 2240 Arbitrage bellman-ford算法的更多相关文章
- POJ 2240 Arbitrage(Floyed-Warshall算法)
题意:给出n种货币,m种兑换比率(一种货币兑换为另一种货币的比率),判断测试用例中套汇是否可行.(套汇的意思就是在经过一系列的货币兑换之后,是否可以获利.例如:货币i→货币j→货币i,这样兑换后,是否 ...
- poj 2240 Arbitrage 题解
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21300 Accepted: 9079 Descri ...
- 最短路(Floyd_Warshall) POJ 2240 Arbitrage
题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...
- POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)
POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...
- Bellman—Ford算法思想
---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...
- Bellman - Ford 算法解决最短路径问题
Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...
- poj 2240 Arbitrage (Floyd)
链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...
- POJ 2240 Arbitrage (Bellman Ford判正环)
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions:27167 Accepted: 11440 Descri ...
- POJ 2240 Arbitrage【Bellman_ford坑】
链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
随机推荐
- 解决JQuery中datatables设置隐藏显示列多次提交后台刷新数据的问题
此次项目开发过程中用到了Jquery的Datatables插件,无疑他是数据列表展示,解决MVC中同步过程中先走控制器后返回视图,查询数据过程中无法提示等待的弊端, 而且他所提供的各种方法也都有较强的 ...
- [mysql] mysqldump 导出数据库表
1.mysqldump的几种常用方法: (1)导出整个数据库(包括数据库中的数据) mysqldump -u username -p dbname > dbname.sql (2)导出数据库结构 ...
- smarty函数-继承extents
继承<{extends}> {extends}标签用在模版中的第一行: 如果子模板用{extends}标签继承父模板,那么它只能包含{block}标签(内容),其它任何模板内容都将忽略: ...
- CentOS下的网络配置文件说明
CentOS网络配置主要涉及到以下四个文件: 1./etc/sysconfig/network 在CentOS官方网站上给出了如下说明: The/etc/sysconfig/networkfile i ...
- Partition does not end on cylinder boundary
fdisk可能会报该消息. DOS/Win95要求分区对齐到cyclinder boundary以适应CHS寻址. 然而现代OS都使用LBA寻址. 使用c命令关闭DOS兼容性检查. 使用u命令切换分区 ...
- Linux命令详解nice
[命令]nice — 调整程序运行的优先级 [格式]nice [OPTION] [command [arguments...]] [说明] 在当前程序运行优先级基础之上调整指定值得到新的程序运行优先级 ...
- txt用Itunes同步到IPhone上
纯水的LGF160s换了IPhone 5,想把原来txt的文件拷到手机上.百度只是有老版本的,最新也是11的.现在用12.06版的,菜单已经不太一样.找了半天,分享一下.
- 从SVN导出指定版本号之间修改的文件
当一个网站项目进入运营维护阶段以后,不会再频繁地更新全部源文件到服务器,这个时间的修改大多是局部的,因此更新文件只需更新修改过的文件,其他 没有修改过的文件就没有必要上载到服务器.但一个稍微上规模的网 ...
- 【WP之一】]独立存储
介绍: 提供一个磁盘存储空间,他是一种虚拟的文件系统,能存储小量的数据:在默认的情况下,它只能存储1MB的文件.根据使用方式及功能的不同,独立存储空间又包含两部分:独立设置存储和独立文件存储.除非卸载 ...
- 补一篇关于Jackson和Gson的文章
一.关于Gson的问题 问题1. 如果对象属性里有[],表示它是一个列表,需要用List对象进行封装,不能用String来定义,不然转不了 问题2. Gson在解析的时候,如果json中有转义字符 \ ...