1. 程式人生 > >POJ-1860-Currency Exchange(bellman)

POJ-1860-Currency Exchange(bellman)

Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 28989Accepted: 10834

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

解題思路:這道題可以用bellman演算法,不過要稍微變通一下,把檢查有無負環改成檢測有無正環,而且只要檢測出來就行了,
不用求具體的路徑

#include <cstring>
#include <cstdio>

const int MAXN = 105;
const int INF = 0x3f3f3f3f;
struct Edge{
int from;
int to;
double rab, cab;
};
Edge edge[MAXN * MAXN];
int edgeNum;
int m, n, s;
double v;

bool isYes()
{
double dis[MAXN];
memset(dis, 0, sizeof(dis));//檢測正環,所以初始化為0
dis[s] = v;
bool isUp = false;
for (int i = 0; i < edgeNum + 1; i++) {
isUp = false;
for (int j = 0; j < edgeNum; j++) {
if (dis[edge[j].to] < (dis[edge[j].from] - edge[j].cab) * edge[j].rab) {
dis[edge[j].to] = (dis[edge[j].from] - edge[j].cab) * edge[j].rab;
isUp = true;
if (i == edgeNum) { //如果無正環,i最多迴圈edgeNum次,也就是遍歷一次所有的邊
return true;
}
}
}
if (!isUp) {
return false;
}
}
}

int main()
{
scanf("%d%d%d%lf", &n, &m, &s, &v);
edgeNum = 0;
int fromS, toS;
double rabS, cabS, rbaS, cbaS;
for (int i = 0; i < m; i++) {
scanf("%d%d%lf%lf%lf%lf", &fromS, &toS, &rabS, &cabS, &rbaS, &cbaS);
edge[edgeNum].from = fromS;
edge[edgeNum].to = toS;
edge[edgeNum].rab = rabS;
edge[edgeNum].cab = cabS;
edgeNum++;
edge[edgeNum].from = toS;
edge[edgeNum].to = fromS;
edge[edgeNum].rab = rbaS;
edge[edgeNum].cab = cbaS;
edgeNum++;
}
if (isYes()) {
printf("YES\n");
}
else {
printf("NO\n");
}
return 0;
}

相關推薦

POJ-1860 Currency Exchange---Bellman-Ford判斷正環

判斷 ++ pac algo 操作 接下來 .cn .com chang 題目鏈接: https://vjudge.net/problem/POJ-1860 題目大意: 我們的城市有幾個貨幣兌換點。讓我們假設每一個點都只能兌換專門的兩種貨幣。可以有幾個點,專門從事相同貨幣兌

POJ-1860-Currency Exchange(bellman)

Time Limit: 1000MSMemory Limit: 30000KTotal Submissions: 28989Accepted: 10834DescriptionSeveral currency exchange points are working in our city. Let us

poj 1860 -- Currency ExchangeBellman-Ford)

ase 類型 操作 ring itl 完成 title 循環 sta poj 1860 -- Currency Exchange(Bellman-Ford) 題意: 我們的城市有幾個貨幣兌換點。讓我們假設每一個點都只能兌換專門的兩種貨幣。可以有幾個點,專門從事相同貨幣兌換

POJ-1860 Currency Exchange 【spfa判負環】

exchange call ant 代碼 while sim api where class Description Several currency exchange points are working in our city. Let us suppose that

POJ #1860 Currency Exchange 最短路徑算法 判斷負環

短路徑 pos main vector message back show 返回 .cn Description   題目描述在這裏:鏈接   更多的樣例:鏈接 思路   我們把每種貨幣看成圖的頂點,而每個交換站點實現一對貨幣的交換,可認為增加一個交換站點就

POJ-1860 Currency Exchange (最短路)

條件 n) fin inline AS 我們 fde 思想 %d https://vjudge.net/problem/POJ-1860 題意 有多種匯幣,匯幣之間可以交換,這需要手續費,當你用100A幣交換B幣時,A到B的匯率是29.75,手續費是0.39,那麽你可以

[POJ 1860] Currency Exchange

first har truct %d while fir div pan tde [題目鏈接] http://poj.org/problem?id=1860 [算法] SPFA判負環 時間復雜度 : O(kn) [代碼]

18.11.16 POJ 1860 Currency Exchange(有向圖最短路)

描述 Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange

POJ 1860 Currency Exchange spfa

注意,使用spfa演算法的時候,一個點進隊n+2次說明所有與其相關的點均已經得到了正環的收益,然而除此之外什麼都不能說明,在這道題上,也就不可能說明此時某點的值是否可以大於它的初始值。spfa是個神奇的演算法,的空一定要好好研究一番。 #include<iostrea

POJ1860 Currency Exchange

empty pop back problem == comm str num push 真是氣skr人。。沒把d[]換成double。。。de了一上午的bug// 記得用G++提交啊 題目鏈接:http://poj.org/problem?id=1860

Currency ExchangePOJ-1860

Problem Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currenc

Currency Exchange POJ

題意 n,m,s,v分別代表:有n種貨幣,有m個地方可以進行貨幣交換,你起始的貨幣型別(一開始理解成有多少種類了ㄟ( ▔, ▔ )ㄏ),你起始貨幣種類的數目 a,b,Rab,Cab,Rba,Cba 分別表示a種類貨幣,b種類貨幣,a換b的匯率,支付的金額,b換a的匯率,支

最短路徑演算法—Bellman-Ford(poj1860 currency exchange

Bellman-Ford演算法詳講Dijkstra演算法是處理單源最短路徑的有效演算法,但它侷限於邊的權值非負的情況,若圖中出現權值為負的邊,Dijkstra演算法就會失效,求出的最短路徑就可能是錯的。這時候,就需要使用其他的演算法來求解最短路徑,Bellman-Ford演算

POJ 1860(最短路之Bellman-Ford)

題意:首先給出四個數n,m,s,v,分別表示n種貨幣,m個兌換關係,s源貨幣,源貨幣的本金v 然後給出m行,每行給出6個數beg,end,r1,c1,r2,c2,分別表示beg和end貨幣兌換的比率和手續費,end和beg貨幣兌換的比率和手續費 。問能否通過貨幣兌換使手裡

Currency Exchange(最短路)

quest ber its 只需要 nging lars script end ive                            poj—— 1860 Currency Exchange Time Limit: 1000

POJ 1860

track clu 能夠 ac代碼 tdi scanf urn clas rac 須要推斷是否有正權環存在,Bellman-Ford算法就能夠辣~ AC代碼: #include <iostream> #include <cstdio> #

POJ 3903 Stock Exchange LIS

num des exc space ans scrip log program center 題目鏈接:http://poj.org/problem?id=3903 題目大意:LIS的nlog(n)寫法。 解題思路:dp[i]:=長度為i的最長遞增子序列的末尾元素最小值。那

POJ 3621 Sightseeing Cows (bellman-Ford + 01分數規劃)

pri ons map clu size algo unsigned namespace include 題意:給出 n 個點 m 條有向邊,要求選出一個環,使得這上面 點權和/邊權和 最大。 析:同樣轉成是01分數規劃的形式,F / L 要這個值最大,也就是 G(r) =

POJ 3903 Stock Exchange 【最長上升子序列】模板題

com ++ 特點 pri 解析 class span 表示 problem 題目鏈接:http://poj.org/problem?id=3903 轉載於:https://www.cnblogs.com/GodA/p/5180560.html 題目大意: 裸的DP最長

POJ1860 Currency Exchange Floyed變形(正權環)

#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <string> #include <iostream> #in