【做练习】Currency Exchange(Bellman-Ford算法判定负环)

本文介绍了如何运用Bellman-Ford算法来解决一个货币兑换问题,判断是否可以通过兑换操作增加资本。题目给出了N种货币、M个兑换点,每种兑换涉及两种货币之间的汇率和佣金。通过分析证明,存在负回路才能增加资本,并提供了算法的思路和代码实现。
摘要由CSDN通过智能技术生成

题目

总时间限制: 1000ms 内存限制: 65536kB

描述

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR.
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real RAB, CAB, RBA and CBA - exchange rates and commissions when exchanging A to B and B to A respectively.
Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations.

输入

The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=103.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102.
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 104.

输出

If Nick can increase his wealth, output YES, in other case output NO to the output file.

样例输入

3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00

样例输出

YES

题目大意

有N种货币,M个兑换点。
每个兑换点能在两种不同货币A,B之间交换。兑换要支付佣金,兑换的规则是:目标货币金额 = (原货币金额 - 佣金) * 兑换率。每个兑换点具有各自的R_AB,C_AB,R_BA,C_BA参数,分别是A到B、B到A的兑率和佣金。
现在,已知Nick拥有某一种货币S指定的数额V,他希望通过兑换薅到羊毛。我们要输出他能否通过兑换(最终仍然换为货币S),并使得他拥有的金额增加。


分析

可以证明,能薅到羊毛,当且仅当兑换存在负回路,也就是一条回路使得金额增加。(之所以称为“负回路”是因为一般来说我们研究的是距离问题,距离越短越好,只不过这里是钱是越多越好)
必要性是显而易见的,薅羊毛本身就是把S换成别的再换成S并增加了资金,所以这必然是有负回路的。充分性:如果存在负回路,我们先把货币换到这个回路上的一种货币,然后沿着这个回路不停交换,最后我们的钱甚至能趋于无限大,肯定是可以薅到羊毛的。

现在,我们问题就转换为负回路问题。我们考虑BellmanFord算法或SPFA算法啊。这两种算法可以用来解决含负权的有源最短路问题,也可以用来判断负回路的存在性,只不过我们这里稍微修改一下,从“最短路”变成了“最大金额”而已。

我选择使用BellmanFord算法。虽然SPFA理论上是一种更优化的算法,但此题我们是存连边的(一个兑换点,相当于一来一回两条边)。如果我们存的是每个节点的邻接表,那么SPFA更好用。

Bellman-Ford算法

首先我们按照动态规划的角度去思考。在一个有权图中,我们用 D [ k ] [ i ] D[k][i] D[k][i]为经过最多 k k k条边,从源点到节点 i i i的最短路。我们有以下递推方程:
D [ k + 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值