Currency Exchange
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 22123 |
|
Accepted: 7990 |
Description
Several Currency exchange points is working. Let us suppose so each of the specializes in the particular currencies and performs exchange operations only with these C Urrencies. There can several points specializing in the same pair of currencies. Each point has its own exchange rates, and exchange rate of A to B is the quantity of B you get for 1A. Also each of the exchange point has some commission, the sum of which has a to pay for your exchange operation. Commission is all collected in source currency.
For example, if you want to exchange from US Dollars into Russian rubles at the exchange point, where the 29.75, and the Commission is 0.39 you'll get (100-0.39) * 29.75 = 2963.3975RUR.
You surely know that there is N different currencies you can deal with in our city. Let us assign the unique integer number from 1 through N to each currency. Then the exchange point can is described with 6 Numbers:integer A and b-numbers of currencies it exchanges, and real R AB, 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 has his money in currency S in the end. Help him to answer this difficult question. Nick must always has 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-th E Number of currency Nick has and v-the quantity of the currency units he has. The following M lines contain 6 numbers each-the description of the corresponding Exchange point-in specified above or Der Numbers is separated by one or more spaces. 1<=s<=n<=100, 1<=m<=100, V is real number, 0<=v<=103.
For each of the point exchange rates and commissions is real, given with in the most of the digits after the decimal point, 10-2<=ra te<=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. 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 be is 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.01 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00
Sample Output
YES
Source
Analytical
Test instructions
There are a variety of remittance, exchange between the currency can be exchanged, which requires a fee, when you use 100A currency exchange B currency, A to B exchange rate is 29.75, the handling fee is 0.39, then you can get (100-0.39) * 29.75 = 2963.3975 b currency. Ask if the amount of s currency can be increased by the amount of s currency that is eventually exchanged.
The exchange of money can be repeated several times, so we need to find out whether there is a positive loop, and the resulting s amount is increased
How to find the right loop? (Positive power loop: In this way, the weights of vertices can be continuously increased, which can be relaxed all the time)
Analysis:
A currency is a point
A "Redemption point" is a method of exchange between two currencies on the chart and is bilateral, but the exchange rate and handling fee for A to B may differ from the exchange rate and handling fee for B to a.
The only notable is the weight value, when the number of the owning currency A is V, the weight of a to a is k, i.e. no redemption
The weight of A to B is (V-cab) *rab
The topic is "seeking the maximum path", the reason is classified as "seeking the least path" is because the problem is exactly the same as the relaxation conditions of the Bellman-ford algorithm, is to be able to relax the maximum positive path, but still can use Bellman-ford thought to solve problems.
Thus initializing the DIS (S) =v and the distance (weights) of the source point to the other point is initialized to infinity (0), when the distance from S to some other point is constantly increasing, the maximum path is present, and if it can always be large, it indicates that there is a positive ring. To determine if there is a loop, use Bellman-ford and SPFA.
SPFA algorithm:
1#include <stdio.h>2#include <string.h>3#include <iostream>4#include <queue>5#include <algorithm>6 using namespacestd;7 Doublecost[ the][ the],rate[ the][ the];8 intn,vis[ the];9 Doublev,dis[ the];Ten BOOLBellman_ford (intstart) { Onememset (DIS,0,sizeof(DIS)); Amemset (Vis,0,sizeof(Vis)); -dis[start]=v; -queue<int>Q; the Q.push (start); -vis[start]=1; - while(!Q.empty ()) { - intx=Q.front (); + Q.pop (); -vis[x]=0; + for(intI=1; i<=n;i++){ A if(dis[i]< (Dis[x]-cost[x][i]) *Rate[x][i]) { atDis[i]= (Dis[x]-cost[x][i]) *Rate[x][i]; - if(dis[start]>v) - return true; - if(!Vis[i]) { - Q.push (i); -vis[i]=1; in } - } to } + } - return false; the } * intMain () { $ intm,s;Panax Notoginseng while(SCANF ("%D%D%D%LF", &n,&m,&s,&v)! =EOF) { -memset (Cost,0,sizeof(Vis)); thememset (Rate,0,sizeof(rate)); + A for(intI=1; i<=n;i++){ the for(intj=1; j<=n;j++) + if(i==j) -rate[i][j]=1.0; $ } $ intx, y; - DoubleRAB,RBA,CAB,CBA; - for(intI=1; i<=m;i++){ theCin>>x>>y>>rab>>cab>>rba>>CBA; -cost[x][y]=cab;Wuyicost[y][x]=CBA; therate[x][y]=Rab; -rate[y][x]=RBA; Wu } - if(Bellman_ford (s)) Aboutprintf"yes\n"); $ Elseprintf"no\n"); - } - return 0; - }
#include <stdio.h> #include <string.h> #include <iostream> #include <queue> #include < Algorithm>using namespace std;double cost[105][105],rate[105][105];int n,vis[105];d ouble v,dis[105];bool bellman_ Ford (int start) {memset (dis,0,sizeof (dis)); memset (vis,0,sizeof (VIS)); Dis[start]=v; queue<int>q; Q.push (start); Vis[start]=1; while (!q.empty ()) {int X=q.front (); Q.pop (); vis[x]=0; for (int i=1;i<=n;i++) {if (dis[i]< (Dis[x]-cost[x][i]) *rate[x][i]) {dis[i]= (dis[x]-cost[x][i]) *rat E[x][i]; if (DIS[START]>V) return true; if (!vis[i]) {Q.push (i); Vis[i]=1; }}}} return false;} int main () {int m,s; while (scanf ("%d%d%d%lf", &n,&m,&s,&v)!=eof) {memset (cost,0,sizeof (VIS)); memset (rate,0,sizeof (rate)); for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) if (i==j)rate[i][j]=1.0; } int x, y; Double RAB,RBA,CAB,CBA; for (int i=1;i<=m;i++) {cin>>x>>y>>rab>>cab>>rba>>cba; Cost[x][y]=cab; COST[Y][X]=CBA; Rate[x][y]=rab; Rate[y][x]=rba; } if (Bellman_ford (s)) printf ("yes\n"); else printf ("no\n"); } return 0;}
poj1860 bellman-ford Queue Optimization Currency Exchange