POJ-1860 Currency Exchange( Bellman_Ford, 正环 )

Posted 「空白」物語

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ-1860 Currency Exchange( 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 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.

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<=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.

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 }

 

以上是关于POJ-1860 Currency Exchange( Bellman_Ford, 正环 )的主要内容,如果未能解决你的问题,请参考以下文章

POJ-1860 Currency Exchange spfa判负环

POJ——T1860 Currency Exchange

POJ 1860 Currency Exchange

POJ 1860 Currency Exchange (最短路)

poj1860 Currency Exchange(spfa判断正环)

POJ1860 Currency Exchange