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. ZigBee绑定细节

    ZigBee中的绑定由APS层来管理,除了绑定表管理外,APS层还有组表管理.快速地址查找等服务功能.应用层不能直接调用APS层中的数据服务来传输数据,只能通过AF层封装的AD_DataRequest ...

  2. SQL Server 收缩事务日志的方法

    由于SQL2008对文件和日志管理进行了优化,所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消:(SQL2005) Backup Log DataBaseName with no_ ...

  3. doctype的种类

    现实生活中人们使用的浏览器是各式各样的!为了防止各各浏览器页面设置不一样而照成我们所做的网页乱序,不得不设置doctye(文档类型):doctype有3中类型:* 过渡的(Transitional): ...

  4. 获取客户端访问的ip地址

    function real_ip() { static $realip = NULL; if ($realip !== NULL) { return $realip; } if (isset($_SE ...

  5. html lang

    目前,语言的标签表示法的国际标准是RFC 4646,名称是<Tags for Identifying Languages>.简单说,这个文件规定,一种语言的标签应该按照如下方式排列: la ...

  6. C# window service的创建

    其实我也是第一次在博客园写博客,看到那些高手说自己要多动手写博客,于是乎自己也尝试尝试. 废话不多说.这几天在研究window service,通过查找各种大神写的博客,我终于成功的自己写出来了. 下 ...

  7. PAT乙级真题1006. 换个格式输出整数 (15)(解题)

    原题: 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为它有2个 ...

  8. 2014年辛星完全解读Javascript第三节

    经过第一节的入门和第二节的运算符,那么接下来我们就可以学习Javascript的函数了,当然了,不管大家之前学习的是什么编程语言,都会有函数的概念,如果大家学的是Pascal,还会有”过程“的概念,但 ...

  9. Mysql grant权限管理

    MySQL 赋予用户权限命令的简单格式可概括为: grant 权限 on 数据库对象 to 用户 [identified by '密码'] 最常用的,弄主从同步的时候,给从库的slave用户设置拥有所 ...

  10. 移动端webapp开发必备知识

    移动设备的用户越来越多,每天android手机的激活量都已经超过130万台,所以我们面向移动终端的WebAPP也开始跟进了.本文主要介绍webapp的开发与调试的相关知识和经验,以及给出几种可选的解决 ...