『Candies 差分约束系统』
<更新提示>
<第一次更新>
<正文>
差分约束系统
我们先来认识一下差分约束系统鸭!
差分约束系统是一种特殊的\(n\)元一次不等式组,它包含了\(n\)个变量\(x_1-x_n\)以及\(m\)个不等式(约束条件)。其中每一个不等式形如\(x_i-x_j\leq c_k\),\(c_k\)是常数,\(i,j \leq n,k \leq m\)。
通常来说,题目会给出这一些限制条件的模型或变式,我们需要在满足这个不等式组的前提下求解一些指定的数值。
注意到不等式\(x_i-x_j\leq c_k\)我们可以将其转换为\(x_i\leq c_k+x_j\)。而最短路算法中的三角形不等式也是这个格式\(dis_j \leq dis_i+w\)(对于\(dis_j > dis_i+w\)我们需要更新最短路,故该式一定能得到满足),所以我们通常可以将差分约束系统的问题转化为最短路问题。一般地,我们将变量\(x_i,x_j\)看作图中的两个节点,而恰有一条长度为\(c_k\)的边从\(x_j\)连向\(x_i\)。这时候,边的含义是一个不等式。
更具体地来说,有一个很常见的问题。
对于给定的差分约束系统P,求\(\max\{x_n-x_1\}\)
先从代数角度思考,我们可以通过若干个不等式的相加得到如下的不等式组:
x_n-x_1\leq d_1
\\x_n-x_1\leq d_2
\\...
\\x_n-x_1\leq d_n
\end{cases}
\]
那么显然\(\min\{d_1,d_2,...,d_n\}\)就是答案。
从图论角度思考:先根据差分约束系统建图。
我们可以通过若干条边从不同的路径有节点1走到n。其中各个路径的权值和分别为\(d_1,d_2,...,d_n\),那么\(\min\{d_1,d_2,...,d_n\}\)是什么呢?
- 没错,最短路。
我们将一个差分约束系统的问题转换为了最短路问题。
接下来,我们通过一道入门例题来了解。
Candies(POJ3159)
Description
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.
snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?
Input Format
The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.
Output Format
Output one line with only the largest difference desired. The difference is guaranteed to be finite.
Sample Input
2 2
1 2 5
2 1 4
Sample Output
5
解析
就是以上提到的差分约束系统。
题目大意:有\(n\)个孩子,\(m\)个要求,每一个要求形如:孩子\(a\)认为孩子\(b\)不能比他多超过\(c\)个糖果。求孩子\(1\)和孩子\(n\)最多相差的糖果数。
对于每一个要求,我们将其视为差分约束系统中的一个不等式即可。
\]
(节点a向节点b连一条权值为c的边)
建图后栈式\(SPFA\)(卡堆优化\(Dijkstra\)和队列\(SPFA\))跑最短路解决。
\(Code:\)
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int N=30000+80,M=150000+80;
struct edge{int ver,val,next;}e[M*4];
int n,m,Last[M],t,dis[N],vis[N];
inline void insert(int x,int y,int v)
{
e[++t].val=v;e[t].ver=y;
e[t].next=Last[x];Last[x]=t;
}
inline void input(void)
{
t=0;
memset(Last,0,sizeof Last);
for(int i=1;i<=m;i++)
{
int x,y,v;
scanf("%d%d%d",&x,&y,&v);
insert(x,y,v);
}
}
inline void spfa(void)
{
memset(dis,0x7f,sizeof dis);
memset(vis,0x00,sizeof vis);
dis[1]=0;vis[1]=1;
stack< int > Stack;
Stack.push(1);
while(!Stack.empty())
{
int temp=Stack.top();
Stack.pop();
vis[temp]=0;
for(int i=Last[temp];i;i=e[i].next)
{
if(dis[e[i].ver]>dis[temp]+e[i].val)
{
dis[e[i].ver]=dis[temp]+e[i].val;
if(!vis[e[i].ver])
{
Stack.push(e[i].ver);
vis[e[i].ver]=true;
}
}
}
}
}
int main(void)
{
while(scanf("%d%d",&n,&m)==2)
{
input();
spfa();
printf("%d\n",dis[n]);
}
return 0;
}
<后记>
『Candies 差分约束系统』的更多相关文章
- Candies(差分约束系统)
http://poj.org/problem?id=3159 思路:用O(V+ElogV)的Dijkstra算法求1到n的最短路.即用优先队列优化Dijkstra算法. #include <st ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- 【POJ3159】Candies(差分约束系统)
题意:有一些人, 给n个人派糖果,给出m组约束,每组约束包含A,B,c 三个数, 意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c . 最后求n 比 1 最多多多少糖果 ...
- UVA11478 Halum [差分约束系统]
https://vjudge.net/problem/UVA-11478 给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的 ...
- BZOJ 2330: [SCOI2011]糖果 [差分约束系统] 【学习笔记】
2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5395 Solved: 1750[Submit][Status ...
- js实现『加载更多』功能实例
DEMO : 滚动加载示例 关于如何实现『加载更多』功能,网上有插件可用,例如比较著名的使用iscroll.js实现的上拉加载更多.下拉刷新功能. 但实际用起来却是很麻烦.由于是第三方插件,要按照对方 ...
- ACM/ICPC 之 差分约束系统两道(ZOJ2770-POJ1201)
当对问题建立数学模型后,发现其是一个差分方程组,那么问题可以转换为最短路问题,一下分别选用Bellmanford-SPFA解题 ZOJ2770-Burn the Linked Camp //差分约束方 ...
- 『Asp.Net 组件』Asp.Net 服务器组件 内嵌JS:让自己的控件动起来
代码: using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ...
- POJ1201 Intervals(差分约束系统)
与ZOJ2770一个建模方式,前缀和当作点. 对于每个区间[a,b]有这么个条件,Sa-Sb-1>=c,然后我就那样连边WA了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...
随机推荐
- Linux系统下一个冷门的RAID卡ioc0及其监控mpt-status
新接手了一台Linux服务器,准备检查是否有配置RAID.参考(http://mip.0834jl.com) 先查看是否有RAID卡: 复制代码 代码如下: # dmesg|grep -i raid ...
- 爬虫之selenium和PhantomJS
---恢复内容开始--- selenium selenium是什么? 是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作 环境搭建 .安装: pip instal ...
- BZOJ.4145.[AMPPZ2014]The Prices(状压DP)
BZOJ 比较裸的状压DP. 刚开始写麻烦惹... \(f[i][s]\)表示考虑了前\(i\)家商店,所买物品状态为\(s\)的最小花费. 可以写求一遍一定去\(i\)商店的\(f[i]\)(\(f ...
- windows下编译Boost
当前boost最新版本为1.55,下载地址:http://sourceforge.net/projects/boost/files/boost/1.55.0/或者从官网(www.boost.org)下 ...
- android studio gradle 打jar 包 (混淆+第三方库包)
将依赖的第三方库打包进自己的jar包 1.先将第三方的库包拿到,然后添加jar包到项目的libs. 2.项目的build.gradle脚本添加下面的task: task buildJar(depend ...
- 机器学习(九)隐马尔可夫模型HMM
1.隐马尔可夫HMM模型 一个隐马尔可夫模型可以表示为\[\lambda=\{A,B,\pi\}\]具体就不说了,比较基本. 2.HMM模型的三个基本问题 1.概率计算问题:给定\(\lambda\) ...
- django+javascrpt+python实现私有云盘代码
丁丁:由于篇幅有限,这里暂时只展示python后端代码,前端js代码后面上传,有需要的也可以留言私信我. 1.view.py 使用用户.部门.公司等相关账号的创建,已经个人,部门账号的冻结,删除,相关 ...
- Go语言基础之包
Go语言基础之包 在工程化的Go语言开发项目中,Go语言的源码复用是建立在包(package)基础之上的.本文介绍了Go语言中如何定义包.如何导出包的内容及如何导入其他包. Go语言的包(packag ...
- [Codeforces Round #516][Codeforces 1063C/1064E. Dwarves, Hats and Extrasensory Abilities]
题目链接:1063C - Dwarves, Hats and Extrasensory Abilities/1064E - Dwarves, Hats and Extrasensory Abiliti ...
- 【前端性能】Web 动画帧率(FPS)计算
我们知道,动画其实是由一帧一帧的图像构成的.有 Web 动画那么就会存在该动画在播放运行时的帧率.而帧率在不同设备不同情况下又是不一样的. 有的时候,一些复杂或者重要动画,我们需要实时监控它们的帧率, ...