POJ-1860 Currency Exchange(Bellman-Ford)
6180 단어 poj최단로Bellman-Ford
http://poj.org/problem?id=1860
Time Limit: 1000MS
Memory Limit: 30000K
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
AB, C
AB, R
BA and C
BA - 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
제목 대의: n종 화폐, 2*m종 환전 관계가 있습니다. 매번 환전할 때 c원 수수료를 지불해야 합니다. 환율은 r이고 처음에는 s화폐 v원이 있습니다. 환전을 통해 소지한 s화폐를 많이 만들 수 있습니까?
본고는 한 바퀴를 돌면 권한이 커지기 때문에 Bellman-Ford의 초기 조건과 갱신 조건을 변경하면 이미 s를 기점으로 하는'최장길'로 변경할 수 있다. 갱신 방법이 바뀌었기 때문에 매번 갱신할 수 있는 점은 반드시 s점에 도달할 수 있는 점, 즉 s로 환전할 수 있기 때문에 Bellman-Ford로'마이너스 회로'가 있는지 판단하면 된다.
토론판 이후의 데이터 정보:
45 43 8 32.460000
40 6 90.90 17.59 45.28 89.70
29 18 37.97 55.13 84.48 52.81
45 26 59.25 99.43 27.79 90.84
7 42 57.94 54.76 90.96 64.60
27 12 18.69 31.72 66.78 65.72 //5
31 4 48.09 14.86 56.63 18.38
23 1 96.51 39.40 14.79 23.51
22 25 85.68 78.28 77.54 0.17
18 5 28.25 56.09 2.81 25.39
34 42 91.34 9.29 34.51 99.34
20 32 39.35 63.72 58.26 19.53
36 34 1.79 61.68 23.29 4.84
30 12 70.83 5.46 4.05 31.37 //13
16 37 39.47 57.44 59.00 79.05
16 14 68.12 5.58 35.39 19.28
13 44 27.08 32.10 75.83 47.52
45 37 6.31 81.34 22.96 54.24
24 19 41.55 58.31 93.99 84.14
28 36 73.32 80.70 37.02 33.93
6 21 56.88 6.52 76.14 30.43
18 35 83.55 30.02 41.25 89.11
17 37 70.84 51.14 44.76 29.99
14 2 42.34 26.79 51.95 93.56
40 6 63.46 83.44 82.66 15.09
27 20 34.61 61.69 10.23 35.04
29 13 83.62 38.65 88.51 27.75
30 22 59.09 3.86 63.35 57.85
4 18 11.24 97.52 8.80 11.15
38 42 79.71 33.62 3.36 36.53
38 45 90.65 72.52 18.35 54.58
33 45 16.35 58.44 57.71 88.69
1 43 84.41 35.96 72.06 27.57
13 9 46.36 94.47 81.63 88.19
42 10 41.22 1.63 98.33 85.94
9 3 40.00 49.78 46.31 86.28
11 37 27.72 17.84 56.89 86.23
45 10 99.57 92.25 71.49 1.57
8 14 19.47 82.06 32.79 90.53
23 37 26.54 38.20 99.18 86.79
10 42 98.34 36.13 88.52 98.24
13 30 85.81 43.84 54.64 33.83
31 8 53.55 51.02 46.20 87.65
30 8 92.31 36.44 91.82 23.12 //43
이 데이터는 YES를 출력해야 하지만 Bellman-Ford를 사용하면 롱더블을 사용해야 합니다. 그렇지 않으면 몇 번 업데이트된 후에 모든 디스가 inf로 바뀌어 최종 결과가 잘못됩니다.
그러나longdouble로 WA를 할 수 있습니다. 더블이 넘칠 것을 고려하지 않았기 때문에 데이터가 잘못되었을 수도 있습니다.
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN=105;
const int INF=0x3f3f3f3f;
struct Edge {
int s,e;
double r,c;
Edge(int ss=0,int ee=0,double rr=0,double cc=0):s(ss),e(ee),r(rr),c(cc) {}
};
int n,m,a,b;
double s,v,r1,c1,r2,c2;
vector<Edge> edge;
double dis[MAXN];
bool Bellman_Ford(int sta) {//
bool relaxed;
memset(dis,0,sizeof(dis));
dis[sta]=v;// sta v, 0
for(int i=1;i<n;++i) {
relaxed=false;
for(int j=0;j<edge.size();++j) {
if((dis[edge[j].s]-edge[j].c)*edge[j].r>dis[edge[j].e]) {// ,
dis[edge[j].e]=(dis[edge[j].s]-edge[j].c)*edge[j].r;
relaxed=true;
}
}
if(!relaxed) {// , ,
return false;
}
}
for(int j=0;j<edge.size();++j) {
if((dis[edge[j].s]-edge[j].c)*edge[j].r>dis[edge[j].e]) {// ,
return true;
}
}
return false;
}
int main() {
while(4==scanf("%d%d%lf%lf",&n,&m,&s,&v)) {
edge.clear();
while(m-->0) {
scanf("%d%d%lf%lf%lf%lf",&a,&b,&r1,&c1,&r2,&c2);
edge.push_back(Edge(a,b,r1,c1));
edge.push_back(Edge(b,a,r2,c2));
}
printf("%s
",Bellman_Ford(s)?"YES":"NO");
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
POJ3071: Football(확률 DP)Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2n. After n rounds, only one team...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.