Written by razrlele
17:04 August 18, 2014
Currency Exchange
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 23824 | Accepted: 8620 |
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.
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}.
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
1 2 3 |
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
1 |
YES |
Source
Northeastern Europe 2001, Northern Subregion
初次接触Bellman-Ford算法, 这里就两个关键点:
1. 每轮松弛操作有E次(即边的个数).
2. 最短路径都是简单路径, 所以最终路径只会有V-1条边, 故松弛操作最多只有V-1轮.
这道题里面只是Bellman-Ford算法的一个运用, 因为真正求的是最大值, 而不是最小值, 故需要把松弛条件改一下.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
/************************************************************************* > File Name: poj1860.cpp > Author: razrLeLe > Personal homepage: https://razrLeLe.com > Created Time: Mon 18 Aug 2014 01:58:44 PM CST ************************************************************************/ #include<iostream> using namespace std; #include<cstdio> #include<string.h> double dis[110]; struct point { int a, b; double rate, commission; }p[500]; int main() { int flag; bool ok = false; int N, M, S; double V; int c; int A, B; double rab, cab, rba, cba; while(~scanf("%d%d%d%lf", &N, &M, &S, &V))//注意这道题目可能有多组输入 { c = 0; flag = 0; for(int i = 0; i < M; i++) { scanf("%d%d%lf%lf%lf%lf", &A, &B, &rab, &cab, &rba, &cba); //cout << c<< endl; p[c].a = A; p[c].b = B; p[c].rate = rab; p[c++].commission = cab; p[c].a = B; p[c].b = A; p[c].rate = rba; p[c++].commission = cba; } //Bellman-Ford memset(dis, 0, sizeof(dis)); dis[S] = V; //relax for(int i = 1; i <= N-1; i++)//松弛操作 { ok = false;//这个是用来优化算法的, 从32ms到0ms for(int j = 0; j < c; j++) { int a = p[j].a, b = p[j].b; double r = p[j].rate, co = p[j].commission; if(dis[b] < (dis[a] - co)*r) { dis[b] = (dis[a] - co)*r; ok = true; // cout << b << " " << dis[b] << endl; } } if(!ok) break; } for(int i = 0; i < c; i++) { if(dis[p[i].b] < (dis[p[i].a] - p[i].commission)*p[i].rate) { flag = 1; break; } } if(flag) { printf("YESn"); } else { printf("NOn"); } } return 0; } |