Currency Exchange POJ - 1860 (Bellman判断正环)

                                             Currency Exchange

Time Limit: 1000MS   Memory Limit: 30000K

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. 

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. 

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,拥有货币数tot
                  第二到m+1行:每个交换点交换的货币a ,b    a到b的汇率,花费,b到a的汇率,花费
       输出:     问能否通过交换使得原有的货币变多(货币类型不变)
思路:     以货币种类为点,以交换为边,以拥有每种货币的最多的数目为距离,看能否求一正权回路,使得每走一次就会使得到到某点的距离增大,如果有,最终必将使得到s点的距离增大,即s货币数目增加。Bellman - Ford本用来判断负权回路,改一下初始dis数组和判断条件,就可以判断正权回路。

ACCode:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
struct Edge {
	int st,to;
	double r,c;
	Edge(int stt=0,int too=0,double rr=0,double cc=0) {
		st = stt;
		to = too;
		r = rr;
		c = cc;
	}
};
int main()
{
	int n,m,s;
	int a,b;
	double rab,cab,rba,cba,dis[105],tot,tmp;
	bool vis[105];
	vector< Edge> v;

	while(~scanf("%d%d%d%lf",&n,&m,&s,&tot)) {
		// init()
		memset(vis,false,sizeof vis);
		memset(dis,0,sizeof dis);
		v.clear(); 

		// 读入数据
		int st;
		for(int i=1;i<=m;i++) {
			scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
			v.push_back(Edge(a,b,rab,cab));
			v.push_back(Edge(b,a,rba,cba));
		}

		// Bellman - Ford
		bool flag;
		Edge t;
		dis[s] = tot;
		for(int i=1;i<=n-1;i++) {
			flag = false;
			for(int j=0;j<v.size();j++) {
				t = v[j];
				if(dis[t.to] < (dis[t.st] - t.c)*t.r) {   //能否使得该类货币最大数目变多? 
					dis[t.to] = (dis[t.st] - t.c)*t.r;
					flag = true;
				}
			}
			if(!flag) break;
		}

		flag = false;
		for(int j=0;j<v.size();j++) {
			t = v[j];
			if(dis[t.to] < (dis[t.st] - t.c)*t.r) {
				dis[t.to] = (dis[t.st] - t.c)*t.r;
				printf("YES\n");
				flag = true;
				break;
			}
		}
		if(!flag) printf("NO\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42765557/article/details/98470352