POJ[1860] Currency Exchange 【判断正权回路】

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_A_B, C_A_B, R_B_A and C_B_A - 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

题目大意:

有多种货币,货币之间可以兑换,同时兑换也需要手续费。当你用100A币兑换B币时,如果A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。问S币的金额经过不限次数的兑换最终得到的S币金额数能否增加。

分析:

一种货币可以看成图上的一个点,一个“兑换点”就是图上两种货币之间的一个边。

考虑边的权值,A到B的权值为 (V-C_A_B)*R_A_B,而B到A的权值为 (V-C_B_A)*R_B_A

要求最终的S币金额增加,即要在图中找到正权回路。另外,这一回路中不一定要包含S,因为只要在这个回路中兜足够的圈数,增加的钱数总能够保证最终S币的增加。

因此可以用Bellman-Ford判断图中是否存在正环,只需要修改一下松弛条件即可。

具体解释见代码。

扫描二维码关注公众号,回复: 10706196 查看本文章
//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <vector>
#include <iomanip>

using namespace std;

const int maxn=10005;
const int INF=0x3f3f3f3f;

double dis[maxn];

int ans;
int m,n,s;
double initv;

struct node{
    int u,v;
    double r,c;
}edge[maxn];

void add(int u,int v,double r,double c){
	ans++;
    edge[ans].u=u;
    edge[ans].v=v;
    edge[ans].r=r;
    edge[ans].c=c;
}

bool Bellman_Ford(){
	memset(dis,0,sizeof(dis));	//初始化其他点的dis值为0 
	dis[s]=initv;
	bool flag;
	for(int i=1;i<=n-1;i++){
		flag=false;
		for(int j=1;j<=ans;j++)
			if(dis[edge[j].v] < (dis[edge[j].u] - edge[j].c) * edge[j].r){   //与求最短路的松弛条件正好相反                                                              
				dis[edge[j].v] = (dis[edge[j].u] - edge[j].c) * edge[j].r;
				flag=true;
			}
		if(!flag) break;	//若已经不再更新,则直接跳出 
	}
	for(int k=1;k<=ans;k++)                                          
		if(dis[edge[k].v] < (dis[edge[k].u] - edge[k].c) * edge[k].r)           //正环能够无限松弛,所以如果跳出后仍能发生更新,则存在正环 
			return true;
	return false;
}

int main(){
	int a,b;
	double rab,cab,rba,cba;
	ans=0;
	cin>>n>>m>>s>>initv;
	for(int i=1;i<=m;i++){
		cin>>a>>b>>rab>>cab>>rba>>cba;
		add(a,b,rab,cab);	//保存两个方向的边 
		add(b,a,rba,cba);
	}
	if(Bellman_Ford())  cout<<"YES"<<endl;
	else  cout<<"NO"<<endl;
	return 0;
}


 

发布了30 篇原创文章 · 获赞 5 · 访问量 900

猜你喜欢

转载自blog.csdn.net/qq_42840665/article/details/101917310