POJ1860 Currency Exchange【Bellman_ford演算法:判斷是否有正環】
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;
}
相關文章
- 判斷一個有向圖是否有環
- POJ3259 Wormholes【判斷是否有負環】Worm
- 對一個連結串列判斷是否有環
- 如何判斷連結串列中是否有環並找出環的入口位置
- 正規表示式判斷是否是數字
- 判斷字元是否重複正規表示式字元
- 判斷是否為環形連結串列
- 演算法題:反轉一個單連結串列&判斷連結串列是否有環演算法
- 如何判斷FMEA是否以正確的方式完成?
- 正則判斷MIME 型別是否是圖片型別
- 用正規表示式判斷時間是否合法
- 判斷欄位中是否有漢字
- 牛客網高頻演算法題系列-BM6-判斷連結串列中是否有環演算法
- 正規表示式判斷密碼是否符合要求。密碼
- POJ2240 Arbitrage【判斷正環的存在】
- 演算法題:判斷括號字串是否有效演算法字串
- 如何判斷一項技術是否有前途?
- JavaScript 判斷物件中是否有某屬性JavaScript物件
- Python:判斷一個正整數是否為迴文數Python
- python如何判斷一列是否有資料Python
- 判斷字串是否唯一字串
- python判斷是否為listPython
- 判斷URL字串是否合法字串
- 判斷字串是否為空字串
- python 判斷是否為中文Python
- 正規化判斷
- js判斷物件裡面是否有某個屬性JS物件
- 判斷一個物件是否為空物件,判斷一個物件中是否有空值物件
- 判斷協議是否出網協議
- QJsonObject判斷欄位是否存在JSONObject
- MySQL判斷表名是否存在MySql
- Delphi Variant 判斷是否為空
- java判斷物件是否為空Java物件
- jQuery 判斷元素是否隱藏jQuery
- mysql如何判斷是否為空MySql
- js判斷字串是否為空JS字串
- postgresql如何判斷表是否存在SQL
- JavaScript 判斷是否是陣列JavaScript陣列