点击打开链接

Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13434   Accepted: 5657

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

题目大意:给你几种货币的名字,还有货币之间的汇率,问你是否通过兑换使货币数量增加

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算法的更多相关文章

  1. POJ 2240 Arbitrage(Floyed-Warshall算法)

    题意:给出n种货币,m种兑换比率(一种货币兑换为另一种货币的比率),判断测试用例中套汇是否可行.(套汇的意思就是在经过一系列的货币兑换之后,是否可以获利.例如:货币i→货币j→货币i,这样兑换后,是否 ...

  2. poj 2240 Arbitrage 题解

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 Descri ...

  3. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

    题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...

  4. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  5. Bellman—Ford算法思想

    ---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...

  6. Bellman - Ford 算法解决最短路径问题

    Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...

  7. poj 2240 Arbitrage (Floyd)

    链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...

  8. POJ 2240 Arbitrage (Bellman Ford判正环)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:27167   Accepted: 11440 Descri ...

  9. POJ 2240 Arbitrage【Bellman_ford坑】

    链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

随机推荐

  1. 由浅入深探究mysql索引结构原理、性能分析与优化 转

    第一部分:基础知识 第二部分:MYISAM和INNODB索引结构 1. 简单介绍B-tree B+ tree树 2. MyisAM索引结构 3. Annode索引结构 4. MyisAM索引与Inno ...

  2. 数据库表转换成javaBean对象小工具

    package test.utils; import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter; ...

  3. DBA常用SQL之数据库基础信息

    第一部分: 1. 查看oracle最大连接数 sql>show parameter processes #最大连接数 2. 修改最大连接数 sql>alter system set pro ...

  4. 拖动控件 javascript原生,兼容IE6-11、chrome、firefox、Opera、Safari

    鼠标拖动元素,对于初学者来说,是一个很难的话题,其实只要应用好事件,就能很好的控制拖动的对象,其主要事件是 mousedown,mousemove,mouseup,其原理是在鼠标点击元素时,在给定鼠标 ...

  5. 【NCDC数据】获取 hadoop权威指南3中的NCDC数据

    vi getNcdcBigData.sh 内容如下: #!/bin/bash for i in {1901..2014} do cd /home/xxxx/hapood/ncdc wget --exe ...

  6. 51nod 1348 乘积之和

    用(r-l+2)维向量f[l,r]表示区间[l,r]内选i个数(0<=i<=r-l+1)相乘的所有方案之和,可以发现f[l,r]=f[l,m]*f[m+1,r],题目模数100003较小, ...

  7. Windows蓝屏后产生的.dmp分析原因

    Windows系统电脑出现蓝屏后都会自动重启,重启后电脑屏幕会提示蓝屏的相关信息,此时如果你没有来得及查看,你也可以进入windows7的“事件查看器”(位置为:控制面板--系统和安全--管理工具-- ...

  8. android学习笔记14——GridView、ImageSwitcher

    GridView--网格视图.ImageSwitcher--图像切换器 ==> GridView,用于在界面上按行.列的分布形式显示多个组件:GridView和ListView父类相同——Abs ...

  9. ARM地址映射

    转自:http://blog.csdn.net/a3163504123/article/details/10958229 重映射之后,一般原来的地址依然有效.也就是说,可能两个地址,对应一个存储单元. ...

  10. sbt设置

    配置中央仓库 vim ~/.sbt/repositories [repositories] local osc: http://maven.oschina.net/content/groups/pub ...