POJ-1860 Currency Exchange( Bellman_Ford, 正环 )

本文介绍了一道关于货币兑换的问题,利用Bellman-Ford算法的变种来判断是否可以通过一系列兑换操作使得初始资金增加。该问题涉及到图论中的负环检测,并针对实际情况进行了调整。

题目链接:http://poj.org/problem?id=1860

Description

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 R AB, C AB, R BA and C BA - 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.

Input

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<=10 3.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2.
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 10 4.

Output

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

Sample Input

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

Sample Output

YES

题目大意:有若干种货币,部分可以互相兑换,兑换时满足题目所给公式。现告诉你有几种货币,几种兑换方式,以及Nick所拥有的货币种类及其金额,问你能否通过若干次兑换后,兑回当前货币且金额增加,兑换过程中不能出现负值
解题思路:Bellman_Ford的变种,只需要将判负环的条件改为判正环即可,在进行松弛操作时,由于不能出现负值,将初始权值赋为0

 1 #include<iostream>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<iomanip>
 6 #include<map>
 7 
 8 using namespace std;
 9 
10 typedef struct Edge{
11     int beg, end;
12     double r, c;
13 }Edge;
14 
15 int n, m, s, edges;
16 double v, dis[105];
17 Edge edge[205];
18 
19 void setedge( int beg, int end, double r, double c ){
20     edges++;
21     edge[edges].beg = beg;
22     edge[edges].end = end;
23     edge[edges].r = r;
24     edge[edges].c = c;
25 }
26 
27 bool relax( int beg, int end, double r, double c ){
28     if( ( dis[beg] - c ) * r > dis[end] ){
29         dis[end] = ( dis[beg] - c ) * r;
30         return true;
31     }
32     return false;
33 }
34 
35 bool Bellman_Ford(){
36     for( int t = 1; t < n; t++ ){
37         for( int i = 1; i <= edges; i++ ){
38             relax( edge[i].beg, edge[i].end, edge[i].r, edge[i].c );
39         }
40     }
41 
42     for( int i = 1; i <= edges; i++ ){
43         if( relax( edge[i].beg, edge[i].end, edge[i].r, edge[i].c ) )
44             return false;
45     }
46 
47     return true;
48 }
49 
50 int main(){
51     ios::sync_with_stdio( false );
52 
53     while( cin >> n >> m >> s >> v ){
54         edges = 0;
55         int beg, end;
56         double r1, c1, r2, c2;
57         while( m-- ){
58             cin >> beg >> end >> r1 >> c1 >> r2 >> c2;
59             setedge( beg, end, r1, c1 );
60             setedge( end, beg, r2, c2 );
61         }
62         memset( dis, 0, sizeof( dis ) );
63         dis[s] = v;
64         if( Bellman_Ford() )
65             cout << "NO" << endl;
66         else cout << "YES" << endl;
67     }
68 
69     return 0;
70 }
 
 

 


转载于:https://www.cnblogs.com/hollowstory/p/5596129.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值