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

  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种货币,货币之间按照汇率交换,当然还要花费一些手续费,货币交换是可以多次重复进行的,问有没有可能经过一系列的货币交换,开始的货币会增加?
    当你用100A币交换B币时,AB的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。
    解题思路:这道题可以抽象为图论中的题,将货币种类看为点,货币之间的交换看为有向边,想要货币的金额产生增加,那么必然要有正权回路,即在一条回路上能够一直松弛下去。该题的问题主要在于所给的参数很多,第一行给出了n种货币有m种交换方式,给你第s种货币有V的金额,对于m种的交换方式,从xy需要汇率rate和手续费commission,从yx也需要这两个参数。同时这里的松弛递推公式也要发生变化:
  1. if(dist[edge[i].t]<(dist[edge[i].f]-edge[i].c)*edge[i].r)
  2. {
  3. dist[edge[i].t]=(dist[edge[i].f]-edge[i].c)*edge[i].r;
  4. }
  1. 因为是需要增加的正权回路,所以如果小于就松弛。
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. #define inf 0x3f3f3f3f
  6. struct Edge
  7. {
  8. int f;
  9. int t;
  10. double r;
  11. double c;
  12. } edge[];
  13. double dist[];
  14. int n,m,s,cnt;
  15. double x;
  16. int bellman_ford()
  17. {
  18. int i,j;
  19. int flag;
  20. for(i=; i<=n; i++)
  21. {
  22. dist[i]=;
  23. }
  24. dist[s]=x;
  25. for(j=; j<=n; j++)
  26. {
  27. flag=;
  28. for(i=; i<=cnt; i++)
  29. {
  30. if(dist[edge[i].t]<(dist[edge[i].f]-edge[i].c)*edge[i].r)
  31. {
  32. dist[edge[i].t]=(dist[edge[i].f]-edge[i].c)*edge[i].r;
  33. flag=;
  34. }
  35. }
  36. if(flag==)
  37. {
  38. break;
  39. }
  40. }
  41. return flag;
  42. }
  43. int main()
  44. {
  45. int i,t;
  46. int u,v;
  47. double a1,a2,b1,b2;
  48. while(scanf("%d%d%d%lf",&n,&m,&s,&x)!=EOF)
  49. {
  50. cnt=;
  51. while(m--)
  52. {
  53. scanf("%d%d%lf%lf%lf%lf",&u,&v,&a1,&b1,&a2,&b2);
  54. edge[cnt].f=u;
  55. edge[cnt].t=v;
  56. edge[cnt].r=a1;
  57. edge[cnt++].c=b1;
  58. edge[cnt].f=v;
  59. edge[cnt].t=u;
  60. edge[cnt].r=a2;
  61. edge[cnt++].c=b2;
  62. }
  63. if(bellman_ford())
  64. {
  65. printf("YES\n");
  66. }
  67. else
  68. {
  69. printf("NO\n");
  70. }
  71. }
  72. return ;
  73. }
  1.  

附上使用SPFA的代码

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. #include<vector>
  5. #include<algorithm>
  6. using namespace std;
  7. const int INF = 0x3f3f3f3f;
  8. const int maxs = 1e3+;
  9. int n,m;
  10. struct Edge
  11. {
  12. int to;
  13. double rate;
  14. double com;
  15. } ;
  16. double dis[maxs];
  17. int vis[maxs];
  18. int cnt[maxs];///用来记录入队列次数
  19. vector<Edge>maps[maxs];
  20. void AddEdge(int u,int v,double r,double co)
  21. {
  22. Edge t;
  23. t.to=v;
  24. t.rate=r;
  25. t.com=co;
  26. maps[u].push_back(t);
  27. }
  28. int SPFA(int s, double v)
  29. {
  30. int i;
  31. memset(dis,,sizeof());
  32. memset(vis,,sizeof());
  33. memset(cnt,,sizeof());
  34. queue<int>q;
  35. dis[s]=v;
  36. vis[s]=;
  37. cnt[s]++;
  38. q.push(s);
  39. while(!q.empty())
  40. {
  41. int u=q.front();
  42. q.pop();
  43. vis[u]=;
  44. for(i=; i<maps[u].size(); i++)
  45. {
  46. int to=maps[u][i].to;
  47. double com=maps[u][i].com;
  48. double rate=maps[u][i].rate;
  49. if(dis[to]<(dis[u]-com)*rate)
  50. {
  51. dis[to]=(dis[u]-com)*rate;
  52. if(!vis[to])
  53. {
  54. vis[to]=;
  55. cnt[to]++;
  56. if(cnt[to]>=n)
  57. {
  58. return ;
  59. }
  60. q.push(to);
  61. }
  62. }
  63. }
  64. }
  65. return ;
  66. }
  67. int main()
  68. {
  69. int s,i;
  70. double k;
  71. while(scanf("%d%d%d%lf",&n,&m,&s,&k)!=EOF)
  72. {
  73. int a,b;
  74. double c,d,e,f;
  75. while(m--)
  76. {
  77. scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);
  78. AddEdge(a,b,c,d);
  79. AddEdge(b,a,e,f);
  80. }
  81. if(SPFA(s,k))
  82. {
  83. puts("YES");
  84. }
  85. else
  86. {
  87. puts("NO");
  88. }
  89. }
  90. return ;
  91. }
  1.  
  1.  

Currency Exchange 货币兑换 Bellman-Ford SPFA 判正权回路的更多相关文章

  1. poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)

    链接:poj 1860 题意:给定n中货币.以及它们之间的税率.A货币转化为B货币的公式为 B=(V-Cab)*Rab,当中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会添加 分 ...

  2. Currency Exchange POJ - 1860 (spfa判断正环)

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

  3. POJ1680 Currency Exchange SPFA判正环

    转载来源:優YoU  http://user.qzone.qq.com/289065406/blog/1299337940 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币,汇 ...

  4. 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 711 ...

  5. POJ 1860 Currency Exchange(最短路&spfa正权回路)题解

    题意:n种钱,m种汇率转换,若ab汇率p,手续费q,则b=(a-q)*p,你有第s种钱v数量,问你能不能通过转化让你的s种钱变多? 思路:因为过程中可能有负权值,用spfa.求是否有正权回路,dis[ ...

  6. POJ1860-Currency Exchange (正权回路)【Bellman-Ford】

    <题目链接> <转载于 >>> > 题目大意: 有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0. ...

  7. HDU - 1317 ~ SPFA正权回路的判断

    题意:有最多一百个房间,房间之间连通,到达另一个房间会消耗能量值或者增加能量值,求是否能从一号房间到达n号房间. 看数据,有定5个房间,下面有5行,第 iii 行代表 iii 号 房间的信息,第一个数 ...

  8. Bellman_ford货币兑换——正权回路判断

    POJ1860 题目大意:你在某一点有一些钱,给定你两点之间钱得兑换规则,问你有没有办法使你手里的钱增多.就是想看看转一圈我的钱能不能增多,出现这一点得条件就是有兑换钱得正权回路,所以选择用bellm ...

  9. [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)

    Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to tr ...

随机推荐

  1. C#窗口皮肤制作(二):创建窗口库项目以及最小化、最大化、关闭button的实现

    非常高兴有朋友关注这篇博客,同一时候也十分抱歉让关注的朋友久等了,隔上一篇博客也有3个月没有更新,主要是因为3月份辞职,4月份初离职到期离开了北京高德,来到了上海张江.眼下新工作也处于熟悉其中,希望大 ...

  2. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  3. lightbox用法

    示例代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  4. Kali渗透测试2-抓包/DNS工具

    转载请注明出处. TCPDUMP:命令行网络抓包工具tcpdump -h tcpdump version 4.9.2 libpcap version 1.8.1 OpenSSL 1.1.0h 27 M ...

  5. GATK --- wdl 语言

    GATK的pipeline使用WDL进行编写 WDL是一种流程管理语言,内置的支持并行,适合编写pipeline 运行wdl脚本需要两步:第一步编辑参数列表对应的json文件,第二步直接运行Cromw ...

  6. NoSQL入门第二天——Redis入门介绍

    一.基本概述 1.是什么 Redis:REmote DIctionary Server (远程字典服务器) 是完全开源免费的,用C语言编写的,遵守BSD协议, 是一个高性能的(key/value)分布 ...

  7. 2017-2018-1 20155209 实现mypwd

    2017-2018-1 20155209 实现mypwd 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd 首先使用 ...

  8. 一个将当前目录下HEX文件的第一行数据删除的程序

    为什么要写这样一个函数 在使用SoftConsole开发M3程序时,生成的hex文件,必须要把第一行数据删除,才能在Libero中使用,所以写了这个小工具,这是2.0版本了,第一版是直接删除第一行数据 ...

  9. Python: C扩展初体验

    前言 使用 Python 毋庸置疑减少了很多规则约束和开发成本,让我们能够更加专注于逻辑而非语法.但是得此失彼,开发效率提高了,却带来了运行性能的问题,所以就常常被其他门派追着暴打. 身为一个 pyt ...

  10. Oracle10g 客户端安装与配置说明

    1:百度文库 http://wenku.baidu.com/link?url=bA-FrFMaqxkoifwz-oiPeU5QmMVVJyy8rYDBryhTUCJywpkDS0VNJcObCIM8l ...