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 ...
随机推荐
- javascript 返回数组中不重复的元素
这是实现结构伪类type-of-type的部分代码: <script type="text/javascript"> var ret= ["span" ...
- shopex 网店系统安装教程
centos上配置shopex环境(LNMP) 安装包地址: http://download.csdn.net/detail/nanmu1258/9109297 软件默认下载至在/opt/local ...
- [linux]BASH 的基本语法
最简单的例子 -- Hello World! 关于输入.输出和错误输出 BASH 中对变量的规定(与 C 语言的异同) BASH 中的基本流程控制语法 函数的使用 2.1 最简单的例子 -- ...
- ajax同步,异步
传输数据 同步:第一条传过去,对方接收到反馈以后,再传第二条; 异步:第一条传过去,不管对方有没有收到,继续传第二条. ajax(默认异步 async:true) 同步:执行完ajax内部代码,才执行 ...
- startActivityForResult用法详解
一.如果想在Activity中得到新打开Activity 关闭后返回的数据,需要使用系统提供的startActivityForResult(Intent intent, int requestCode ...
- 解决tableView分割线左边不到边的情况
//解决tableView分割线左边不到边的情况// if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {// ...
- phpstorm laravel单元测试 配置
laravel中集成了单元测试工具phpunit可以在项目的根目录下进行使用,命令是:phpunti ./tests/单元测试文件名称.在phpstorm中使用phpunit需要做一些配置,指定com ...
- 【1-4】jQuery代码风格-导航栏
实现一个导航栏,单机不同的商品名称链接,显示相应的内容,同时高亮显示当前选择的商品. 实现功能如图: css: /* reset */ ;padding:0 0 12px 0;font-size:12 ...
- 【Properties文件】Java使用Properties来读取配置文件
配置文件位置及内容 执行结果 程序代码 package Utils.ConfigFile; import java.io.BufferedInputStream; import java.io.B ...
- bzoj3905: Square
Description Nothing is more beautiful than square! So, given a grid of cells, each cell being black ...