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. undrop for innodb c_parser 不完美之处

    今天发现c_parser导出数据是会丢掉某些行,给过调试发现是他处理utf8编码时计算有误,目前还没有发现自动解决总是的方法,只会手动改代码来解决. 下一步计划把c_parser移植到windows下 ...

  2. 04_例子讲解:rlViewDemo.exe

    参考资料:http://www.roboticslibrary.org/tutorials/first-steps-windows 使用rlViewDemo对应的快捷方式启动程序,可以看到如下界面: ...

  3. Ubuntu 设定壁纸自动切换的shell脚本

    升级到Ubuntu14.04后,感觉bug的确比12.04少多了.顶部任务栏支持半透明效果,所以整个桌面也看上去漂亮了很多.这样的桌面也是值得瞎捣鼓一下的,想到换壁纸,但是没找到设定动态更换壁纸的选项 ...

  4. Hadoop示例程序WordCount编译运行

    首先确保Hadoop已正确安装及运行. 将WordCount.java拷贝出来 $ cp ./src/examples/org/apache/hadoop/examples/WordCount.jav ...

  5. WeX5与阿里内测的Weex与有何纠葛?快来看HTML5开发圈那些逗逼事儿!

    4月21日~23日,由infoQ主办的2016 Qcon大会北京站如期举行. HTML5开发已经成为移动开发/前端专题中无可争议的焦点,核心议题已经由前几年的是否该用HTML5转向了如何高性能.高效率 ...

  6. 为云饰数据库添加Index

    Asset Collection: 1. _id_ 2. CategoryId_1_Date_-1 3. CategoryId_1_Id_1 4. CategoryId_1_Name_1 5. Cat ...

  7. mysql中limit的用法实例解析

    mysql中limit的用法解析. 在mysql中,select * from table limit m,n.其中m是指记录开始的index,从0开始,n是指从第m条开始,取n条. 例如: mysq ...

  8. IOS系统框架

    IOS系统框架:ios架构号MAC OS的基础框架相似:ios扮演底层硬件和应用程序的中介,底层为所有应用程序提供基础服务,高层则包含一些复杂巧妙的技术和服务,并且创建的应用程序不能直接访问硬件. C ...

  9. Oracle Lock 概述

    按锁的机制分类 排他锁( X ):如果事务T对对象A加上排他锁,则只允许T对A对象读取和修改,其他事务不能对A增加任何锁,直到T释放加载A上的排他锁 共享锁( S ):如果事务T对表A加上共享锁,则事 ...

  10. DB2分区表删除和添加分区

    1.数据库版本 2.具体procedure DROP PROCEDURE DB2USER.TOOLS_PARTITION_TABLE_SHOW (VARCHAR ()); )) /********** ...