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. PAT Basic 1010

    1010 一元多项式求导 设计函数求一元多项式的导数.(注:x^n^(n为整数)的一阶导数为n*x^n-1^.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数). ...

  2. Struts2之初体验

    Struts21.了解Struts2 请求调度框架Struts2是一个MVC框架Struts2类库:Struts2-core Struts2核心XWork-core xwork核心 Struts2的构 ...

  3. java 中 image 和 byte[] 相互转换

      java 中 image 和 byte[] 相互转换可恶的…………其实也挺好的 只是把好不容易写出来的东西记下来,怕忘了…… 下面,我来介绍一个简单的 byte[] to image, 我们只需要 ...

  4. 【01】let和const命令

    let和const命令   魔芋总结: 01,let声明变量,只在代码块{}内有效. 02,不存在变量提升,只能先声明,再使用.否则报错. 03,暂时性死区 如果代码块中存在let和const声明的变 ...

  5. mysql数据库修改字段类型

    修改字段类型: alter table 表名 modify column 字段名字 decimal(18, 4) ;

  6. PHP cannoy modify header information - headers already sent by ....

    我采用的是MVC模式的写法,代码和html分离的写法 <?php require '../mysql_connect.php'; require('../model/functions.php' ...

  7. BNU OJ 1027 金币系统

    金币系统 Time Limit: 1000ms Memory Limit: 65535KB   64-bit integer IO format: %lld      Java class name: ...

  8. python--getitem一拦截索引运算

    getitem一拦截索引运算 __getitem__方法拦截实例的索引运算.当实例x出现在x[i]这样的索引运算中时,Python会调用这个实例继承的__getitem__方法(如果有的话),把x作为 ...

  9. Masonry练习

    tableView的cell自动适应,scrollview自动适应,自定义自动布局控件 demo链接:http://pan.baidu.com/s/1jHsrGwQ

  10. 刷题总结——系列维护(ssoi)

    题目: 题解: 题解如上图,至于计算大于s的数字的数量和小于s数字的和用权值线段树或者树状数组维护就行了···注意离散化 另外发现cout和puts比printf快好多····· 代码: #inclu ...