POJ 1860 Bellman-frod判断负权回路
来源:互联网 发布:淘宝培训机构单烽讲师 编辑:程序博客网 时间:2024/06/15 15:06
Currency Exchange
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 24022 Accepted: 8718
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
Currency Exchange
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 24022 Accepted: 8718
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
题意:
有几个货币,每个货币的兑换比例不一样,手续费也不一样。这两个参数从A到B和从B到A是不一样的。问有没有一个环使得交换过后钱变多了。
题解:
Bellman-frod判断一下有无负权回路即可。
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <vector>#define f(i,a,b) for(i = a;i<=b;i++)#define fi(i,a,b) for(i = a;i>=b;i--)using namespace std;struct Po{int from,to;double R,C;};Po G[350];double d[350];int main(){// freopen("data.in","r",stdin); int N,M,S; double V; while(~scanf("%d%d%d%lf",&N,&M,&S,&V)){ int i,j; int CNT = 0; f(i,1,M){ int ff,tt; double rr,cc; scanf("%d%d%lf%lf",&ff,&tt,&rr,&cc); G[CNT].from = ff;G[CNT].to = tt; G[CNT].R = rr;G[CNT++].C = cc; scanf("%lf%lf",&rr,&cc); G[CNT].from = tt;G[CNT].to = ff; G[CNT].R = rr;G[CNT++].C = cc; } memset(d,0,sizeof(d)); d[S] = V; f(j,1,N-1){ int ok = 0; f(i,0,CNT-1){ if(d[G[i].to]<(d[G[i].from]-G[i].C)*G[i].R){ d[G[i].to] = (d[G[i].from]-G[i].C)*G[i].R; ok = 1; } } if(ok == 0) break; } int ok = 0; f(i,0,CNT-1) if(d[G[i].to]<(d[G[i].from]-G[i].C)*G[i].R){ ok = 1; break; } if(ok) printf("YES\n"); else printf("NO\n"); } return 0;}
- POJ 1860 Bellman-frod判断负权回路
- POJ 3259 Wormholes(判断负权回路|SPFA||Bellman-Ford)
- poj 3259 bellman-ford判断是否存在负权回路
- poj 3259 bellman最短路判断有无负权回路
- Bellman-Ford算法判断负权回路
- bellman判断负权回路--poj3259
- Bellman-For判断负权回路
- poj 3259 负权回路+Bellman
- poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,判断是否存在负权回路)
- poj 3259 bellman-ford算法 判断是否存在负权回路
- POJ 3259 Bellman-frod
- PKU 3259 Wormholes - 判断负权回路 Bellman-Ford
- POJ-3259 Wormholes(负权回路[Bellman-Ford])
- POJ 3259 Wormholes (Bellman) (负权回路)
- 【bellman-Ford判断正权回路】POJ
- POJ 3259 SPFA判断负权回路
- 【spfa 判断负权回路】POJ
- zigbee串口打印无法显示或乱码解决方法
- 类与对象编程题1(C++程序设计第3周)
- 1212
- linux之awk命令
- C#_索引器的使用
- POJ 1860 Bellman-frod判断负权回路
- 转载:Android快速开发系列 10个常用工具类
- 排序算法--冒泡排序
- Perfect Squares | LeetCode
- php 小计----常量名称和值的在单双引号中的搭配使用
- 类与对象编程题2(C++程序设计第3周)
- UESTC 1265 宝贵资源
- ActionScript 3.0 学习(十四) 从加载的.swf文件中读取flash库中的as链接的类
- Jenkins的分布式构建