日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

*【POJ - 1860】Currency Exchange (单源最长路---Bellman_Ford算法判正环)

發布時間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 *【POJ - 1860】Currency Exchange (单源最长路---Bellman_Ford算法判正环) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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

題目大意:

有多種匯幣,匯幣之間可以交換,這需要手續費,當你用100A幣交換B幣時,A到B的匯率是29.75,手續費是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B幣。問s幣的金額經過交換最終得到的s幣金額數能否增加

貨幣的交換是可以重復多次的,所以我們需要找出是否存在正權回路,且最后得到的s金額是增加的

怎么找正權回路呢?(正權回路:在這一回路上,頂點的權值能不斷增加即能一直進行松弛)

解題報告:

一種貨幣就是一個點

一個“兌換點”就是圖上兩種貨幣之間的一個兌換方式,是雙邊,但A到B的匯率和手續費可能與B到A的匯率和手續費不同。

唯一值得注意的是權值,當擁有貨幣A的數量為V時,A到A的權值為K,即沒有兌換

而A到B的權值為(V-Cab)*Rab

本題是“求最大路徑”,之所以被歸類為“求最小路徑”是因為本題題恰恰與bellman-Ford算法的松弛條件相反,求的是能無限松弛的最大正權路徑,但是依然能夠利用bellman-Ford的思想去解題。

因此初始化dis(S)=V?? 而源點到其他點的距離(權值)初始化為無窮小(0),當s到其他某點的距離能不斷變大時,說明存在最大路徑;如果可以一直變大,說明存在正環。判斷是否存在環路,用Bellman-Ford和spfa都可以。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<cstring>using namespace std; int n,m,s; double v; int cnt; double dis[300 + 5]; struct Point {int pos,to;double rate,cost;Point(){}Point(int pos,int to,double rate,double cost):pos(pos),to(to),rate(rate),cost(cost){} } p[300 + 5];bool Bellman_ford() {//此處與Bellman-Ford的處理相反,初始化為源點到各點距離0,到自身的值為原值dis[s] = v;//如果有一遍跑的時候,一個點也沒松弛,那就說明沒正環了,直接break。 int flag = 0;for(int i = 1; i<n; i++) {flag = 0;for(int j = 0; j<cnt; j++) {if( ( dis[p[j].pos] - p[j].cost )*p[j].rate > dis[p[j].to] ) {dis[p[j].to] = (dis[p[j].pos]-p[j].cost)*p[j].rate;flag = 1;} } if(!flag ) {return 0;//說明一遍下來一個可松弛的點都沒有,說明沒正環。 }}//再跑一遍 flag = 0;for(int j = 0; j<cnt; j++) {if( ( dis[p[j].pos] - p[j].cost )*p[j].rate > dis[p[j].to] ) {dis[p[j].to] = (dis[p[j].pos]-p[j].cost)*p[j].rate;return 1;//找到了可松弛的點 說明有正環 } }if(flag == 0) //這句應該不能加? return 0 ;//反之就沒正環 } void init() {cnt = 0;memset(p,0,sizeof(p));memset(dis,0,sizeof(dis)); } int main() {//好像不是多組輸入? double r1,r2,c1,c2;while(~scanf("%d%d%d%lf",&n,&m,&s,&v) ) { // printf("%d",v);init();int a,b;while(m--) {scanf("%d%d%lf%lf%lf%lf",&a,&b,&r1,&c1,&r2,&c2);p[cnt] = Point(a,b,r1,c1);cnt++;p[cnt] = Point(b,a,r2,c2);cnt++;} // for(int i = 0; i<cnt; i++) { // printf("%d %d %lf %lf\n",p[i].pos,p[i].to,p[i].rate,p[i].cost); // } // printf("%d\n",cnt);if(Bellman_ford()) printf("YES\n");else printf("NO\n");}return 0 ;}

AC代碼2(鄰接表,還未看)?

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <queue> #define LL long long #define eps 1e-8 #define maxn 310 #define inf 0x3f3f3f3f using namespace std;int sign(double x){if(fabs(x)<eps) return 0;return x<0? -1:1; }int m,n,s; double cur; int edges, u[maxn], v[maxn]; double rate[maxn], cost[maxn]; int first[maxn], next[maxn]; //初始化edge和first double dis[maxn];void add_edge(int s, int t, double a, double b) {u[edges] = s; v[edges] = t;rate[edges] = a; cost[edges] = b;next[edges] = first[s];first[s] = edges++; }bool bellman(int s) {for(int i=1; i<=n; i++) dis[i] = -1;dis[s] = cur; //!!!for(int i=1; i<=n; i++) {for(int e=0; e<edges; e++) {double tmp = (dis[u[e]]-cost[e])*rate[e];if(sign(dis[v[e]]-tmp) < 0) {dis[v[e]] = tmp;if(i == n) return 0;}}}return 1; }int main(int argc, char const *argv[]) {while(scanf("%d %d %d %lf", &n,&m,&s,&cur) != EOF){memset(first, -1, sizeof(first));edges = 0;for(int i=1; i<=m; i++) {int u,v; double ra,rb,ca,cb;scanf("%d %d %lf %lf %lf %lf", &u,&v,&ra,&ca,&rb,&cb);add_edge(u,v,ra,ca);add_edge(v,u,rb,cb);}if(!bellman(s)) puts("YES");else puts("NO");}return 0; }

?

總結:

? ? 剛開始一直調試不出正確答案,scanf的時候c2寫成c1了,而且在main函數中定義了int u,v本來是想讀入Point的起點終點,但是誤打誤撞有個全局變量double v,?導致一直出錯誤答案。

總結

以上是生活随笔為你收集整理的*【POJ - 1860】Currency Exchange (单源最长路---Bellman_Ford算法判正环)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。