POJ1860 Currency Exchange【Bellman_ford演算法:判斷是否有正環】

Enjoy_process發表於2018-10-19

Currency Exchange

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 37892   Accepted: 14532

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

Source

Northeastern Europe 2001, Northern Subregion

問題連結:POJ1860 Currency Exchange

問題描述:有多種匯幣,匯幣之間可以交換,這需要手續費,當你用100A幣 交換B幣時,A到B的匯率是29.75,手續費是0.39,那麼你可以得到 (100 - 0.39) * 29.75 = 2963.3975 B幣。問s幣的金額經過交換最終得到的s幣金額數能否增加。首先輸入三個整數n,m,s和一個實數v分別表示有n種貨幣(從1開始編號),有m個兌換點,現在有編號為s的貨幣價值為v。接著輸入m行,每行用兩個整數a,b和四個實數Rab,Cab,Rbc,Cba描述一個兌換點,分別表示此對換點實現編號為a和b的貨幣的兌換。由a兌換為b時的匯率和手續費分別為Rab和Cab;由b兌換為a時的匯率和手續費分別為Rba和Cba;

解題思路:由於可以重複兌換,因此只要存在一個正環,那麼本金就會不斷增加,因此實際上就是判讀是否存在正環。Bellman - ford演算法是求含負權圖的單源最短路徑的一種演算法,效率較低,程式碼難度較小。其原理為連續進行鬆弛,在每次鬆弛時把每條邊都更新一下,若在n-1次鬆弛後還能更新,則說明圖中有負環,因此無法得出結果,否則就完成。Bellman_ford演算法是判斷是否存在負環,但是隻要簡單修改以下鬆弛條件和初始化,就能使用Bellman_ford演算法判斷是否存在正環。具體看程式碼實現

AC的C++程式:

#include<iostream>
#include<cstring>

using namespace std;

const int N=105;
struct Node{
	int start,end;
	double c,r;
}edge[2*N];
int n,m,M;
double dist[N];

//判斷是否有正環,存在返回true,否則返回false 
bool Bellman_ford(int s,double v)
{
	memset(dist,0,sizeof(dist));
	dist[s]=v;
	for(int i=1;i<n;i++)//進行n-1次鬆弛 
	  for(int j=0;j<M;j++)
	    if(dist[edge[j].end]<(dist[edge[j].start]-edge[j].c)*edge[j].r)
	      dist[edge[j].end]=(dist[edge[j].start]-edge[j].c)*edge[j].r;
	//經過n-1次鬆弛操作後,如果還能鬆弛,說明存在正環 
	for(int j=0;j<M;j++)
	  if(dist[edge[j].end]<(dist[edge[j].start]-edge[j].c)*edge[j].r)
	    return true;
	return false;
} 

int main()
{
	int s,a,b;
	double v;
	while(~scanf("%d%d%d%lf",&n,&m,&s,&v)){
		M=0;
		for(int i=0;i<m;i++){
			scanf("%d%d",&a,&b);
			edge[M].start=a,edge[M].end=b;
			scanf("%lf%lf",&edge[M].r,&edge[M].c);
			M++;
			edge[M].start=b,edge[M].end=a;
			scanf("%lf%lf",&edge[M].r,&edge[M].c);
			M++;
		}
		if(Bellman_ford(s,v))
		  printf("YES\n");
		else
		  printf("NO\n");
	}
	return 0;
}

 

相關文章