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=110; 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=1;i<=n;++i) { lowcost[i]=0; vis[i]=0; couNode[i]=0; } vis[start]=1; couNode[start]=1; lowcost[start]=S; que.push(start); while(!que.empty()) { u=que.front(); que.pop(); vis[u]=0; len=E[u].size(); for(int i=0;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>=0 && (lowcost[u]-C)*R>lowcost[v]) { lowcost[v]=(lowcost[u]-C)*R; if(!vis[v]) { vis[v]=1; ++couNode[v]; que.push(v); if(couNode[v]>=n) return 0; } } } } return 1; } 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=1;i<=N;++i) E[i].clear(); for(int i=1;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 0; }
(简单) POJ 1860 Currency Exchange,SPFA判圈。
原文:http://www.cnblogs.com/whywhy/p/4338563.html