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. 【转】Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败

    错误原因如下: Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot ...

  2. OpenStack:安装Keystone

    >安装Keystone1. 安装# apt-get install keystone2. 创建dbcreate database keystone;grant all privileges on ...

  3. ubuntu14.04字符界面中文乱码及中文输入

    作为ubuntu用户字符界面是绝对不陌生的,尤其是维护管理服务器的朋友为了节省资源都是用的字符界面,但是默认字符界面中文目录文件都是乱码,根本无法打开编辑,那么怎么让字符界面显示中文目录文件,还有在字 ...

  4. iOS开发之Pch预编译文件的创建

    在Xcode6之前,创建一个新工程xcode会在Supporting files文件夹下面自动创建一个“工程名-Prefix.pch”文件,也是一个头文件,pch头文件的内容能被项目中的其他所有源文件 ...

  5. SQL 执行顺序

    SQL 是一种声明式语言,与其他语言相比它的最大特点是执行顺序-并非按照语法顺序来执行.因此很多程序猿看到SQL就头疼,我之前也是这样,后来看到一篇文章后豁然开朗-地址. 理解了SQL的执行顺序无疑对 ...

  6. [转]安装openoffice,并且配置为windows服务

    [转]安装openoffice,并且配置为windows服务 http://blog.csdn.net/zzzz3621/article/details/18400277 下载windows reso ...

  7. mozilla css developer center

    https://developer.mozilla.org/en-US/docs/Web/CSS

  8. WPF——数据绑定(一)什么是数据绑定

    注意:本人初学WPF,文中可能有表达或者技术性问题,欢迎指正!谢谢! 一:什么是数据绑定? “Windows Presentation Foundation (WPF) 数据绑定为应用程序提供了一种简 ...

  9. 四则运算之C++版

    一.设计思想 之前的版本是用Java语言实现的,在这次的练习中,我用C++语言将其功能逐一实现,其实C++与Java有很多相似之处,只是一些书写格式不同,思路还是一样的. 二.源代码 #include ...

  10. 新 四则运算题目 C++

    源代码: #include <stdlib.h>#include <iostream.h>#include <conio.h>#include <time.h ...