Currency Exchange

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

Appoint description: 
System Crawler  (2015-05-14)

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. 松弛方法变反,最后检查是否有正环。
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. int N,M,S;
  6. double V;
  7. double D[];
  8. struct Node
  9. {
  10. int from,to;
  11. double r,c;
  12. }G[];
  13.  
  14. bool Bellman_Ford(void);
  15. int main(void)
  16. {
  17. int from,to;
  18. double box;
  19.  
  20. while(scanf("%d%d%d%lf",&N,&M,&S,&V) != EOF)
  21. {
  22. for(int i = ;i < M * ;)
  23. {
  24. scanf("%d%d%lf%lf",&G[i].from,&G[i].to,&G[i].r,&G[i].c);
  25. i ++;
  26. G[i].from = G[i - ].to;
  27. G[i].to = G[i - ].from;
  28. scanf("%lf%lf",&G[i].r,&G[i].c);
  29. i ++;
  30. }
  31. if(Bellman_Ford())
  32. puts("YES");
  33. else
  34. puts("NO");
  35. }
  36. }
  37.  
  38. bool Bellman_Ford(void)
  39. {
  40. bool update;
  41. fill(D,D + ,);
  42. D[S] = V;
  43.  
  44. for(int i = ;i < N - ;i ++)
  45. {
  46. update = false;
  47. for(int j = ;j < M * ;j ++)
  48. if(D[G[j].to] < (D[G[j].from] - G[j].c) * G[j].r && (D[G[j].from] - G[j].c) * G[j].r >= )
  49. {
  50. update = true;
  51. D[G[j].to] = (D[G[j].from] - G[j].c) * G[j].r;
  52. if(D[S] > V)
  53. return true;
  54. }
  55. if(!update)
  56. break;
  57. }
  58. for(int i = ;i < M * ;i ++)
  59. if(D[G[i].to] < (D[G[i].from] - G[i].c) * G[i].r)
  60. return true;
  61. return false;
  62. }

POJ 1860 Currency Exchange (最短路)的更多相关文章

  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 (最短路bellman_ford思想找正权环 最长路)

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

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

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

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

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

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

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

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

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

  7. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

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

  8. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) T ...

  9. POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】

    链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

随机推荐

  1. 优化checkbox和radio,类似Bootstrap中的iCheck

    checkbox和radio浏览器默认的已经满足不了大众的审美需求,更不用说浏览器之间的差异化,取而代之,优化checkbox和radio的方法也随之诞生了. html结构:单选框为例,简单说明: 其 ...

  2. vs2008 release下调试状态设置[转]

    这是一个老生常谈的话题,但还是有时候会漏洞一些设置.总结一些,总共需要三个地方设置, 分别是1)c\c++-> General->Debug Information Format. 2) ...

  3. js 创建List<Map> 这种格式的集合

    //赋值 var list_map = new Array(); for ( var i = 0; i < 10; i++) { list_map.push({baidux:'baidux'+i ...

  4. NAT类型与穿透 及 STUN TURN 协议

    STUN : Simple Traversal of User Datagram Protocol [UDP] Through Network Address Translators [NATs] S ...

  5. Binary Search

    Binary Search                              [原文见:http://www.topcoder.com/tc?module=Static&d1=tuto ...

  6. Thinkphp的Volist标签

    Volist标签主要用于在模板中循环输出数据集或者多维数组. volist标签(循环输出数据) 闭合 非闭合标签 属性 name(必须):要输出的数据模板变量 id(必须):循环变量 offset(可 ...

  7. Sqlite表的结构修改

    Sqlite删除列方法 http://blog.csdn.net/aben_2005/article/details/6563538 SQLite3 table 结构修改 http://blog.cs ...

  8. 深入理解Oracle索引(25):一招鲜、吃遍天之单字段索引创建思路

    本文较短.不过实用性很好.还是记录之.          ㈠ 先别看SQL语句.看执行计划.挑出走全表扫的表 ㈡ 回头看SQL语句.分析上述表的约束字段有哪些.检查各个约束字段的索引是否存在 ㈢ 选择 ...

  9. 「译」JavaScript 的怪癖 1:隐式类型转换

    原文:JavaScript quirk 1: implicit conversion of values 译文:「译」JavaScript 的怪癖 1:隐式类型转换 译者:justjavac 零:提要 ...

  10. scu 4436: Easy Math 水题

    4436: Easy Math Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.scu.edu.cn/soj/problem.actio ...