poj 1860 Currency Exchange(bellman-ford)

网友投稿 256 2022-08-31

poj 1860 Currency Exchange(bellman-ford)

题目:​​Exchange​​

Time Limit: 1000MS

 

Memory Limit: 30000KB

 

64bit IO Format: %I64d & %I64u

​​Submit​​​ ​​Status​​

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

不知道怎么解决这道题,于是看看大神们的思路。原来是用bellman判断正环。即使这样,也花了不少时间来理解。bellman-ford算法:基于动态规划的思想,反复利用已有的边来更新最短距离,具体步骤:

(1).初始化所有的点i,赋初值dis[i]=INF,dis[s]=0;

(2).如果dis[u]!=INF且dis[v]>dis[u]+map[u][v],那么dis[v]=dis[u]+map[u][v]。

(3).循环步骤(2)n-1次或直到某次不再更新。

(4).对于每一条边(u,v),如果dis[u]!=INF且dis[v]>dis[u]+map[u][v]则存在负权回路。

在最糟糕的情况下需要更新n-1次,每一次更新遍历每一条边。所以bellman-ford的时间复杂度O(n*m)比dijkstra还高。

这里的初始化:dis[i]=0; dis[s]=weal.更新操作:dis[edge[j].v]<(dis[edge[j].u]-edge[j].c)*edge[j].r  dis[edge[j].v]=(dis[edge[j].u]-edge[j].c)*edge[j].r  如果提前跳出了循环,判断是否大于weal; 如果没有大于weal但是存在正环也算是OK。关于正环:只要存在一个让钱不断变多的环,那么就一定可以让原始weal进入那个交易环并且最终获利。比如例子中的2,3:

是的,他就是一个正环。转了一圈回到了起点,iterator=n,跳出循环后iterator=n+1.spfa算法里的循环主要和所有顶点的进队平均次数k和度e相关,所以还是用bellman-ford算法易于比较iterator VS n+1

#include #include #include #include using namespace std;const int N=105,INF=1000000;int n,m,s,count;double dis[N],weal;struct node{ int u,v; double r,c;}edge[2*N];void addedge(int uu,int vv,double rr,double cc){ edge[count].u=uu; edge[count].v=vv; edge[count].r=rr; edge[count++].c=cc;}bool bellmanford(){ int i; for(i=1;iweal) return 1; return 0;}int main(){ //freopen("cin.txt","r",stdin); while(cin>>n>>m>>s>>weal){ count=0; int a,b; double r1,c1,r2,c2; while(m--){ scanf("%d %d %lf %lf %lf %lf",&a,&b,&r1,&c1,&r2,&c2); addedge(a,b,r1,c1); addedge(b,a,r2,c2); } if(bellmanford()) puts("YES"); else puts("NO"); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:公关界的007:携手《我和我的父辈》 TCL娱乐营销新玩法实力出圈!
下一篇:差分约束系统 & poj 3159 Candies
相关文章

 发表评论

暂时没有评论,来抢沙发吧~