poj1860 - Currency Exchange
2015-12-04 17:32
411 查看
题目网址:http://poj.org/problem?id=1860
题目:
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 23979 Accepted: 8696
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
题目大意:
有一个孩子,就是想要换钱嘛,两个地方的钱的汇率不一样,然后换钱还需要押金。
所以那个孩子就想,会不会有那么一种情况,换着换着钱就多了起来。
前面四个数字是n(货币的数量), m(有m个交换点), s(开始拥有的货币编号),以及money
最后两个数字表示换钱的地点,以及从第一个地点换到第二个地点的汇率已经佣金,还有第二个地点到第一个地点换钱的汇率和佣金。
思路:
看看是不是有正环,这就是最短路的问题了,直接用bellman算法松弛就好了。看看是不是有正环,因为最后的金币得和原来的金币是同一种币种呀
题目:
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 23979 Accepted: 8696
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
题目大意:
有一个孩子,就是想要换钱嘛,两个地方的钱的汇率不一样,然后换钱还需要押金。
所以那个孩子就想,会不会有那么一种情况,换着换着钱就多了起来。
前面四个数字是n(货币的数量), m(有m个交换点), s(开始拥有的货币编号),以及money
最后两个数字表示换钱的地点,以及从第一个地点换到第二个地点的汇率已经佣金,还有第二个地点到第一个地点换钱的汇率和佣金。
思路:
看看是不是有正环,这就是最短路的问题了,直接用bellman算法松弛就好了。看看是不是有正环,因为最后的金币得和原来的金币是同一种币种呀
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAX = 110; int n, m, s; double monney; struct Point { int u, v;//从u换到v double rate, comm;//汇率、佣金 } point[2*MAX]; double dist[MAX]; bool Bell_man() { for(int i = 0; i <= n; i++)//初始化 { dist[i] = 0.0; } dist[s] = monney;//最开始拥有的钱 for(int j = 1; j < n; j++)//最多进行n-1次松弛 { bool flag = true; for(int i = 0; i < 2*m; i++) { if(dist[point[i].v] < (dist[point[i].u]-point[i].comm)*point[i].rate)//表示可以松弛(也就是钱变多了) { dist[point[i].v] = (dist[point[i].u]-point[i].comm)*point[i].rate; flag = false; } } if(flag)//不能再松弛了,所以就一定没有正环了,直接返回不能 { return 0; } } for(int i = 0; i < 2*m; i++)//判断是不是有正环 { if(dist[point[i].v] < (dist[point[i].u]-point[i].comm)*point[i].rate) { return 1; } } return 0; } int main() { while(scanf("%d%d%d%lf", &n, &m, &s, &monney) != EOF) { int a, b, k = 0; double c, d, c1, d1; for(int i = 1; i <= m; i++)//双向兑换 { scanf("%d%d%lf%lf%lf%lf", &a, &b, &c, &d, &c1, &d1); point[k].u = a, point[k].v = b; point[k].rate = c; point[k++].comm = d; point[k].v = a; point[k].u = b; point[k].rate = c1; point[k++].comm = d1; } if(Bell_man()) { printf("YES\n"); } else { printf("NO\n"); } } return 0; }
相关文章推荐
- iOS开发——高级语法篇&继承、实现、依赖、关联、聚合、组合的联系与区别
- JDBC-Oracle
- 数字三角形问题
- Android 4.4(KitKat)中VSync信号的虚拟化
- 完美解决Android SDK Manager无法更新
- 设置View大小随屏幕自动放大
- 《FPGA全程进阶---实战演练》第三章之PCB设计之去耦电容
- 《HTML5 CANVAS基础教程》读书笔记
- 设计模式解析与实战之工厂方法模式
- HDUOJ Number Sequence找规律
- 关于SQL SERVER 2008 R2配置管理器出现“远程过程调用失败”【0x800706be】的解决办法(
- jquery radio的取值 radio的选中 radio的重置
- C语言函数入栈顺序
- 使用 alias 以及 sshpass 命令简化ssh登陆步骤
- java笔记-面向对象(封装)
- yii2 嵌套布局 layout
- Failed to load javaHL Library
- NOIP滚粗
- 对数组对象按某些属性排序方法
- 设置一个字符串中的个别字符的特殊格式