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 ...
随机推荐
- Java引用总结--StrongReference、SoftReference、WeakReference、PhantomReference
Java引用总结--StrongReference.SoftReference.WeakReference.PhantomReference 1 Java引用介绍 Java从1.2版本开始引入了4种引 ...
- selenium加载时间过长
为了获取网站js渲染后的html,需要利用selenium加载网站,但是会出现加载时间过长的现象,因此可以限制其加载时间以及强制关掉加载: # !/usr/bin/python3.4 # -*- co ...
- 用JS编写日历的简单思路
提要:本文以写当前时间环境下当月的日历表为例,用最简单的方法实现JavaScript日历,旨在展示JS世界中实用为本.简单为上的解决问题的思路. Web页中的日历一般离不开表格,通常都使用表格装载指定 ...
- 2W+汉字转拼音JS字库(UTF-8生僻字等通用无乱码)
测试页面 <html> <head> <script Language="JavaScript" src="pinyin.js"& ...
- 用于所有Windows驱动程序开发者的概念
用户模式和内核模式 虚拟地址空间 设备节点和设备堆栈 I/O 请求数据包 驱动程序堆栈 微型驱动程序和驱动程序对 Windows 驱动程序工具包中的头文件 为不同版本的 Windows 编写驱动程序
- 全面了解 Linux 服务器 - 3. 查看 Linux 服务器的硬盘使用情况
1)查看硬盘及分区信息 liuqian@ubuntu:~# fdisk -l ...... ...... Disk /dev/sda: 100 GiB, 107374182400 bytes, 209 ...
- Error: Error #2014: Feature is not available at this time. at flash.filesystem::File$/initDocumentsDir()
Error: Error #2014: Feature is not available at this time. at flash.filesystem::File$/initDocumentsD ...
- Unity破解for mac
Mac 版本的破解: 1,确定Unity的版本,然后找到对应的crack包,下载.例如 Unity 4.2.0f4 crack包,我已经放到我的资源里了. 2,解压.这里我解压到了桌面上(也就是 /U ...
- 如何修改SharePoint列表条数等阈值
若要修改SharePoint中对列表最大条数等设定的阈值,可按如下步骤操作: 1. 打开页面:管理中心 > 应用程序管理 > 管理Web应用程序.2. 选择要修改阈值的Web应用程序,并在 ...
- 使用Atlas实现MySQL读写分离
1.MySQL所在机器 192.168.29.128(Master) 192.168.29.129(Slave) 配置好主从同步,参考 http://www.cnblogs.com/luxh/p/40 ...