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
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int Inf=0x3f3f3f3f;
struct edge
{
int to;
double r;
double c;
int next;
} edge[maxn]; int n,m,s;
double v;
int len;
int head[maxn];
double dis[maxn];
int vis[maxn];
int num[maxn];
void add(int u,int v,double r,double c)
{
edge[len].to = v;
edge[len].r = r;
edge[len].c = c;
edge[len].next = head[u];
head[u] = len++;
}
bool spfa()
{
queue<int > q;
dis[s] = v;
q.push(s);
vis[s] = ;
num[s]++;
while(!q.empty())
{
int t = q.front();
q.pop();
vis[t] = ;
for(int i = head[t];i!= -;i = edge[i].next)
{
int id = edge[i].to;
if(dis[id]< (dis[t]-edge[i].c)*edge[i].r)
{
dis[id] = (dis[t]-edge[i].c)*edge[i].r;
if(vis[id] == )
{
q.push(id);
vis[id] = ;
num[id]++;
if(num[id]> n)
return ;
}
}
}
}
return ;
}
void init()
{
for(int i = ;i<= n;i++)
dis[i] = -;
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
memset(head,-,sizeof(head));
}
int main()
{
cin>>n>>m>>s>>v;
init();
for(int i = ;i<= m;i++)
{
int a,b;
double ra,rb,ca,cb;
scanf("%d %d %lf %lf %lf %lf",&a,&b,&ra,&ca,&rb,&cb);
add(a,b,ra,ca);
add(b,a,rb,cb);
}
if(spfa())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return ;
}

最新文章

  1. EF Code First 常用命令
  2. iOS崩溃调试的使用和技巧总结
  3. excel插入当前时间快捷键Ctrl+;
  4. 让chrome打开手机网页
  5. PHP命令行模式基本介绍
  6. Pod::Executable pull
  7. 加粗合并latex表格线的加粗及合并两行
  8. gdb/valgrind/coredump to debug c/cpp program
  9. centos7安装图形化界面
  10. django rest framework renderer
  11. # 20175333曹雅坤《Java程序设计》第七周学习总结
  12. HTTP 404 Not Found Error with .woff or .woff2 Font Files
  13. 跟踪SQL
  14. 【转】消除代码中的 if-else/switch-case
  15. SpringMVC(二八) 重定向
  16. [解决]CXF wsdl2java 生成代码存在的一些问题
  17. IOS 多文件上传 Java web端(后台) 使用List&lt;MultipartFile&gt; 接收出现的问题
  18. PTA——猜数字游戏
  19. stdafx.h是什么用处, stdafx.h、stdafx.cpp的作用
  20. Java构建工具_Ant详解

热门文章

  1. [Err] 126 - Incorrect key file for table &#39;/tmp/#sql_1cdc_0.MYI&#39;; try to repair it
  2. 关于Springboot配置文件的理解
  3. Java—增强for循环与for循环的区别/泛型通配符/LinkedList集合
  4. 快速入门Mybatis
  5. jQuery之表单校验:新用户注册
  6. Mybatis-03-日志
  7. Mybatis如何在插入(ID是后台生成的)后返回ID?
  8. Eclipse怎么切换工作空间
  9. Go 编译器内部知识:向 Go 添加新语句-第 2 部分
  10. python基础 Day3