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 ;
}

Currency Exchange(SPFA判负环)的更多相关文章

  1. POJ1680 Currency Exchange SPFA判正环

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

  2. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  3. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  4. spfa判负环

    bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...

  5. poj 1364 King(线性差分约束+超级源点+spfa判负环)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14791   Accepted: 5226 Description ...

  6. 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)

    传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...

  7. BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划

    BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...

  8. [P1768]天路(分数规划+SPFA判负环)

    题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于出现在本次试题上了 ...

  9. LightOj 1221 - Travel Company(spfa判负环)

    1221 - Travel Company PDF (English) Statistics problem=1221" style="color:rgb(79,107,114)& ...

  10. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

随机推荐

  1. UI自动化填写问卷(selenium)+定时任务(懒人必备)

    1.自动填报 UI自动化 selenium 开发程序动机:天天有人催着填写问卷,弄的头大.主要还是懒的每天一个个去填写内容. 开发总时长:2个小时:学习+开发+修改 遇到的小问题: 在自动化填写地图的 ...

  2. 【系统之音】WindowManager工作机制详解

    前言 目光所及,皆有Window!Window,顾名思义,窗口,它是应用与用户交互的一个窗口,我们所见到视图,都对应着一个Window.比如屏幕上方的状态栏.下方的导航栏.按音量键调出来音量控制栏.充 ...

  3. Linux命令持续学习

    1 基础命令 1 jps 查看后台运行的java相关的程序  jvm调优所有 2 ps -ef | grep java 查询后台运行的程序(通过关键字) 3 cd - 回到上一级目录 4 vim之后输 ...

  4. Go之Gorm简介及使用案例

    简介 ORM Object-Relationl Mapping, 它的作用是映射数据库和对象之间的关系,方便我们在实现数据库操作的时候不用去写复杂的sql语句,把对数据库的操作上升到对于对象的操作 G ...

  5. 初识HTML(二)

    目录 HTML表格 HTML列表 HTML表格 表格主要显示.展示数据. 表格基本语法:<table>定义一个表格,<tr>定义表格中的一行,<td>定义一行中的某 ...

  6. ElementUi——el-select下拉框

    <el-select v-model="ruleForm.status" placeholder="请选择状态" @change="styleC ...

  7. QUIC协议详解之Initial包的处理

    从服务器发起请求开始追踪,细说数据包在 QUIC 协议中经历的每一步.大量实例代码展示,简明易懂了解 QUIC. 前言 本文介绍了在 QUIC 服务器在收到 QUIC 客户端发起的第一个 UDP 请求 ...

  8. HTML基础-01

    HTML:超文本标记语言,是一种使用结构化Web网页(标准制定者:W3C)及其内容的标记语言. 发展过程:XHTML5,HTML5,XHTML1.0,HTML4.01,HTML3.2 HTML5特性: ...

  9. angular中a标签带请求头下载excel

    <!DOCTYPE html> <html lang="en" ng-app="app"> <head> <meta ...

  10. Kubernetes 的层级命名空间介绍

    原文链接:https://fuckcloudnative.io/posts/introducing-hierarchical-namespaces/ 在单个 Kubernetes 集群上安全托管大量用 ...