POJ 1860 Currency Exchange 貝爾曼-福特算法(Bellman-Ford)

Currency Exchange
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16723   Accepted: 5856

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

這道題我只想說,各種無情與各種的坑,已經無力吐槽了,但是依然還是AC了,這道題具體可以選擇Bellman-Ford算法,這個算法本身是求最短路徑的,但是在這道題中,負權環的利用對求解這道題灰常有幫助,我們可以把每一種幣種當成是圖論中的一個點,然後之間的匯率以及匯費計算結果當成之間的邊的權值,然後求自己所擁有的貨幣經過一系列兌換,直到兌換回原來貨幣的時候是否可以增長,這道題解題就是運用Bellman-Ford算法求最長路程,其實就是判定這個圖中是否有一條負權環,即一條環,可無限增加Nick手中的錢數,只要任意存在一條這樣的環,則Nick手中的錢可以增長,否則不可以。這樣的情況下就可以AC了,之後我在做題時候發現,如果把數組定義爲題目中所說的100稍大的105時,題目會報Runtime Error,對此我很無語,把105改成256,結果就AC了,這是一個巨坑的點,給的數據超出了他所限定的數據範圍!!!

下面是AC代碼:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
double num,rate[256][256],com[256][256];
int m,n,fi;
struct Edge{
    int u, v;    
    double com,rate;  
}edge[256];
double dist[256];
void relax(int u, int v, double rate, double com)
{
    if(dist[v] < ( dist[u] - com ) * rate && dist[u] > com)
        dist[v] = ( dist[u] - com ) * rate;
}
bool Bellman_Ford()
{
	int i;
	for(i=1;i<=n;i++)
		dist[i]=0;
	dist[fi]=num;
    for(i=1; i<=n-1; ++i)
        for(int j=1; j<=2*m; ++j)
            relax(edge[j].u, edge[j].v, edge[j].rate, edge[j].com);
    bool flag = 1;
        for(i=1; i<=2*m; ++i)
        if(dist[edge[i].v] < ( dist[edge[i].u] - edge[i].com ) * edge[i].rate )
        {
            flag = 0;
            break;
        }
    return flag;
}
int main()
{
	int i,a,b;
	while(scanf("%d%d%d%lf",&n,&m,&fi,&num)!=EOF)
	{
	memset(rate,0,sizeof(rate));
	memset(com,0,sizeof(com));
	for(i=1;i<=2*m;i+=2)
	{
		scanf("%d%d",&a,&b);
		edge[i].u=a;
		edge[i].v=b;
		edge[i+1].u=b;
		edge[i+1].v=a;
		cin>>edge[i].rate>>edge[i].com>>edge[i+1].rate>>edge[i+1].com;
	}
	if(Bellman_Ford()==0)
		printf("YES\n");
	else
		printf("NO\n");
	}
	return 0;
}


發佈了43 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章