poj 1860 Currency Exchange(bellman-ford)
来源:互联网 发布:文件编辑软件 编辑:程序博客网 时间:2024/04/29 09:17
题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25713
Currency Exchange
Time Limit: 1000MS Memory Limit: 30000KB 64bit IO Format: %I64d & %I64u
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.
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.
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.01 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00
Sample Output
YES
(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 <iostream>#include <cstdio>#include <cmath>#include <cstring>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;i<n;i++) dis[i]=0; dis[s]=weal; for(i=1;i<=n;i++){ bool tag=0; for(int j=0;j<count;j++){ if(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; tag=1; } } if(tag==0) break; } if(i==n+1 || dis[s]>weal) 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;}
0 0
- POJ 1860 Currency Exchange Bellman-Ford
- POJ-1860-Currency Exchange-Bellman-ford
- poj 1860 Currency Exchange :bellman-ford
- POJ 1860 Currency Exchange(Bellman-Ford)
- POJ 1860 Currency Exchange(Bellman-Ford)
- poj 1860 Currency Exchange(bellman-ford)
- Bellman-Ford-POJ-1860-Currency Exchange
- POJ 1860 Currency Exchange(Bellman-Ford)
- poj 1860 Currency Exchange 【bellman-ford】
- poj 1860 Currency Exchange (Bellman- Ford)
- POJ-1860 Currency Exchange(Bellman-Ford)
- poj 1860 Currency Exchange(bellman-ford)
- ZOJ 1544 / POJ 1860 Currency Exchange(bellman-ford)
- zoj 1544 || poj 1860 Currency Exchange(Bellman-ford)
- POJ 1860Currency Exchange (最短路径.Bellman-Ford)
- Poj 1860 Currency Exchange(Bellman-Ford,SPFA解单源最短路径问题)
- POJ 1860 Currency Exchange(最短路 Bellman-Ford)
- POJ 1860 Currency Exchange (Bellman-Ford 找正环)
- IOException while loading persisted sessions: java.io.EOFException
- 迅为4412开发板Linux驱动教程/硬件知识及原理图的使用
- java基础系列--反射与AOP实现
- HDU 4324:Triangle LOVE【拓扑排序】
- js 字符串转化成数字
- poj 1860 Currency Exchange(bellman-ford)
- WebSocket 实战
- android:关于主工程和library project
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- 查询数组中相同元素的个数
- 学习使用Gradle
- Eclipse使用技巧总结
- MapReducer实现单词计算
- java中的反射机制