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