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 ...
随机推荐
- 【转】纯CSS设置Checkbox复选框控件的样式
Checkbox复选框是一个可能每一个网站都在使用的HTML元素,但大多数人并不给它们设置样式,所以在绝大多数网站它们看起来是一样的.为什么不把你的网站中的Checkbox设置一个与众不同的样式,甚至 ...
- java中的循环
while循环: while(循环条件){ 循环操作 } 停止while循环有两种方式:1.不再满足while后的循环条件时,循环终止: ...
- 【Reporting Services 报表开发】— 级联式参数设置
级联式参数设置 再清楚的菜单,只要遇到选择项目一多的时候,难免会让人眼花缭乱,而找不到该选的选项.举例来说,像是零售业动辄万种商品品类,如果希望快速的选择到希望查看的产品品类时,就需要更有效率的搜索方 ...
- webApi文档好帮手-apidoc使用教程
来源:http://blog.csdn.net/xumin198908/article/details/41964159 在开发后台接口的过程中,我们肯定要提供一份api接口文档给终端app. 目前大 ...
- Php检测文件编码方法
<?php /** * 检测文件编码 * @param string $file 文件路径 * @return string|null 返回 编码名 或 null */ function det ...
- python简要
python用冒号代替{}开启语句块 /usr/bin/python 加在脚本的头部, ./脚本 help("str") : 查看命令帮助 '''三引号可以打印换行字符串 prin ...
- shell脚本摘要
开启监听端口的程序时,查看是否开启成功(该例子监听8983端口) #动态显示[|][/][-][\] function spinner() { local pid=$1 local delay=0.5 ...
- Spark1.6 DataSets简介
Apache Spark提供了强大的API,以便使开发者为使用复杂的分析成为了可能.通过引入SparkSQL,让开发者可以使用这些高级API接口来从事结构化数据的工作(例如数据库表,JSON文件),并 ...
- NYOJ16 矩形嵌套(DAG最长路)
矩形嵌套 紫书P262 这是有向无环图DAG(Directed Acyclic Graph)上的动态规划,是DAG最长路问题 [题目链接]NYOJ16-矩形嵌套 [题目类型]DAG上的dp & ...
- NeHe OpenGL教程 第四十三课:FreeType库
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...