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 R
AB, C
AB, R
BA and C
BA - 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.
Output
If Nick can increase his wealth, output YES, in other case output NO to the output file.
题目大意:给出每种货币之间的交换汇率,手续费,
问,能不能互相交换使自己的钱增多;
解题方法:
ford,看能不能产生环,然后每次交换之后自己的钱在增多
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
struct Node{
int a,b;
double rate,cost;
}p[510];
double vis[110];
int n,m,s,c;
double v;
bool flod(){
memset(vis,0,sizeof(vis));
vis[s]=v;
for(int i=1;i<n;i++){
bool flag=false;
for(int j=0;j<c;j++){
int a=p[j].a;
int b=p[j].b;
double r,C;
r=p[j].rate;
C=p[j].cost;
if(vis[b]<(vis[a]-C)*r){
vis[b]=(vis[a]-C)*r;
flag=true;
}
}
if(!flag) break;
}
for(int i=0;i<c;i++){
if(vis[p[i].b]<(vis[p[i].a]-p[i].cost)*p[i].rate)
return true;
}
return false;
}
int main(){
while(~scanf("%d%d%d%lf",&n,&m,&s,&v)){
c=0;
for(int i=0;i<m;i++){
int a,b;
double rab,cab,rba,cba;
scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
p[c].a=a;
p[c].b=b;
p[c].rate=rab;
p[c].cost=cab;
c++;
p[c].a=b;
p[c].b=a;
p[c].rate=rba;
p[c].cost=cba;
c++;
}
if(flod()){
printf("YES\n");
}else printf("NO\n");
}
return 0;
}