POJ1860
Currency Exchange
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 14080 | Accepted: 4842 |
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.
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.
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
思路:N个点,然后之间可以进行相应的货币的兑换。从货币A->B兑换,首先需要缴纳CAB元A货币,然后按照RAB的兑换率进行兑换。即:兑换后后:(A-CAB)*RAB;问经过若干次兑换后,将货币兑换成开始的货币,问是否能够增加开始的货币。
此题可以利用SPFA算法。但是此处求的是最大值,所以松弛操作需要修改一下,因为以往的松弛操作都是求最小值。当经过松弛后如果兑换回A的货币数比开始多,则输出YES,如果某一个点经过N次松弛操作后,仍然可以松弛,则可以进行无限次的松弛下去,得到无限多的某一种货币,然后无限多的某种货币再兑换成开始的货币是,肯定也会是无限多的。故也会比开始的时候的货币数多。故也输出YES。否则输出NO。
1 #include <cstdlib> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <cctype> 7 #include <vector> 8 #include <queue> 9 #define MIND 0E0 10 11 12 13 using namespace std; 14 15 16 struct Node{ 17 int v; 18 double rate; 19 double c; 20 }; 21 22 vector<Node>vt[100+4]; 23 vector<Node>revt[100+4]; 24 25 queue<int>q; 26 27 28 int counter[100+4]; 29 30 31 32 int main(int argc, char *argv[]) 33 { 34 35 36 37 int n,m,s; 38 double v; 39 40 scanf("%d%d%d%lf",&n,&m,&s,&v); 41 int i,j,k; 42 43 for(i=1;i<=n;i++) 44 {revt[i].clear();vt[i].clear();} 45 46 for(i=1;i<=n;i++) 47 counter[i]=0; 48 49 50 51 for(i=1;i<=m;i++) 52 { 53 int a,b; 54 double rab,cab,rba,cba; 55 scanf("%d%d",&a,&b); 56 scanf("%lf%lf%lf%lf",&rab,&cab,&rba,&cba); 57 Node tmp; 58 59 tmp.v=b; 60 tmp.rate=rab; 61 tmp.c=cab; 62 vt[a].push_back(tmp); 63 64 tmp.v=a; 65 tmp.rate=rba; 66 tmp.c=cba; 67 vt[b].push_back(tmp); 68 69 70 tmp.v=a; 71 tmp.rate=rab; 72 tmp.c=cab; 73 revt[b].push_back(tmp); 74 75 tmp.v=b; 76 tmp.rate=rba; 77 tmp.c=cba; 78 revt[b].push_back(tmp); 79 80 81 82 } 83 84 85 86 87 int flag=0; 88 89 90 91 double cost[100+4]; 92 93 for(i=1;i<=n;i++) 94 {cost[i]=MIND;} 95 cost[s]=v; 96 q.push(s); 97 98 99 while(!q.empty()) 100 {int x=q.front(); 101 q.pop(); 102 103 104 vector<Node>::iterator it; 105 for(it=vt[x].begin();it!=vt[x].end();it++) 106 {if(cost[it->v]<(cost[x]-it->c)*it->rate) 107 {cost[it->v]=(cost[x]-it->c)*it->rate;counter[it->v]++;q.push(it->v); 108 109 if(counter[it->v]>n) 110 {flag=1;break;} 111 } 112 } 113 114 115 } 116 117 118 if(cost[s]>v) 119 flag=1; 120 121 /*if(flag==1) 122 cout<<"YES"<<endl; 123 else 124 cout<<"NO"<<endl; 125 */ 126 127 if(flag==1) 128 printf("YES\n"); 129 else 130 printf("NO\n"); 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 //system("PAUSE"); 148 return EXIT_SUCCESS; 149 }