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. CentOS 7 安装docker (图文)

     一定要用root账号登录系统,打开终端/或远程工具(如xshell)登录系统 检查是否已经安装命令  rpm –qa|grep docker 出现如上说明已安装 也可用命令docker -v  (如 ...

  2. JGUI源码:实现图标按钮及下拉菜单(16)

    效果如下 代码片段如下 <div class="jgui-btn" id="personalbtn" style="float:right;&q ...

  3. TeamViewer 密码有关

    TeamViewer这个密码字母代表g不是q.

  4. What is the difference between __str__ and __repr__ in Python

    from https://www.pythoncentral.io/what-is-the-difference-between-__str__-and-__repr__-in-python/ 目的 ...

  5. Java裸写爬虫技术,运用多线程技术,高效爬取某个医疗机构网站数据

    最近喜欢上了数据的庞大的感觉,就爬取了一下某个医疗机构网站医疗数据,由于数据量庞大,只爬取了江西省的各个市的各个医院的各个科室的各个科室.中各种信息.其中用的持久层技术是hibernate框架,和用到 ...

  6. Springboot自定义异常处理

    1.自定义异常类 import lombok.Data; @Data public class UserException extends RuntimeException { private Lon ...

  7. python学习05

    数据类型之字典dict.set集合 1).字典dict 1. dict_1={'name':'tom','age':18} 是以键值对(key-value)的方式,其中键是可hash值的,即表示键是唯 ...

  8. java接口多实现和多继承

    package test; interface mouth { public abstract void speak(); } interface nose{ public abstract void ...

  9. C#匿名对象(转JSON)

    多类型匿名对象 var result = new { pages = , users = new System.Collections.ArrayList { ,name="}, ,name ...

  10. javascript 正则test、exec、search、match区别?

    都可以放正则表达示 exec是RegExp类的匹配方法 match是字符串类的匹配方法 test() 方法用于检测一个字符串是否匹配某个模式.返回 true,否则返回 false. var resul ...