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 ...
随机推荐
- android 添加依赖的库文件
Notpad: 2016-3-16: 1.android 添加依赖的库文件 右键自己的项目 -> properties ->android ->在Library处点击add -> ...
- PHP5.3以上版本没有libmysql.dll,以及由此带来的困扰
有朋友下载了PHP5.3,PHP5.4版本想加载mysql支持的时候发现没有libmysql.dll文件,无法完成mysql配置,其实PHP5.3版本开始,使用mysqlnd库,不再使用libmysq ...
- mysql5.7.9 源码安装 (转)
1,安装所有包 yum -y install gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgc ...
- .git 目录文件介绍
$>tree -L 1.|-- HEAD # 这个git项目当前处在哪个分支里|-- config # 项目的配置信息,git config命令会改动它|-- des ...
- linux ubuntu系统下,adb不是内部命令 (如何才能让adb命令可以使用)
linux ubuntu系统下,adb不是内部命令 原文地址 linux ubuntu系统下,adb不是内部命令 解决方法: 1.sudo gedit ~/.bashrc 2.将下面的两句加到上面打开 ...
- [mysql] ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.
今天安装mysql遇到这样一个问题: ERROR 1862 (HY000): Your password has expired. To log in you must change it using ...
- DataGridView 添加行号
private void dataGridViewX1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //Dat ...
- ASP.NET网页验证码常用方法
验证码生产类 using System; using System.Data; using System.Configuration; using System.Web; using System.W ...
- 打开PDF文件弹出阅读未加标签文档的解决方法
在“高级”菜单的“辅助工具”选中“设置助手”,然后点选“设置屏幕阅读器选项”,下一步之后,将“忽略已加标签文档的阅读顺序”和“添加标签到文档之前进行确认”(有的版本显示的是“为文档加标签前确认”)前面 ...
- RMQ问题ST算法 (还需要进一步完善)
/* RMQ(Range Minimum/Maximum Query)问题: RMQ问题是求给定区间中的最值问题.当然,最简单的算法是O(n)的,但是对于查询次数很多(设置多大100万次),O(n)的 ...