Arbitrage

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

题目大意:

    有N种货币,给出了其中一些货币的汇率。eg:(A 2 B) 代表一块A能换两块B,但不代表两块B能换一块A(有向!)

    判断是否可以通过货币之间的转换来赚钱。样例一中的1单位USDollar可以通过转换变为1.05单位的USDollar,赚钱了!

解题思路:

    Floyd算法的变形,Edge[i][j]=max(Edge[i][j],Edge[i][m]*Edge[m][j])

    初始化时将Edge[i][i]=1,Floyd之后通过判断Edge[i][i]是否大于1,来判断是否可以挣钱!

Code:

 #include<string>
#include<iostream>
#include<stdio.h>
#include<cstring>
#define MAXN 100
using namespace std;
double edge[MAXN+][MAXN+];
string name[MAXN+];
int N;
void floyd()
{
for (int m=; m<=N; m++)
for (int i=; i<=N; i++)
for (int j=; j<=N; j++)
if (edge[i][m]!=INT_MAX&&edge[m][j]!=INT_MAX&& //Floyd的变形公式
edge[i][j]<edge[i][m]*edge[m][j])
edge[i][j]=edge[i][m]*edge[m][j];
}
int main()
{
int M,times=;
while (cin>>N)
{
times++;
for (int i=; i<=N; i++)
for (int j=; j<=N; j++)
if (i!=j) edge[i][j]=INT_MIN;
else edge[i][j]=;
if (N==) break;
for (int i=; i<=N; i++)
cin>>name[i];
cin>>M;
for (int i=; i<=M; i++)
{
string tmp2,tmp1;
double dis;
cin>>tmp1>>dis>>tmp2;
int x1,x2;
for (x1=; x1<=N; x1++) //将字符串转换成对应的标号
if (name[x1]==tmp1) break;
for (x2=; x2<=N; x2++)
if (name[x2]==tmp2) break;
edge[x1][x2]=dis;
}
floyd();
int cnt=;
for (int i=;i<=N;i++)
if (edge[i][i]>) //判断是否能通过各种转化挣钱
cnt++;
if (cnt>=) printf("Case %d: Yes\n",times);
else printf("Case %d: No\n",times);
}
return ;
}

POJ2240——Arbitrage(Floyd算法变形)的更多相关文章

  1. Uvaoj 10048 - Audiophobia(Floyd算法变形)

    1 /* 题目大意: 从一个点到达另一个点有多条路径,求这多条路经中最大噪音值的最小值! . 思路:最多有100个点,然后又是多次查询,想都不用想,Floyd算法走起! */ #include< ...

  2. 图论——Floyd算法拓展及其动规本质

    一.Floyd算法本质 首先,关于Floyd算法: Floyd-Warshall算法是一种在具有正或负边缘权重(但没有负周期)的加权图中找到最短路径的算法.算法的单个执行将找到所有顶点对之间的最短路径 ...

  3. HDOJ 1217 Arbitrage(拟最短路,floyd算法)

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

  4. floyd算法小结

    floyd算法是被大家熟知的最短路算法之一,利用动态规划的思想,f[i][j]记录i到j之间的最短距离,时间复杂度为O(n^3),虽然时间复杂度较高,但是由于可以处理其他相似的问题,有着广泛的应用,这 ...

  5. [图论]Floyd 算法小结

    Floyd 算法小结  By Wine93 2013.11 1. Floyd算法简介 Floyd算法利用动态规划思想可以求出任意2点间的最短路径,时间复杂度为O(n^3),对于稠密图, 效率要高于执行 ...

  6. 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法

    图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...

  7. 7-8 哈利·波特的考试(25 分)(图的最短路径Floyd算法)

    7-8 哈利·波特的考试(25 分) 哈利·波特要考试了,他需要你的帮助.这门课学的是用魔咒将一种动物变成另一种动物的本事.例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等.反方向变 ...

  8. 最短路径之Floyd算法

    Floyd算法又称弗洛伊德算法,也叫做Floyd's algorithm,Roy–Warshall algorithm,Roy–Floyd algorithm, WFI algorithm. Floy ...

  9. 最短路径—Dijkstra算法和Floyd算法

    原文链接:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html 最后边附有我根据文中Dijkstra算法的描述使用jav ...

随机推荐

  1. 【转载】学习C++和编程的几个要点

    1.把C++当成一门新的语言学习(和C没啥关系!真的.):2.看<ThinkingIn C++>,不要看<C++变成死相>:3.看<The C++ Programming ...

  2. urllib2.urlopen超时

    urllib2.urlopen设置超时,可以解决read方法阻塞的问题 urllib2.urlopen(‘xxx’,timeout=30)

  3. Poj 2109 / OpenJudge 2109 Power of Cryptography

    1.Link: http://poj.org/problem?id=2109 http://bailian.openjudge.cn/practice/2109/ 2.Content: Power o ...

  4. Ubuntu 14.04下java开发环境的搭建--2--Eclipse的安装

    前面说了JDK的安装,http://www.cnblogs.com/bcsflilong/p/4196536.html 下面我们来安装Eclipse! 安装Eclipse 的前提是,你的JDK已经安装 ...

  5. Hibernate的单向OneToMany、单向ManyToOne

    单向OneToMany 一个用户有多张照片,User----->Images是一对多关系,在数据库中Images维护一个外键useid 1.在映射关系的主控方Image这边,我们什么都不做.(为 ...

  6. sqlite3简单使用

    下载SQLite3 地址:http://www.sqlite.org/download.html 下载好的文档是SQlite3.exe,假如放在D盘. cmd D: D:\>SQlite3.ex ...

  7. DTcms手机版使用余额支付 提示信息跳转到PC版的错误。以及提交订单不打开新页面

    手机版使用余额支付 提示信息跳转到PC版的错误 引起错误的原因是中间需要提交到DTcms.Web\api\payment\balance\index.aspx去处理 导致BasePage.cs中的li ...

  8. [大牛翻译系列]Hadoop系列性能部分完结

    Hadoop系列性能部分完结.其它的部分发布时间待定. Hadoop系列将不再一日一篇,开始不定期发布.

  9. phpstorm 自定义函数配置

    phpstorm 自定义函数配置 打开设置->活动模板->

  10. wordpress在Linux nginx下权限设置

    1.wordpress 权限对安装和使用效果的影响很大:权限错误将影响theme的安装:不能安装theme或者修改theme或删除theme. 相关设置:  chmod 755 wordpress f ...