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. python 购物车小程序(列表、循环、条件语句)

    goods = [ ['iphone6s', 5800], ['mac book', 9000], ['coffee', 32], ['python book', 80], ['bicyle', 15 ...

  2. (转)5个Xcode开发调试技巧

    1.Enable NSZombie Objects(开启僵尸对象) Enable NSZombie Objects可能是整个Xcode开发环境中最有用的调试技巧.这个技巧非常非常容易追踪到重复释放的问 ...

  3. Atrenta电话面试(C++研发工程师)

    1.代码量是多少,你负责哪一块,工作量占%几,改进了什么   2.c++ 和 c 的 区别   3.list 和 vector 的 适用条件   4.hash_map 和 map 的 区别 , 使用h ...

  4. 15,re正则表达式

    判断手机号是否合法. phone_number = input('请输入手机号:') if re.match('^(13|14|15|18)[0-9]{9}$',phone_number): prin ...

  5. Python第三方库之openpyxl(7)

    Python第三方库之openpyxl(7) 散点图 散点或xy图表类似于一些折线图.主要的区别在于,一个系列的值被绘制在另一个值上.当值未排序时,这是有用的. from openpyxl impor ...

  6. poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 43315   Accepted: 14296 Ca ...

  7. HDU-3065 病毒侵袭持续中 AC自动机又是一板子!

    病毒侵袭持续中 上一题是求出现多少病毒输出病毒序号,而这题输出每个病毒出现的次数.这题有字典树基础都能做出来,把叶子节点用相应的编号标记起来,匹配的时候遍历到叶子节点用一个数组把次数存起来就行了. 有 ...

  8. ajax 原生 post

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. BZOJ 2286 [Sdoi2011]消耗战 ——虚树

    虚树第一题. 大概就是建一颗只与询问有关的更小的新树,然后在虚树上DP #include <map> #include <ctime> #include <cmath&g ...

  10. vector容器中添加和删除元素

    添加元素: 方法一: insert() 插入元素到Vector中 iterator insert( iterator loc, const TYPE &val ); //在指定位置loc前插入 ...