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 ...
随机推荐
- wdcp的安装扩展模块
其实就是官方包里面的所有附加模块全部支持啦.~~是在官方的基础上修改的优化了每次都解压缩php源码包,按需解压缩使用方法如下wget http://git.oschina.net/loblog/mem ...
- CSS 分组
选择器分组 假设希望 h2 元素和段落都有灰色.为达到这个目的,最容易的做法是使用以下声明: h2, p {color:gray;} 将 h2 和 p 选择器放在规则左边,然后用逗号分隔,就定义了一个 ...
- 过滤HTML代码
public static string FilterHtml(string string_include_html) { string[] HtmlRegexArr ={ #region Html ...
- DB2导出脚本,重新建立数据库
在做项目的时候,我们经常会涉及到数据库的迁移 所以我们需要导出 db2数据的建库脚本,存储过程脚本,函数脚本, 我是这么做的 windows键---cmd---进入命令-----db2cmd----进 ...
- spring + spring mvc可能会遇到的问题
Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器,而SpringMVC(对应mvc_dispatcher_serv ...
- Hadoop学习12-配置集群环境
由于之前虚拟机都是用的桥接方式,有时候没有网络可用,想学习的时候,就狠不方便. 于是研究了一下,希望搭建一个多台虚机组成一个局域网的集群,即host-only方式 1.安装VM,网络选择“host-o ...
- Windows下使用scapy+python2.7实现对pcap文件的读写操作
scapy在linux环境中对pcap文件进行操作非常方便,但在windows下,特别是在python2.7环境下却会碰到各种各样的依赖包无法使用的问题,最明显的可能就属dnet和pcap的pytho ...
- l类型转换错误ClassCastException
出现问题原因story中参数写错:
- 001_JavaScript 错误 - Throw、Try 和 Catch
try 语句测试代码块的错误. catch 语句处理错误. throw 语句创建自定义错误. 错误一定会发生 当 JavaScript 引擎执行 JavaScript 代码时,会发生各种错误: 可能是 ...
- 前端开发框架Bootstrap和KnockoutJS
江湖中那场异常惨烈的厮杀,如今都快被人遗忘了.当年,所有的武林同道为了同一个敌人都拼尽了全力,为数不多的幸存者心灰意冷,隐姓埋名,远赴他乡,他们将唯一的希望寄托给时间.少年子弟江湖老,红颜少女的鬓边也 ...