Arbitrage

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5679    Accepted Submission(s): 2630

Problem 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 file 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
 
 
 
题意:      套汇是指利用不同外汇市场的外汇差价。在某一外汇市场上买进某种货币,同一时候在还有一外汇市场上卖出该种货币。以赚取利润。这样的利润称之为套利。

比方1美元能够买0.5英镑。而1英镑能够买10法郎,1法郎能够买0.21美元那么可用通过套汇使用1美元买到1.05美元,套利是存在的。以下给出各个货币的种类和名称,再给出一些货币兑换的汇率。请问是否存在套利?

 
解题思路:能够看做最短路问题,只是权值的计算是乘而不是相加。而与小于1的权值相乘会导致权值减小,即存在负权值问题。
 
所以此题不能用dijkstra算法,此题能够用floyd算法。
 
 
代码例如以下:
 
<span style="font-size:12px;">#include<cstdio>
#include<cstring>
#define maxn 32
double map[maxn][maxn];
int n,t; void floyd()
{
int i,j,k;
for(k=0;k<n;++k)
{
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
if(map[i][j]<map[i][k]*map[k][j])
map[i][j]=map[i][k]*map[k][j];
}
}
}
int sign=0;
for(i=0;i<n;++i)
{
if(map[i][i]>1)//自身的汇率为1。若汇率大于1则说明能套汇
{
sign=1;
break;
}
}
if(sign)
printf("Yes\n");
else
printf("No\n");
} int main()
{
int m,i,j,a,b,t=1;;
double c;
char str[32][32],s1[32],s2[32];
while(scanf("%d",&n)&&n)
{
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
map[i][j]=0;
}
for(i=0;i<n;++i)
scanf("%s",str[i]);
scanf("%d",&m);
while(m--)
{
scanf("%s%lf%s",s1,&c,s2);
for(i=0;i<n;++i)
{
if(strcmp(s1,str[i])==0)
a=i;
if(strcmp(s2,str[i])==0)
b=i;
}
map[a][b]=c;
}
printf("Case %d: ",t++);
floyd();
}
return 0;
}</span>

HDOJ 1217 Arbitrage(拟最短路,floyd算法)的更多相关文章

  1. HDOJ 1217 Arbitrage (最短路)

    题意:每两种货币之间都有不同的汇率  如果换回自己最后是赚的 输出Yes 否则是No 因为最多只有三十种货币 所以用Floyd是可行的 与一般的最短路板子不同的地方 汇率是要乘而不是加 如果乘上一个小 ...

  2. 【ACM程序设计】求短路 Floyd算法

    最短路 floyd算法 floyd是一个基于贪心思维和动态规划思维的计算所有点到所有点的最短距离的算法. P57-图-8.Floyd算法_哔哩哔哩_bilibili 对于每个顶点v,和任一顶点对(i, ...

  3. 最短路--floyd算法模板

    floyd算法是求所有点之间的最短路的,复杂度O(n3)代码简单是最大特色 #include<stdio.h> #include<string.h> ; const int I ...

  4. 多源最短路Floyd 算法————matlab实现

    弗洛伊德(Floyd)算法是一种用于寻找给定的加权图中顶点间最短路径的算法.该算法名称以创始人之一.1978年图灵奖获得者.斯坦福大学计算机科学系教授罗伯特·弗洛伊德命名. 基本思想 通过Floyd计 ...

  5. 最短路 - floyd算法

    floyd算法是多源最短路算法 也就是说,floyd可以一次跑出所以点两两之间的最短路 floyd类似动态规划 如下图: 用橙色表示边权,蓝色表示最短路 求最短路的流程是这样的: 先把点1到其他点的最 ...

  6. HDU 2066 最短路floyd算法+优化

    http://acm.hdu.edu.cn/showproblem.php?pid=206 题意 从任意一个邻居家出发 到达任意一个终点的 最小距离 解析 求多源最短路 我想到的是Floyd算法 但是 ...

  7. 【POJ - 3259】Wormholes(最短路 Floyd算法)

    Wormholes 题目描述 教学楼里有很多教室,这些教室由双向走廊连接.另外,还存在一些单向的秘密通道,通过它们可以回到过去.现在有 N (1 ≤ N ≤ 500) 个教室,编号 1..N, M ( ...

  8. 【Aizu - 0189】Convenient Location (最短路 Floyd算法)

    Convenient Location 直接翻译了 Descriptions 明年毕业的A为就业而搬家.就职的公司在若干城市都有办公室,不同天出勤的办公室也不同.所以A在考虑住在哪去各个办公室的时长最 ...

  9. 洛谷 P1119 灾后重建 最短路+Floyd算法

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 总结 题面 题目链接 P1119 灾后重建 题目描述 B地区在地震过后,所有村 ...

随机推荐

  1. The US in understimating Huawei, says founder Ren zhengfei

    Huawei Founder Ren Zhengfei has downplayed the impact of the US executive order that cripple Huawei' ...

  2. stm32开发套件选择——LL SPL HAL Snippets的应用范围

  3. oracle sequence的用法

    在oracle中sequence就是序号,每次取的时候它会自动增加.sequence与表没有关系. 1.Create Sequence     首先要有CREATE SEQUENCE或者CREATE ...

  4. 原 jmeter中类似lr的场景设置

    有一天如果你们领导来一句给我测下这个首页到底能扛多少并发,并发量极限是多少,这时你不要慌不要忙,拿出jmeter神器,设置下场景,目标设置成1000,每10秒启动100个并发,等着看什么时候系统响应开 ...

  5. python 中zip()函数的使用

    zip(*iterables)函数的定义: zip()函数的对象Iterables,iterables可以有多个参数(元组,列表等可迭代对象)组成.通过zip()函数返回一组元组数据,每个元组中的第i ...

  6. 九度oj 题目1447:最短路

    题目描述: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线 ...

  7. Cache技术――OSCache(转-全)

    OSCache使用指南 一.下载安装 OSCache是一个基于web应用的组件,他的安装工作主要是对web应用进行配置,大概的步骤如下: 1. 下载.解压缩OSCache 从http://www.op ...

  8. 玩转css样式选择器----当父元素有多个子元素时选中第一个

  9. 激活win10系统的方法(亲测)

    WIN+X 按A (或者点击左下角有个windows小图标“鼠标右键”选择选择“命令提示符号(管理员)”) 输入下面命令,回车(一行按一个回车键)slmgr.vbs /upkslmgr /ipk W2 ...

  10. sugar与阿龙的互怼(第一季)

    §   第一季 回家风波 高考了,啦啦啦~ 快要高考了,显然sugar很伤心. 显然不是因为快要考试了sugar才伤心的. 那为什么??? 因为他们都回家了,但是sugar和他的小伙伴们都不回家!!! ...