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

题意:N种货币,M个兑换点,S开始的货币类型,V开始拥有的货币数
给出M个兑换点的信息
a b 代表兑换的两种货币,然后给出a兑换b的汇率和佣金,b兑换a的汇率和佣金
问是否可以经过多轮兑换后使得本金变多 思路:spfa判断正环
两种方法 cnt【y】++ ,直到cnt【y】 >= n (y代表放入队列的点,也就是松弛点)
    cnt【y】+=cnt【x】,直到cnt【y】>=n
当然这题也可以判断dist【v】 > V?(表示起点)
 #include<cstdio>
#include<cstring>
#include<queue> using namespace std; int n,m,s;
double v; struct Node
{
int y;
double val;
double sub;
int next;
Node(int y=,double val=,double sub = ,int next=):y(y),val(val),sub(sub),next(next) {}
} node[]; int cnt,head[];
void add(int x,int y,double val,double sub)
{
node[++cnt].y=y;
node[cnt].next=head[x];
node[cnt].val=val;
node[cnt].sub=sub;
head[x]=cnt;
}
queue<int>que;
double dist[];
int vis[];
int tot[];
int num[];
bool spfa()
{
while(!que.empty())que.pop();
memset(vis,,sizeof(vis));
memset(dist,,sizeof(dist));
memset(num,,sizeof(num));
que.push(s);
dist[s] = v;
while(!que.empty())
{ int from = que.front();
que.pop();
vis[from] = ;
for(int i=head[from]; i; i=node[i].next)
{
int to = node[i].y;
if(dist[to] < (dist[from]-node[i].sub)*node[i].val)
{
dist[to] = (dist[from]-node[i].sub)*node[i].val;
if(!vis[to])
{
que.push(to);
vis[to]=;
num[to]++;
if(num[to]>= n)return ;
}
}
}
// if(dist[s] > v)return 1;
}
return ;
} int main()
{
scanf("%d%d%d%lf",&n,&m,&s,&v);
for(int i=; i<=m; i++)
{
int u,v;
double a,b,c,d;
scanf("%d%d",&u,&v);
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
add(u,v,a,b);
add(v,u,c,d);
}
int ans = spfa();
if(ans)printf("YES\n");
else printf("NO\n");
}
												

Currency Exchange POJ - 1860 (spfa判断正环)的更多相关文章

  1. Currency Exchange POJ - 1860 spfa判断正环

    //spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...

  2. Currency Exchange POJ - 1860 (spfa)

    题目链接:Currency Exchange 题意: 钱的种类为N,M条命令,拥有种类为S这类钱的数目为V,命令为将a换成b,剩下的四个数为a对b的汇率和a换成b的税,b对a的汇率和b换成a的税,公式 ...

  3. poj 1860 (Bellman_Ford判断正环)

    题意:给出n种货币,m中交换关系,给出两种货币汇率和手续费,求能不能通过货币间的兑换使财富增加. 用Bellman_Ford 求出是否有正环,如果有的话就可以无限水松弛,财富可以无限增加. #incl ...

  4. poj1860 Currency Exchange(spfa判断正环)

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

  5. poj3621 SPFA判断正环+二分答案

    Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big c ...

  6. (最短路 SPFA)Currency Exchange -- poj -- 1860

    链接: http://poj.org/problem?id=1860 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2326 ...

  7. HDU 1317(Floyd判断连通性+spfa判断正环)

    XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  8. HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)

    XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  9. Currency Exchange 货币兑换 Bellman-Ford SPFA 判正权回路

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

随机推荐

  1. 野路子码农系列(2)Python中的类,可能是最通俗的解说

    啥叫佩奇?啥叫类?啥叫面向对象?后面两个问题以前在大学里“祖传谭浩强”的时候我经常会有所疑问.老师说着一堆什么public, private,我都是一脸懵逼,啥叫私有?为啥要私有?然后就神游天外了…… ...

  2. Java基础--异常处理

    1.异常的传统处理方式 缺点: [1] 通过判断影响执行效率. [2] 判断逻辑和业务逻辑交织在一起,可维护性很差. public class Test01 { public static void ...

  3. JS基础-第5天

    复习函数 函数 作用:封装一段代码,封装的功能可以被反复调用执行. 定义函数 // 第一种 - 函数声明 function 函数名(){ } // 第二种 - 函数表达式 var 函数名 = func ...

  4. docker安装elasticsearch

    docker search elasticsearch 选择一个版本,拉取镜像 docker pull elasticsearch: 查看镜像 docker images 通过镜像,启动一个容器,并将 ...

  5. Linux VPS通过安装CPULimit来限制CPU使用率

    说明:我们手上经常有很多廉价的VPS,有时候使用某些软件应用的时候,会出现CPU跑满的情况,而长时间跑满会被VPS商家停掉,所以这里我们需要想办法来限制进程CPU使用率,这里就说个教程. 简介 cpu ...

  6. es过滤集提升权重

    es { "query":{ "function_score":{ "query":{ "match":{ " ...

  7. JS判断页面是在浏览器还是微信打开

    一.Navigator对象 1.获取用户的浏览器信息. let ua = navigator.userAgent.toLowerCase(); 打印一下ua的结果: Mozilla/5.0 (Maci ...

  8. OSL

    1,SimpleColorShader: shader gamma(color cin = color(,,),output color Cout=color(,,)) { Cout = cin; } ...

  9. 在Linux搭建Git服务器

    搭建Git服务器 https://www.cnblogs.com/dee0912/p/5815267.html Git客户端的安装 https://www.cnblogs.com/xuwenjin/p ...

  10. 2019 icpc南昌全国邀请赛-网络选拔赛J题 树链剖分+离线询问

    链接:https://nanti.jisuanke.com/t/38229 题意: 给一棵树,多次查询,每次查询两点之间权值<=k的边个数 题解: 离线询问,树链剖分后bit维护有贡献的位置即可 ...