ZOJ 2770火烧连营——差分约束
偶尔做了一下差分约束。
题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵。
————————————————————————————————————————————————---
差分约束:就是利用多个不等式来推导另一个不等式。
由于不等式a-b<=c和求最短路径时的三角形不等式相同,就变成了求最短路。
所有不等式化为a-b<=c的形式,则建造b到a的边,权为c。
求a到b的最短距离,则转化为b-a<=c,距离的值为c。
该题中:
Si表示从第一营到i营的人数,则有:
每个营的人数不少于0,Si-Si-1>=0,进而推出Si-1-Si<=0;
每个营的人数不大于Ci,从而推出Si-Si-1<=Ci;
第i营到第j营人数不少于k,从而推出Sj-Si-1>=k,进而推出Si-1-Sj<=-k
从以上三组不等式分别建边,组成图。
求的是所有应最少有多少人,即Sn-S0>=x,求的就是x.
从上式推出S0-Sn<=-x,所以从Sn求到S0的最短路,就是x的相反数。
————————————————————————————————————————————————————
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
int n,m;
struct edge
{
int u,v,w,next;
}e[];
int head[],js;
int dis[];
int inqt[];
bool inq[];
queue<int>q;
void init()
{
memset(head,,sizeof(head));
js=;
}
void readint(int &x)
{
char c=getchar();
int f=;
for(;c>''||c<'';c=getchar())if(c=='-')f=-f;
x=;
for(;c>=''&&c<='';c=getchar())x=x*+c-'';
x*=f;
}
void addage(int u,int v,int w)
{
e[++js].u=u;e[js].v=v;e[js].w=w;
e[js].next=head[u];head[u]=js;
}
bool spfa()
{
memset(dis,0x7f,sizeof(dis));
memset(inq,,sizeof(inq));
memset(inqt,,sizeof(inqt));
dis[n]=;
inqt[n]=;
q.push(n);
inq[n]=;
while(!q.empty())
{
int u=q.front();
q.pop();
inq[u]=;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(dis[v]>dis[u]+e[i].w)
{
dis[v]=dis[u]+e[i].w;
if(!inq[v])
{
q.push(v);
inq[v]=;
inqt[v]++;
if(inqt[v]>n)return ;
}
}
}
}
return ;
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
init();
for(int c,i=;i<=n;i++)
{
readint(c);
addage(i-,i,c);
addage(i,i-,);
}
for(int a,b,c,i=;i<m;i++)
{
readint(a);readint(b);readint(c);
addage(b,a-,-c);
}
if(spfa())printf("%d\n",-dis[]);
else printf("Bad Estimations\n");
}
return ;
}
ZOJ 2770火烧连营——差分约束的更多相关文章
- 约分差束 例题 ZOJ 2770 火烧连营
题目来源:ZOJ Monthly, October 2006, ZOJ2770题目描述:大家都知道,三国时期,蜀国刘备被吴国大都督陆逊打败了.刘备失败的原因是刘备的错误决策.他把军队分成几十个大营,每 ...
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)
题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...
- ZOJ 2770 Burn the Linked Camp 差分约束 ZOJ排名第一~
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...
- ZOJ 2770 Burn the Linked Camp 差分约束
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2770 Burn the Linked Camp Time Limi ...
- ZOJ2770-Burn The Linked Camp(火烧连营Orz 差分约束-线性约束+最长路(OR反向最短路))
It is well known that, in the period of The Three Empires, Liu Bei, the emperor of the Shu Empire, w ...
- zoj 2770 Burn the Linked Camp (差分约束系统)
// 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...
- zoj 1508 Intervals (差分约束)
Intervals Time Limit: 10 Seconds Memory Limit: 32768 KB You are given n closed, integer interva ...
- 2014 Super Training #6 B Launching the Spacecraft --差分约束
原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...
- Candies-POJ3159差分约束
Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...
随机推荐
- hmt5初学小技巧
图片替换技术: 1.利用行高 .hanggao{ width: 154px; height: 30px; background-image: url(../img/logo_db.png); over ...
- Restful 支持 自定义序列化
[ServiceContract] [ServiceKnownType(typeof(HRAwardObject))] [ServiceKnownType(typeof(WorkflowBasicIn ...
- bootstrap表格内容垂直居中
td{ vertical-align: middle !important;}
- 如何解决虚拟机克隆导致"Bringing up interface eth0: Error: No suitable device found: no device found for connection 'System eth0'."
在VMware的虚拟机中克隆CentOS,在重启网卡的时候报错: Bringing up interface eth0: Error: No suitable device found: no de ...
- SVD分解的理解[转载]
http://www.bfcat.com/index.php/2012/03/svd-tutorial/ SVD分解(奇异值分解),本应是本科生就掌握的方法,然而却经常被忽视.实际上,SVD分解不但很 ...
- log4net 发布到生产环境不写日志的解决方法--使用 NLog日志
1.升级到log4net的最新版 PM下执行 Install-Package log4net 还是无法解决的,使用下面的方法 2.使用Nlog替换之,详见https://github.com/NLog ...
- Unity3D学习笔记
双击或F-居中显示对象 Alt-旋转场景 Align With View-正视主镜头 添加质量 使成为预制物体, 即flash中元件, 预制物体在Hierarchy中名字成蓝色, Assets是的对象 ...
- Flex中设置Camera的视频清晰质量的最佳配合
今天需要设置Flex中的Camera组件的一些属性,后来发现清晰度不是很高,于是捣鼓了上午半天,设置了很多的参数,竟然发现手册上就是有现成的一些设置方法,郁闷!不过我还是专门设置了几个有用和必要的属性 ...
- 重置SQL Server连接池
EXEC sp_configure 'show advanced options', 1 GO -- To update the currently configured value for adva ...
- IE请求访问的设置
1:问题 目前有个项目采用AngularJs发送Restful风格的请求的方式来实现前后端的通信,测试人员在使用IE浏览器进行测试的时候发现,有时候请求发送不到后台,后来经查找发现是由于IE浏览器的默 ...