Currency Exchange
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 22648 | Accepted: 8180 |
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题意是给出了M种钱币,N个转换所,每个钱币的转换关系,中间要给转换所交一定的中介费。求是否能够有这样的条件,使得钱在转了一圈之后变多了。
看到只需输入YES/NO这样,不用求具体的单源点最短路径或是所有的,就感觉很符合Bellman。。。还有一点,Bellman和Floyd能够处理负权值的图,但Flyod不能有负环。Dijsktra不能处理含有负权值的图。这题用Bellman只是在给每条边松弛的时候不再是简单的相加了,而是dis[edge[j].s]-edge[j].com)*edge[j].l 。
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #pragma warning(disable:4996) using namespace std; struct E{ int s; int e; double com; double l; }edge[5205]; int N,M,S,edge_num; double V; double dis[505]; void addedge(int start,int end,double co,double len) { edge_num++; edge[edge_num].s=start; edge[edge_num].e=end; edge[edge_num].com=co; edge[edge_num].l=len; } bool bellman_ford() { int i,j; for(i=1;i<=N-1;i++) { int flag=0; for(j=1;j<=edge_num;j++) { if(dis[edge[j].e]<(dis[edge[j].s]-edge[j].com)*edge[j].l) { flag=1; dis[edge[j].e]=(dis[edge[j].s]-edge[j].com)*edge[j].l; } } if(flag==0) break; } for(j=1;j<=edge_num;j++) { if(dis[edge[j].e]<(dis[edge[j].s]-edge[j].com)*edge[j].l) return true; } return false; } int main() { int i,start,end; double co,len; edge_num=0; memset(dis,0,sizeof(dis)); cin>>N>>M>>S>>V; for(i=1;i<=M;i++) { cin>>start>>end>>len>>co; addedge(start,end,co,len); cin>>len>>co; addedge(end,start,co,len); } dis[S]=V; if(bellman_ford()) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。