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.
只要判断是否存在正权回路。。。
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue> using namespace std; const int INF=10e8;
const int MaxN=; struct Edge
{
int v;
double R,C; Edge(int _v,double _R,double _C):v(_v),R(_R),C(_C) {}
}; vector <Edge> E[MaxN];
bool vis[MaxN];
int couNode[MaxN]; bool SPFA(double S,double lowcost[],int n,int start)
{
queue <int> que;
int u,v;
double R,C;
int len; for(int i=;i<=n;++i)
{
lowcost[i]=;
vis[i]=;
couNode[i]=;
}
vis[start]=;
couNode[start]=;
lowcost[start]=S; que.push(start); while(!que.empty())
{
u=que.front();
que.pop(); vis[u]=;
len=E[u].size(); for(int i=;i<len;++i)
{
v=E[u][i].v;
R=E[u][i].R;
C=E[u][i].C; if(lowcost[u]>=C && (lowcost[u]-C)*R>= && (lowcost[u]-C)*R>lowcost[v])
{
lowcost[v]=(lowcost[u]-C)*R; if(!vis[v])
{
vis[v]=;
++couNode[v];
que.push(v); if(couNode[v]>=n)
return ;
}
}
}
} return ;
} inline void addEdge(int u,int v,double R,double C)
{
E[u].push_back(Edge(v,R,C));
} double ans[MaxN]; int main()
{
int N,M,X,u,v;
double S,r1,r2,c1,c2; while(~scanf("%d %d %d %lf",&N,&M,&X,&S))
{
for(int i=;i<=N;++i)
E[i].clear(); for(int i=;i<=M;++i)
{
scanf("%d %d %lf %lf %lf %lf",&u,&v,&r1,&c1,&r2,&c2); addEdge(u,v,r1,c1);
addEdge(v,u,r2,c2);
} if(SPFA(S,ans,N,X))
printf("NO\n");
else
printf("YES\n");
} return ;
}