题目描述

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 - c) * r ,求出是否存在正权回路,且货币数增加。

思路:

使用ford 的判圈方法。总共需要循环所有的边n - 1次,松弛一遍,松弛完之后如果还出现满足条件的情况则一定存在正环

代码如下:

#include<iostream>
#include<vector>
#include<cstring>
#define INF 0x3f3f3f3f
#include<queue>
#include<cstdio>
#include<cmath>
using namespace std;
int a[1005], b[1005];
double c[1005], d[1005];
double dis[1005];
int n, m, s, k;double v;
bool djs()
{memset(dis, 0, sizeof dis);  //dis表示兑换成每一种类之后的钱数dis[s] = v;    //转化s种类的钱数为vfor(int i = 1; i < n; i++){bool flag = false;for(int j = 1; j <= k; j++){if(dis[b[j]] < (dis[a[j]] - d[j])*c[j]){  //先松弛一遍dis[b[j]] = (dis[a[j]] - d[j])*c[j];flag = true;}}}for(int i = 1; i <= k; i++){        if(dis[b[i]] < (dis[a[i]] - d[i])*c[i])  //松弛完一遍后,如果还满足条件,则一定存在正环return true;}return false;  //否则,return false
}
int main()
{scanf("%d %d %d %lf",&n, &m, &s, &v);k = 1;int A, B;double C, D, E, F;for(int i = 0; i < m; i++){cin >> A >> B >> C >> D >> E >> F;a[k] = A;b[k] = B;c[k] = C;d[k++] = D;a[k] = B;b[k] = A;c[k] = E;d[k++] = F;}if(djs()) cout << "YES" << endl;else cout << "NO" <<endl;return 0;
}

Currency Exchange(判断有无正环)相关推荐

  1. POJ1680 Currency Exchange SPFA判正环

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

  2. I - Arbitrage(判断是否有无正环 II)

    题目描述 Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a cur ...

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

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

  4. Currency Exchange——最短路Bellman-Ford算法

    [题目描述] Several currency exchange points are working in our city. Let us suppose that each point spec ...

  5. *【POJ - 1860】Currency Exchange (单源最长路---Bellman_Ford算法判正环)

    题干: Description Several currency exchange points are working in our city. Let us suppose that each p ...

  6. Arbitrage(判断正环 spfa写法)

    题目如下: Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a cu ...

  7. Arbitrage——判断正环Bellman-Ford/SPFA

    [题目描述] Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a c ...

  8. hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  9. 货币兑换(判断正环)

    货币兑换 POJ - 1860 我们城市有几个货币兑换点.让我们假设每个点专门研究两种特定的货币,并且只与这些货币进行兑换操作.可以有多个点专门用于同一对货币.每个点都有自己的汇率,A到B的汇率就是1 ...

最新文章

  1. mybatis mysql方言_MyBatis 方言支持 - Mysql to 华为高斯数据库(gaussdb)
  2. div地址跳转 vue_vue---导航栏点击跳转到对应位置
  3. 15道谷歌面试题及答案
  4. Android 项目中常用到的第三方组件
  5. hdu 1176 馅饼
  6. Java的面试汇总,有这些还担心不通过?
  7. Ubuntu8.10安装小记
  8. 今日头条的排名算法_今日头条的推荐算法原理分析
  9. DGA 域名生成算法攻防
  10. 解决-手机通过Charles连接代理无法上网,亲测可用
  11. 小tips:页面滚动到关闭时的位置与不滚动
  12. 获取屏幕分辨率和刷新率
  13. 低通滤波器 截止频率 学习笔记
  14. 第三方服务之Bmob——快速入门
  15. 招投标概念及注意事项
  16. 短信验证注册,一个完整而优雅的JAVA后端实现
  17. 2022年15款实用有趣的小程序推荐。
  18. PL/SQL Developer配置使用说明
  19. C++基本语法知识查漏补缺(一)
  20. 画中画效果自由制作,视频、图片都可制作

热门文章

  1. esper(4-2)-Category Context
  2. 在ionic2中集成swiper插件
  3. 设计模式:单例模式7种写法
  4. HDU 2686 多线程DP
  5. [Teaching] [Silverlight] 30秒快速建立遊戲迴圈 (Game Loop)
  6. XSLT的处理模型(1)
  7. GitHub如何在README.md文件中插入图片
  8. python之yield
  9. git clone加速(github加速)
  10. 设计模式08_适配器