Arbitrage

http://acm.hdu.edu.cn/showproblem.php?pid=1217

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
 

解题思路:将货币种类的名字转化为数字,用其作为货币相互转换的下标,然后用Floyd算出货币之间转换后的最多货币,再寻找自己转换为自己大于1的情况,如果有就输出Yes, 没有就输出No

解题代码:

 // File Name: Arbitrage 1217.cpp
// Author: sheng
// Created Time: 2013年07月19日 星期五 15时57分20秒 #include <math.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <map>
using namespace std; const int max_n = ;
map<string, int> k; double cas[max_n][max_n]; int main()
{
string str1, str2;
int n, m, T = ;
while (scanf("%d", &n) != EOF && n)
{
k.clear();
memset (cas, , sizeof (cas));
for (int i = ; i <= n; i ++)
{
cin >> str1;
k[str1] = i;//转换为数字
}
scanf ("%d", &m);
while (m --)
{
double temp;
cin >> str1;
cin >> temp;
cin >> str2;
cas[k[str1]][k[str2]] = temp;
}
for (int i = ; i <= n; i ++)
for (int j = ; j <= n; j ++)
for (int k = ; k <= n; k ++)
{
if (cas[j][k] < cas[j][i] * cas[i][k])
cas[j][k] = cas[j][i] * cas[i][k];
}
int i;
for (i = ; i <= n; i ++)
if (cas[i][i] > )
{
printf ("Case %d: Yes\n", T++);
break;
}
if (i > n)
printf ("Case %d: No\n", T++);
}
return ;
}

HDU 1217 Arbitrage (Floyd)的更多相关文章

  1. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  2. hdu 1217 (Floyd变形)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)   ...

  3. hdu 1217 Arbitrage (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1217 /************************************************* ...

  4. HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 Brit ...

  5. HDU 1217 Arbitrage(Floyd的应用)

    给出一些国家之间的汇率,看看能否从中发现某些肮脏的......朋友交易. 这是Floyd的应用,dp思想,每次都选取最大值,最后看看自己跟自己的.....交易是否大于一.... #include< ...

  6. hdu 1217 Arbitrage (spfa算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个ST ...

  7. hdu 1217 Arbitrage(佛洛依德)

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

  8. hdu 1217 Arbitrage

    Flody多源最短路 #include<cstdio> #include<cstring> #include<string> #include<cmath&g ...

  9. hdu 1217 汇率 Floyd

    题意:给几个国家,然后给这些国家之间的汇率.判断能否通过这些汇率差进行套利交易. Floyd的算法可以求出任意两点间的最短路径,最后比较本国与本国的汇率差,如果大于1,则可以.否则不可以. 有向图 一 ...

随机推荐

  1. Mysql允许外网接入

    首先你可以为mysql创建一个账户,或者为root用户接入数据库. 授权用户指定所有主机以指定用户连接服务器 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDE ...

  2. hdu 5443 The Water Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Description In Land waterless, ...

  3. golang面向对象初识

    struct是变量的集合 interface是方法的集合 struct与interface都支持匿名字段, 换言之, 支持组合实现继承. golang的struct与C++的class一样, 只能声明 ...

  4. kafka中server.properties配置文件参数说明

    转自:http://blog.csdn.net/lizhitao/article/details/25667831 参数 说明(解释) broker.id =0 每一个broker在集群中的唯一表示, ...

  5. vs2010 开发过程中调试时 有错误不中断

    出现这个的原因是调试的设置有问题,修改下设置就好了. 修改方法:点击上边工具栏中   调试--异常 出现以下页面,把引发那列复选框全部勾上,点击确定就好了.

  6. mif_maker2010.exe下载和使用说明

    mif_malker2010.exe下载地址:http://pan.baidu.com/s/1bCqAp4 使用说明:http://www.cnblogs.com/BitArt/archive/201 ...

  7. wire与reg的区别?转载大神!

    本文转自:http://www.cnblogs.com/thymon/archive/2010/06/09/1754541.html //------------------------------- ...

  8. 微软职位内部推荐-Senior SDE

    微软近期Open的职位: Position: Senior SDE-- Mobile Products Android/iOS/WP Senior Developer Contact Person: ...

  9. Netsharp快速入门(之7) 基础档案(工作区1 向导创建工作区)

    作者:秋时 杨昶   时间:2014-02-15  转载须说明出处 3.5     商品开发 3.5.1  创建部件工作区 3.5.1.1 工作区向导 1.打开平台工具,选择界面管理节点下的部件工作区 ...

  10. 前端之JavaScript第三天学习(7)-JavaScript-数据类型

    字符串.数字.布尔.数组.对象.Null.Undefined JavaScript 拥有动态类型 JavaScript 拥有动态类型.这意味着相同的变量可用作不同的类型: 实例 var x      ...