POJ 1860 Currency Exchange
2015-09-16 10:45
357 查看
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:
Sample Output:
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 题意:要知道,每两种货币的兑换都是不同的,货币A-B和货币B-A也是不同的,所以经过一系列的货币兑换后,本来持有的钱会出现增加后减少的情况,那么现在知道货币现在的价值,种类,以及有多少种货币和转化方式,求经过一系列的兑换后回到最初的种类,价值是否增加(那么最短路中回路问题就出现了)。
#include<stdio.h> #include<queue> #include<algorithm> using namespace std; const int N=110; const int INF=0x3f3f3f3f; double C , R , dist , V; ///C数组保存货币兑换的利率,R数组保存货币兑换需要缴纳的值 int vis , cou , n, s; ///cou数组保存每个点遍历的次数,如果次数>=n的话说明出现回路了,代表价值值增加 void Init() { int i, j; for (i = 1; i <= n; i++) { dist[i] = -INF; cou[i] = vis[i] = 0; for (j = 1; j <= n; j++) C[i][j] = R[i][j] = -INF; C[i][i] = R[i][i] = 0; } } int Spfa(int u) { int i, v; queue<int>Q; Q.push(u); dist[u] = V; cou[u]++; while (!Q.empty()) { v = Q.front(); Q.pop(); for (i = 1; i <= n; i++) { if (dist[i] < (dist[v]-C[v][i])*R[v][i]) { dist[i] = (dist[v]-C[v][i])*R[v][i]; if (!vis[i]) { Q.push(i); vis[i] = 1; cou[i]++; if (cou[i] >= n) return 1; ///判断是否有回路,只需要判断是否存在回路的原因是dist[i]只有变大了才会进行这一步,即价值增加 } } } vis[v] = 0; } if (dist[s] > V) return 1; ///没有回路但是价值增加了也是可行的(感觉这一步像废话,如果增加了肯定是有回路存在啊!) return 0; } int main () { int m, a, b, ans; double r1, r2, c1, c2; while (scanf("%d %d %d %lf", &n, &m, &s, &V) != EOF) { Init(); while (m--) { scanf("%d %d %lf %lf %lf %lf", &a, &b, &r1, &c1, &r2, &c2); C[a][b] = c1; C[b][a] = c2; R[a][b] = r1; R[b][a] = r2; } ans = Spfa(s); if (ans) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- Intersection of Two Linked Lists 解答
- How to add Oracle JDBC driver in your Maven local repository
- block
- 从谷歌说开去:互联网的未来将充满垄断
- KEIL MDK 查看代码量、RAM使用情况--RO-data、RW-data、ZI-data的解释(转)
- 轻乳酪蛋糕
- C# 系统应用之TreeView控件显示树状磁盘文件目录及加载图标
- 直接启动SDK Manager: $ADNROID_HOME/tools/android
- hdu 5442 Favorite Donut(后缀数组)
- 常量指针和指针常量
- spark调度系列------2. Spark Executor的创建和启动过程
- 不安装oracle客户端,用plsql连接oracle
- CodeForcesGym 100735H Words from cubes
- (大数据分析-3)HBASE Review
- 点滴感悟
- ios开发问题2—uitableviewController中如何改变tableview的位置
- 使用脚本便捷地在 Ubuntu 中安装最新 Linux 内核
- js 闭包概念
- cisco配置在线下载工具
- PHP Ajax 跨域问题最佳解决方案