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, CAB, 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

  1. 3 2 1 20.0
  2. 1 2 1.00 1.00 1.00 1.00
  3. 2 3 1.10 1.00 1.10 1.00

Sample Output

  1. YES
  2.  
  3. 题目大意:给你n种货币,m种货币交换关系,交换率和手续费,给你起始的货币类型和金额,问你是否可以通过交换货币,最后回到起始的货币时能盈利。
  4.  
  5. 解题思路:如果要盈利,只需要判断图中存不存在正环, 即可以一直让某种货币额度无限增加。由于是无向图,那么只要存在正环,那么我就可以最后转化成起始的货币且盈利。所以只要将SPFA判负环的条件变化一下就行。初始值时,让除原点之外的d数组都赋值为0。同时松弛条件变为d[e.to] < (d[e.from] - e.com)*e.rate。即可,最后判断当u为起点时的d[u]是否大于起始金额即可。
  6.  
  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<string.h>
  4. #include<queue>
  5. #include<vector>
  6. #include<iostream>
  7. using namespace std;
  8. const int INF = 0x3f3f3f3f;
  9. const int maxn = 1e3+200;
  10. int n , m;
  11. struct Edge{
  12. int from,to;
  13. double rate , com;
  14. };
  15. vector<Edge>edges;
  16. vector<int>G[maxn];
  17. void init(){
  18. for(int i = 0; i <= n; i++){
  19. G[i].clear();
  20. }
  21. edges.clear();
  22. }
  23. double d[maxn] ,cnt[maxn], inq[maxn];
  24. void AddEdge(int u,int v,double r,double co){
  25. edges.push_back( (Edge){u,v,r,co} );
  26. m = edges.size();
  27. G[u].push_back(m-1);
  28. }
  29.  
  30. bool SPFA(int s, double V){
  31. queue<int>Q;
  32. for(int i = 0; i <= n; i++){
  33. d[i] = 0;
  34. }
  35. d[s] = V;
  36. cnt[s] ++;
  37. inq[s] = 1;
  38. Q.push(s);
  39. while(!Q.empty()){
  40. int u = Q.front();
  41. Q.pop();
  42. if(u == s&& d[s] > V){
  43. return true;
  44. }
  45. inq[u] = 0;
  46. for(int i = 0; i < G[u].size(); i++){
  47. Edge & e = edges[G[u][i]];
  48. if(d[e.to] < (d[e.from] - e.com)*e.rate ){
  49. d[e.to ] = (d[e.from] - e.com) *e.rate;
  50. if(!inq[e.to]){
  51. inq[e.to] = 1;
  52. Q.push(e.to);
  53. }
  54. }
  55. }
  56. }
  57. return false;
  58. }
  59. int main(){
  60. int mm,s;
  61. double k;
  62. while(scanf("%d%d%d%lf",&n,&mm,&s,&k)!=EOF){
  63. int a,b;
  64. double c,d;
  65. for(int i = 0; i < mm; i++){
  66. scanf("%d%d%lf%lf",&a,&b,&c,&d);
  67. AddEdge(a,b,c,d);
  68. scanf("%lf%lf",&c,&d);
  69. AddEdge(b,a,c,d);
  70. }
  71. bool yes = SPFA(s,k);
  72. if(yes){
  73. puts("YES");
  74. }else{
  75. puts("NO");
  76. }
  77. }
  78. return 0;
  79. }

  

  1.  

POJ 1860——Currency Exchange——————【最短路、SPFA判正环】的更多相关文章

  1. POJ 1860 Currency Exchange 最短路+负环

    原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  2. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  3. Currency Exchange 货币兑换 Bellman-Ford SPFA 判正权回路

    Description Several currency exchange points are working in our city. Let us suppose that each point ...

  4. poj 1860 Currency Exchange (最短路bellman_ford思想找正权环 最长路)

    感觉最短路好神奇呀,刚开始我都 没想到用最短路 题目:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 mo ...

  5. POJ 1860 Currency Exchange 最短路 难度:0

    http://poj.org/problem?id=1860 #include <cstdio> //#include <queue> //#include <deque ...

  6. 最短路(Bellman_Ford) POJ 1860 Currency Exchange

    题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...

  7. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

  8. POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告

    三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...

  9. POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

随机推荐

  1. 关于C#中的算术运算

    使用中间变量交换两个int型变量的值: ; ; a = a+b; b = a-b; a = a-b; 相信大家很容易写出来,但考虑到边界值情况时会有一些有趣的事情. 我们知道有一个int.MaxVal ...

  2. Java实现终止线程池中正在运行的定时任务

    源于开发 最近项目中遇到了一个新的需求,就是实现一个可以动态添加定时任务的功能.说到这里,有人可能会说简单啊,使用quartz就好了,简单粗暴.然而quartz框架太重了,小项目根本不好操作啊.当然, ...

  3. Django之博客系统:增加标签

    一般在发表博客后会给每个帖子加上一个标签.类似帖子关键字的功能.在这一章中来看下如何给博客添加标签功能(tagging) 添加标签需要集成第三方的Django标签应用来完成这个功能.django-ta ...

  4. Binder学习笔记(三)—— binder客户端是如何组织checkService数据的

    起点从TestClient.cpp的main函数发起: int main() { sp < IServiceManager > sm = defaultServiceManager(); ...

  5. G - 美素数

    小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识.  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是 ...

  6. c语言参考书籍

    很惭愧没能把c++学的很好,毕竟离开始工作只有2年时间,对自己要求不要过高,慢慢来吧.话说知道自己的不足,以后要更加抓紧了!fighting~ 现在计划着把c语言给学习一下了,当然这次指的是深入地学习 ...

  7. Easyui-交互式消息弹出框

    由于项目在优化的时候需要用到弹出框,按自己的想法是傻傻的用一些alert直接弹出得了,但是这样用户体验度不是特别好,影响界面美观,所以自己还是用了封装好的easyui给的消息框,怎么用呢,这个里面很有 ...

  8. 【转】ROWNUM与ORDER BY先后关系

    源地址:http://www.cnblogs.com/accumulater/p/6137385.html

  9. 【以太坊开发】区块链中的预言机:Oraclize原理介绍

    智能合约的作用很多,但是很多数据还是要基于互联网,那么如何在合约中获取互联网中的数据?Oraclize就是为了这个目的而诞生的. 工作原理: 智能合约通过对Oraclize发布一个合约之间的调用请求来 ...

  10. luogu2723 丑数

    提供一种单调队列做法(非正解) 显然每一个丑数能够由一个质数乘以另一个丑数得到 所以我们开k个单调递增队列,每次从这些队列顶部找到一个最小的元素把他捞出来,然后枚举所有质数,用这个元素乘以质数,放入相 ...