博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【POJ 2240】Arbitrage
阅读量:5317 次
发布时间:2019-06-14

本文共 2873 字,大约阅读时间需要 9 分钟。

Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18408   Accepted: 7796

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

3USDollarBritishPoundFrenchFranc3USDollar 0.5 BritishPoundBritishPound 10.0 FrenchFrancFrenchFranc 0.21 USDollar3USDollarBritishPoundFrenchFranc6USDollar 0.5 BritishPoundUSDollar 4.9 FrenchFrancBritishPound 10.0 FrenchFrancBritishPound 1.99 USDollarFrenchFranc 0.09 BritishPoundFrenchFranc 0.19 USDollar0

Sample Output

Case 1: YesCase 2: No

Source

 
可以用Floyd过。
需要注意的是,他是求一个像环一样的最长路径。
所以这里是和普通的Floyd有所不同,但本质上还是传递闭包。
 
#include 
#include
#include
#include
#include
using namespace std;map
ID;double dis[35][35];int n, m;int main(){ int kase = 0; while (scanf("%d", &n) == 1 && n) { string s, s1; ID.clear(); for (int i = 0; i < n; ++i) { cin >> s; ID[s] = i; } for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) dis[i][j] = 1.0; scanf("%d", &m); for (int i = 0; i < m; ++i) { double x; cin >> s >> x >> s1; int id1 = ID[s], id2 = ID[s1]; dis[id1][id2] = x; } for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) dis[i][j] = max(dis[i][j], dis[i][k] * dis[k][j]); bool flag = true; printf("Case %d: ", ++kase); for (int i = 0; i < n; ++i) if (dis[i][i] > 1) { flag = false; printf("Yes\n"); break; } if (flag) printf("No\n"); } return 0;}

 

转载于:https://www.cnblogs.com/albert7xie/p/4905518.html

你可能感兴趣的文章
python升级安装后的yum的修复
查看>>
Vim配置Node.js开发工具
查看>>
web前端面试题2017
查看>>
ELMAH——可插拔错误日志工具
查看>>
MySQL学习笔记(四)
查看>>
【Crash Course Psychology】2. Research & Experimentation笔记
查看>>
两数和
查看>>
移动设备和SharePoint 2013 - 第3部分:推送通知
查看>>
SOPC Builder中SystemID
查看>>
MySQL数据库备份工具mysqldump的使用(转)
查看>>
NTP服务器配置
查看>>
【转】OO无双的blocking/non-blocking执行时刻
查看>>
关于 linux 的 limit 的设置
查看>>
HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)
查看>>
vim中文帮助教程
查看>>
MySQL基础3
查看>>
云计算数据与信息安全防护
查看>>
RxJS & Angular
查看>>
面向对象(多异常的声明与处理)
查看>>
MTK笔记
查看>>