当前位置:   article > 正文

E - Currency Exchange (spfa)(求最大路径长度,判断是否存在环路)_neo4j环判断

neo4j环判断

滴答滴答---题目链接 

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

YES

大致题意:有多组数据,每组数据首先输入n,m,s,v。n表示有n个货币兑换点,m表示有m组兑换关系,兑点A到兑点B的汇率和手续费可能与兑点B到兑点A的汇率和手续费不同,是双向的。s表示源点,v表示初始金额。接下来的m行每行输入a,b,rab,cad,rba,cba ,表示兑换点a和兑换点b之间的汇率和手续费。问源点s经过一系列的兑换货币后回到源点s时的货币是否能增加,如果能则输出yes,否则输出no。

思路:初始化dis(S)=V 而源点到其他点的距离(权值)初始化为无穷小(0),当s到其他某点的距离能不断变大时,说明存在最大路径;如果可以一直变大,说明存在正环。判断是否存在环路,用Bellman-Ford和spfa都可以。
a点到b点的权值为(v’-cab)*rab b点到a点的权值为(v”-cba)*rba

下面是spfa:

  1. #include<algorithm>
  2. #include<queue>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<vector>
  6. #include<math.h>
  7. using namespace std;
  8. const int N=105;
  9. const int inf=0x3f3f3f;
  10. struct st
  11. {
  12. int b;
  13. double r,c;
  14. st(int b,double r,double c):b(b),r(r),c(c){}
  15. };
  16. vector<st>g[N];
  17. double v[N];
  18. int spfa(int k,double m)
  19. {
  20. queue<int>Q;
  21. Q.push(k);
  22. while(Q.size())
  23. {
  24. int s=Q.front();
  25. Q.pop();
  26. int len=g[s].size();
  27. for(int i=0; i<len; i++)
  28. {
  29. st q=g[s][i];
  30. double p=(v[s]-q.c)*q.r;
  31. if(p>v[q.b])
  32. {
  33. v[q.b]=p;
  34. Q.push(q.b);
  35. }
  36. }
  37. if(v[k]>m)
  38. return 1;
  39. }
  40. return 0;
  41. }
  42. int main()
  43. {
  44. int N,M,s;
  45. double m;
  46. while(scanf("%d%d%d%lf",&N,&M,&s,&m)!=EOF)
  47. {
  48. for(int i=1; i<=N; i++)
  49. {
  50. v[i]=0;
  51. g[i].clear();
  52. }
  53. v[s]=m;
  54. int a,b;
  55. double rab,cab,rba,cba;
  56. for(int i=0; i<M; i++)
  57. {
  58. scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
  59. g[a].push_back(st(b,rab,cab));
  60. g[b].push_back(st(a,rba,cba));
  61. }
  62. int ans=spfa(s,m);
  63. if(ans==1)
  64. printf("YES\n");
  65. else
  66. printf("NO\n");
  67. }
  68. return 0;
  69. }

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/131015
推荐阅读
相关标签
  

闽ICP备14008679号