Currency Exchange(SPFA判负环)
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
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
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判负环)的更多相关文章
- POJ1680 Currency Exchange SPFA判正环
转载来源:優YoU http://user.qzone.qq.com/289065406/blog/1299337940 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币,汇 ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- spfa判负环
bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...
- poj 1364 King(线性差分约束+超级源点+spfa判负环)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14791 Accepted: 5226 Description ...
- 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)
传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...
- BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划
BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...
- [P1768]天路(分数规划+SPFA判负环)
题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于出现在本次试题上了 ...
- LightOj 1221 - Travel Company(spfa判负环)
1221 - Travel Company PDF (English) Statistics problem=1221" style="color:rgb(79,107,114)& ...
- poj 2049(二分+spfa判负环)
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...
随机推荐
- 数电学习笔记之CMOS传输门工作原理
CMOS 传输门从结构上看是由一个PMOS和一个NMOS管组成 先简单粗略讲讲PMOS管和NMOS管导通与截止吧 首先我们MOS管有三个极,源极(S:Source).漏极(D:Drain)和栅极(G: ...
- Linux学习笔记之linux软件包安装以及源的替换
先是软件源的替换,在刚安装的Ubuntu中会配有原先的软件源,所以如果要替换时,可在网上找与自己ubuntu相对应的软件源,比如我的ubuntu版本为12.04,所以我得找到相对应能够适用Ubuntu ...
- 机器学习 | 详解GBDT梯度提升树原理,看完再也不怕面试了
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是机器学习专题的第30篇文章,我们今天来聊一个机器学习时代可以说是最厉害的模型--GBDT. 虽然文无第一武无第二,在机器学习领域并没有 ...
- jquery 效果笔记
jquery效果 显示隐藏 show() 语法 show([speed,[easing],[fn]]) 参数可以省略,无动画直接使用 hide() to ...
- css实现折扇效果
总结思路: 1.首先进行结构的书写 <div class="box"></div> 2.要进行图片的重叠要用到position定位,需要重叠的元素及子元 ...
- C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...
- C#LeetCode刷题之#819-最常见的单词(Most Common Word)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3969 访问. 给定一个段落 (paragraph) 和一个禁用单 ...
- asp.netcore mvc 防CSRF攻击,原理介绍+代码演示+详细讲解
一.CSRF介绍 1.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session ridin ...
- menset与fill
menset函数一般只对int型数组进行0.-1的赋值.原因:menset对数组是按字节赋值,对每个字节的赋值是相同的,故int的4个字节全部被赋相同的值,而0正好二进制编码全为0,-1的二进制编码全 ...
- MapReduce之MapJoin案例
@ 目录 使用场景 优点 具体办法:采用DistributedCache 案例 需求分析 代码实现 使用场景 Map Join 适用于一张表十分小.一张表很大的场景. 优点 思考:在Reduce 端处 ...