偶尔做了一下差分约束。

题目大意:给出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. android中的AIDL进程间通信

    关于IPC应该不用多介绍了,Android系统中的进程之间不能共享内存,那么如果两个不同的应用程序之间需要通讯怎么办呢?比如公司的一个项目要更新,产品的需求是依附于当前项目开发一个插件,但是呢这个插件 ...

  2. JQuery AJAX 解析获得的JSON数据

    下面的解析的Json是一个二级循环. <!DOCTYPE html> <html> <head> <script src="https://code ...

  3. SoftEnther VPN 在Window的使用

    1.首先下载SoftEnther VPN Client 下载地址 2. 下载后,执行vpngate-client-×××.exe 文件 选择安装一个软件部分: SoftEnther VPN Clien ...

  4. ssh远程连接错误

    在平时工作中,有时候需要SSH登陆到别的Linux主机上去,但有时候SSH登陆会被禁止,并弹出如下类似提示: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  5. Eclipse 配置Maven

    Eclipse 配置Maven 下载Maven 首先在官网下载Maven:http://maven.apache.org/download.cgi 下载后将其解压到相应的位置 配置Maven环境变量 ...

  6. gerrit集成gitweb:Error injecting constructor, java.io.IOException: Permission denied

    使用gerrit账户在centos上安装gerrit,然后集成gitweb,gerrit服务启动失败,查看日志,报错信息如下: [-- ::,] ERROR com.google.gerrit.pgm ...

  7. springmvc跳转和重定向

    如果springmvc返回的视图中带有redirect:或者forward:,springmvc会进行特殊处理, redirect:会执行重定向,forward:会执行转发操作! @RequestMa ...

  8. weblogic 优化设置 http://wenku.baidu.com/view/c42e7a5bbe23482fb4da4cf2.html

    引自:http://wenku.baidu.com/view/c42e7a5bbe23482fb4da4cf2.html

  9. Windows资源管理器 已停止工作

    解决方案:http://jingyan.baidu.com/article/5225f26b6aa830e6fa0908a8.html

  10. PDF 生成插件 flying saucer 和 iText

    最近的项目中遇到了需求,用户在页面点击下载,将页面以PDF格式下载完成供用户浏览,所以上网找了下实现方案. 在Java世界,要想生成PDF,方案不少,所以简单做一个小结吧. 在此之前,先来勾画一下我心 ...