Currency Exchange
Time Limit : 2000/1000ms(Java/Other) MemoryLimit : 60000/30000K (Java/Other)
Total Submission(s) :1 AcceptedSubmission(s) : 1
Problem Description Several currency exchange points are working in our city. Let ussuppose that each point specializes in two particular currenciesand performs exchange operations only with these currencies. Therecan be several points specializing in the same pair of currencies.Each point has its own exchange rates, exchange rate of A to B isthe quantity of B you get for 1A. Also each exchange point has somecommission, the sum you have to pay for your exchange operation.Commission is always collected in sourcecurrency.
For example, if you want to exchange 100 US Dollars into RussianRubles at the exchange point, where the exchange rate is 29.75, andthe 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 dealwith in our city. Let us assign unique integer number from 1 to Nto each currency. Then each exchange point can be described with 6numbers: integer A and B – numbers of currencies it exchanges, andreal R
AB, C
AB,R
BA andC
BA – exchange rates and commissionswhen exchanging A to B and B to Arespectively.
Nick has some money in currency S and wonders if he can somehow,after some exchange operations, increase his capital. Of course, hewants to have his money in currency S in the end. Help him toanswer this difficult question. Nick must always have non-negativesum of money while making hisoperations.
Input The first line of the input contains four numbers: N – the numberof currencies, M – the number of exchange points, S – the number ofcurrency Nick has and V – the quantity of currency units he has.The following M lines contain 6 numbers each – the description ofthe 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 withat 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 noexchange point is used more than once in this sequence. You mayassume that ratio of the numeric values of the sums at the end andat the beginning of any simple sequence of the exchange operationswill be less than 10
4.
Output If Nick can increase his wealth, output YES, in other case outputNO 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 PKU 题目大意: 有多种汇币,汇币之间可以交换,这需要付佣金,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100- 0.39) * 29.75 = 2963.3975 B币。问s币的金额经过交换最终得到的s币金额数能否增加。 解题思路: 反向Bellman-Ford 求一个最大路径,把Bellman_Ford的松弛条件改变就可以求出一个最大正权路径。 source: #include #include using namespace std;
struct Edge { int a,b; double c,r; };
bool bellman_ford(Edge* e,double* d,int s,double q,int n,intm) { int i,j; for(i=1;i<=n; i++) d[i]=0; d[s]=q; bool flag; for(i=1;i<=n; i++) { flag=true; for(j=1; j<=m; j++) if(d[e[j].a]>0&&d[e[j].b]<(d[e[j].a]-e[j].c)*e[j].r) { d[e[j].b]=(d[e[j].a]-e[j].c)*e[j].r; flag=false; } if(flag) returnfalse; } return true; } int main() { int n,m,s,i,a,b; double q,r,c; Edge e[300]; double d[105]; scanf(“%d%d%d%lf”,&n,&m,&s,&q); for(i=1;i<=m; i++) { scanf(“%d%d%lf%lf”,&a,&b,&r,&c); e[i].a=a; e[i].b=b; e[i].r=r; e[i].c=c; scanf(“%lf%lf”,&r,&c); e[m+i].a=b; e[m+i].b=a; e[m+i].r=r; e[m+i].c=c; } if(bellman_ford(e,d,s,q,n,2*m)) printf(“YES\n”); else printf(“NO\n”); return 0; }