POJ - 1860 Currency Exchange (Spfa判断是否有正权环)

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.

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<=10^3.
For each point exchange rates and commissions are real, given with at 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 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 10^4.

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

题意:钱的种类为N,M个兑换口,拥有种类为S的钱的数目V。

每个兑换口可以将a与b互换,剩下的四个数为a兑换成b的汇率与手续费和b换成a的汇率与手续费。

换钱公式:换后钱数 = (换前钱数-手续费)*汇率。

问钱的数目是否可以增多。

思路:

钱换来换去再每次都减去手续费,现实中只会逐渐变少(要不然人人都是富翁了)。要说哪种情况下钱能凭白变多,那只能是出现了漏洞,类似于从A换到B汇率是1.10,B换到C汇率是1.10,C换到A汇率也是1.10,这种情况下换一圈就发了。

所以其实就是找正权环。

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>

using namespace std;

const int MAXN =1e3+10;

struct Edge{
	int to;
	double rate,com;
	Edge(){}
	Edge(int a,double b,double c):to(a),rate(b),com(c){}
};

vector<Edge> E[MAXN];

inline void Add(int from,int to,double rate,double com){
	E[from].push_back(Edge(to,rate,com));
}

int N,M,S;
double V;

bool mark[MAXN];
double dis[MAXN];//这里的含义是当前的钱兑换成各个币种后的数量。 

bool Spfa(int from){
	memset(mark,false,sizeof mark);
	memset(dis,0,sizeof dis);
	dis[from] = V;
	mark[from] = true;
	queue<int> Q;
	Q.push(from);
	while(!Q.empty()){
		int t = Q.front();
		Q.pop();
		mark[t] = false;
		for(int i=0 ; i<E[t].size() ; ++i){
			Edge &e = E[t][i];
			if(dis[e.to] < (dis[t]-e.com)*e.rate){
				dis[e.to] = (dis[t]-e.com)*e.rate;
				if(!mark[e.to]){
					mark[e.to] = true;
					Q.push(e.to);
				}
			}
			if(dis[from] > V)return true;
			/*其他种类的钱大于V都不能代表钱变多了,只有当最初币种的钱变多了才是真的多了。
			而且题中也说了结果币种是S*/ 
		}
	}
	return false;
}

int main(){
	
	while(scanf("%d %d %d %lf",&N,&M,&S,&V) == 4){
		int a,b;
		double c,d,e,f;
		for(int i=1 ; i<=M ; ++i){
			scanf("%d %d %lf %lf %lf %lf",&a,&b,&c,&d,&e,&f);
			Add(a,b,c,d);//a->b
			Add(b,a,e,f);//b->a
		}
		if(Spfa(S))printf("YES\n");
		else printf("NO\n");
		for(int i=0 ; i<=N ; ++i)E[i].clear();
	}
	
	return 0;
}

 

posted @ 2018-07-27 18:12  Assassin_poi君  阅读(149)  评论(0编辑  收藏  举报