POJ2240——Arbitrage(Floyd算法变形)
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算法变形)的更多相关文章
- Uvaoj 10048 - Audiophobia(Floyd算法变形)
1 /* 题目大意: 从一个点到达另一个点有多条路径,求这多条路经中最大噪音值的最小值! . 思路:最多有100个点,然后又是多次查询,想都不用想,Floyd算法走起! */ #include< ...
- 图论——Floyd算法拓展及其动规本质
一.Floyd算法本质 首先,关于Floyd算法: Floyd-Warshall算法是一种在具有正或负边缘权重(但没有负周期)的加权图中找到最短路径的算法.算法的单个执行将找到所有顶点对之间的最短路径 ...
- HDOJ 1217 Arbitrage(拟最短路,floyd算法)
Arbitrage Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- floyd算法小结
floyd算法是被大家熟知的最短路算法之一,利用动态规划的思想,f[i][j]记录i到j之间的最短距离,时间复杂度为O(n^3),虽然时间复杂度较高,但是由于可以处理其他相似的问题,有着广泛的应用,这 ...
- [图论]Floyd 算法小结
Floyd 算法小结 By Wine93 2013.11 1. Floyd算法简介 Floyd算法利用动态规划思想可以求出任意2点间的最短路径,时间复杂度为O(n^3),对于稠密图, 效率要高于执行 ...
- 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法
图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...
- 7-8 哈利·波特的考试(25 分)(图的最短路径Floyd算法)
7-8 哈利·波特的考试(25 分) 哈利·波特要考试了,他需要你的帮助.这门课学的是用魔咒将一种动物变成另一种动物的本事.例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等.反方向变 ...
- 最短路径之Floyd算法
Floyd算法又称弗洛伊德算法,也叫做Floyd's algorithm,Roy–Warshall algorithm,Roy–Floyd algorithm, WFI algorithm. Floy ...
- 最短路径—Dijkstra算法和Floyd算法
原文链接:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html 最后边附有我根据文中Dijkstra算法的描述使用jav ...
随机推荐
- UML静态类图
0,主要分为类.接口.协作.关系,这四种元素.作用:a,显示类.接口以及他们之间的静态结构和关系:b,用于描述系统的结构化设计. 1,类 CStudent +m_strName : string +S ...
- JS 框架
<html xmlns=”http://www.w3.org/1999/xhtml”> <head> <title>Untitled Page</title& ...
- Unity3D设置字体颜色大小,用于游戏分数显示设置等,
最近在学unity3d,慢慢的学会了许多unity的东西,今天记录下unity3d的Label字体大小及颜色的代码,下面是显示游戏中分数的代码,, public static int Score = ...
- 关于apache Alias斜杠/的实验
1.Alias /icons/ "D:/wamp/bin/apache/Apache2.2.17/icons/" 访问http://localhost/icons/正常,访问htt ...
- Eclipse中将项目导出jar包,以及转化成exe的方法
Eclipse中将项目导出jar包,以及转化成exe的方法 http://wenku.baidu.com/view/385597f07c1cfad6195fa7c6.html
- SQL正常工作日上班安排
alter proc [work] as declare @i int begin id into #restdate from dt_work where work_date in (select ...
- 通用SQL存储过程分页以及asp.net后台调用
创建表格并添加300万数据 use Stored CREATE TABLE UserInfo( --创建表 id ,) PRIMARY KEY not null,--添加主键和标识列 UserName ...
- C# Json数据反序列化为Dictionary并根据关键字获取指定值
Json数据: { "dataSet": { "header": { "returnCode": "0", " ...
- c++,C# 转换
//C++中的DLL函数原型为 //extern "C" __declspec(dllexport) bool 方法名一(const char* 变量名1, unsi ...
- PHP网页的工作原理
网络基本概念 IP地址 唯一标识网络上的主机或设备. IP地址是由四段8位二进制构成,中间用小数点隔开.如:192.168.18.70 每一段取值0-255的十进制. 特殊的IP地址:127.0.0. ...