偶尔做了一下差分约束。

题目大意:给出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火烧连营——差分约束的更多相关文章

  1. 约分差束 例题 ZOJ 2770 火烧连营

    题目来源:ZOJ Monthly, October 2006, ZOJ2770题目描述:大家都知道,三国时期,蜀国刘备被吴国大都督陆逊打败了.刘备失败的原因是刘备的错误决策.他把军队分成几十个大营,每 ...

  2. POJ 1201 &amp; HDU1384 &amp; ZOJ 1508 Intervals(差分约束+spfa 求最长路径)

    题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...

  3. ZOJ 2770 Burn the Linked Camp 差分约束 ZOJ排名第一~

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...

  4. ZOJ 2770 Burn the Linked Camp 差分约束

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2770 Burn the Linked Camp Time Limi ...

  5. 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 ...

  6. zoj 2770 Burn the Linked Camp (差分约束系统)

    // 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...

  7. zoj 1508 Intervals (差分约束)

    Intervals Time Limit: 10 Seconds      Memory Limit: 32768 KB You are given n closed, integer interva ...

  8. 2014 Super Training #6 B Launching the Spacecraft --差分约束

    原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...

  9. Candies-POJ3159差分约束

    Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...

随机推荐

  1. 列表框QListWidget类

    QListWidget类也是GUI中常用的类,它从QListView下派生: class Q_GUI_EXPORT QListWidget : public QListView { Q_OBJECT ...

  2. Alpha版本——Postmortem会议

    No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 设想和目标 1.我们的软件要解决什么 ...

  3. 观察者模式及Java实现例子

    http://www.cnblogs.com/mengdd/archive/2013/02/07/2908929.html 观察者模式 观察者模式 Observer 观察者模式定义了一种一对多的依赖关 ...

  4. Correlation Filter in Visual Tracking系列一:Visual Object Tracking using Adaptive Correlation Filters 论文笔记

    Visual Object Tracking using Adaptive Correlation Filters 一文发表于2010的CVPR上,是笔者所知的第一篇将correlation filt ...

  5. 每天一个Linux命令(3):pwd命令

    Linux中用 pwd 命令来查看"当前工作目录"的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文 ...

  6. Spring Boot 性能优化

    spring 框架给企业软件开发者提供了常见问题的通用解决方案,包括那些在未来开发中没有意识到的问题.但是,它构建的 J2EE 项目变得越来越臃肿,逐渐被 Spring Boot 所替代.Spring ...

  7. 【javascript基础】1、基本概念

    前言 最近迷茫了一段时间,不知道应该从何处开始学习前端知识,好像这种状态已经持续了一年了,天天也在看前端的东西,但是记住的多少或者说在脑中一团糟,没有什么清晰的概念.最近加入了jQuery源码交流群( ...

  8. petapoco IsNew

    // Check if a poco represents a new record public bool IsNew(string primaryKeyName, object poco) { v ...

  9. 剑指Offer:面试题19——二叉树的镜像(java实现)

    问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...

  10. oracle连接的三个配置文件(转)

           Oracle中TNS的完整定义:transparence Network Substrate透明网络底层,监听服务是它重要的一部分,不是全部,不要把TNS当作只是监听器 ORACLE当中 ...