POJ1860 Currency Exchange —— spfa求正环

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

 

Currency Exchange
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 32128   Accepted: 12228

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

Source

Northeastern Europe 2001, Northern Subregion
 
 
 
题解:
当能够最终换回初始货币,并且获利时,满足以下两种情况之一:
1.用spfa求出了正环,并且初始点在环内。此种情况理所当然地符合条件。
2.用spfa求出了正环,然而初始点不在环内。那么:可以在这个环内兜转无数次,赚了无数钱,然后再花费一定的代价换回到初始货币(不管代价多大,由于赚的钱是无限的,所以最终还是会获利)。
综上:只要能求出正环,就能获利。
 
 
 
代码如下:
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <vector>
  6 #include <cmath>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 #define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
 13 #define ms(a,b) memset((a),(b),sizeof((a)))
 14 using namespace std;
 15 typedef long long LL;
 16 const double EPS = 1e-8;
 17 const int INF = 2e9;
 18 const LL LNF = 9e18;
 19 const int MOD = 1e9+7;
 20 const int MAXN = 1e3+10;
 21 
 22 int n, m,X;
 23 double V;
 24 
 25 struct edge
 26 {
 27     double r, c;
 28     int to,next;
 29 }edge[MAXN*MAXN];
 30 int cnt, head[MAXN];
 31 
 32 void addedge(int u, int v, double r, double c)
 33 {
 34     edge[cnt].to = v;
 35     edge[cnt].r = r;
 36     edge[cnt].c = c;
 37     edge[cnt].next = head[u];
 38     head[u] = cnt++;
 39 }
 40 
 41 void init()
 42 {
 43     cnt = 0;
 44     memset(head, -1, sizeof(head));
 45 }
 46 
 47 double dis[MAXN];
 48 int times[MAXN], inq[MAXN];
 49 bool spfa(int st)
 50 {
 51     memset(inq, 0, sizeof(inq));
 52     memset(times, 0, sizeof(times));
 53     for(int i = 1; i<=n; i++)
 54         dis[i] = -INF;
 55 
 56     queue<int>Q;
 57     Q.push(st);
 58     inq[st] = 1;
 59     dis[st] = V;
 60     while(!Q.empty())
 61     {
 62         int u = Q.front();
 63         Q.pop(); inq[u] = 0;
 64         for(int i = head[u]; i!=-1; i = edge[i].next)
 65         {
 66             int v = edge[i].to;
 67             if(dis[v]< (dis[u]-edge[i].c)*edge[i].r )
 68             {
 69                 dis[v] = (dis[u]-edge[i].c)*edge[i].r;
 70                 if(!inq[v])
 71                 {
 72                     Q.push(v);
 73                     inq[v] = 1;
 74                     if(++times[v]>n) return true;
 75                 }
 76             }
 77         }
 78     }
 79     return false;
 80 }
 81 
 82 int main()
 83 {
 84     while(scanf("%d%d%d%lf",&n,&m,&X, &V)!=EOF)
 85     {
 86         init();
 87         for(int i = 1; i<=m; i++)
 88         {
 89             int u, v;
 90             double r1,c1,r2,c2;
 91             scanf("%d%d%lf%lf%lf%lf", &u, &v, &r1,&c1,&r2,&c2);
 92             addedge(u,v,r1,c1);
 93             addedge(v,u,r2,c2);
 94         }
 95         if(spfa(X))
 96            puts("YES");
 97         else
 98             puts("NO");
 99     }
100 }
View Code

 

posted on 2017-09-28 19:43  h_z_cong  阅读(330)  评论(0编辑  收藏  举报

导航