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. ASP.NET MVC中如何在当前页面上弹出另外一个页面

    注意:不是链接到另一个页面,而是弹出一个页面,当前的页面和弹出页面都存在于浏览器的同一个标签页中,效果如图: 弹出的窗体置于四大天王页面之上,但是无法继续操作底层的页面,代码如下: function ...

  2. Repo command reference

    Repo command reference In this document init sync upload diff download forall prune start status Rep ...

  3. jQuery DOM 互转

    jQuery对象与DOM对象是不一样的 通过一个简单的例子,简单区分下jQuery对象与DOM对象: <p id=”imooc”></p> 我们要获取页面上这个id为imooc ...

  4. 转:深入 AngularUI Router

    原文地址:http://www.ng-newsletter.com/posts/angular-ui-router.html ui-router: https://angular-ui.github. ...

  5. wp8.1 sdk preview 预览版

    http://pan.baidu.com/s/1hqyusja?qq-pf-to=pcqq.c2c#dir/path=%2FWPSDK%208.1%20DevPreview%20Installerwp ...

  6. mysql replication常见错误整理

    这篇文章旨在记录MySQL Replication的常见错误,包括自己工作中遇到的与网友在工作中遇到的,方面自己及别人以后进行查找.每个案例都是通过Last_IO_Errno/Last_IO_Erro ...

  7. ActionProxy相关实现类

    package com.opensymphony.xwork2; import com.opensymphony.xwork2.config.Configuration; import com.ope ...

  8. 九度oj 题目1398:移动次数

    题目描述: 众所周知JOBDU旗下的JOBBALA公司是一家以个性.亲民著称的IT公司.在JOBBALA公司成立50周年的日子里,公司CEO组织全体员工登山旅游.按照往常的习惯,导游通常要求游客按照身 ...

  9. 【Android】监听viewpager子页面里面的Button按钮

    最近做项目遇到Viewpager+Fragment滑动页面,要监听子页面中的按钮,在网上查了些解决办法: 办法一: 这种方法是在适配器初始化中进行监听,有人亲测通过,但是我继承FragmentPage ...

  10. 好未来谢华亮:AI 在教育行业中的应用

    11 月 23 日,在以「AI 产业技术的渗透与融合」为主题的 NIUDAY 北京站中,好未来 SEG 智慧教育事业部技术总监谢华亮为大家带来了关于「AI 在教育行业中的应用」的分享. 本文是对分享内 ...