ZOJ2770 Burn the Linked Camp(差分约束系统)
区间和一定要联系到前缀和。
- 这题,把前缀和看作点,从s0到sn;
- 对于每一个营地i的容量capi,有这么个关系si-si-1<=capi;
- 对于每一个区间的评估i,j,k,有sj-si-1>=k,即si-1-sj<=k;
- 接下来就是连边了,对于v<=u+w由u向v连权w的边,超级源向n+1个点连权0的边。
- 最后跑SPFA,如果出现负环则无解;而有解的话,所求答案就在d[sn]里,不过因为题目的前缀和是非负整数且要求的最少,所以就要让d中所有数都同时加上合适的数得到另一个真正所求的解。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define MAXN 1111
#define MAXM 22222 struct Edge{
int v,cost,next;
}edge[MAXM];
int head[MAXN],NE;
void addEdge(int u,int v,int cost){
edge[NE].v=v; edge[NE].cost=cost; edge[NE].next=head[u];
head[u]=NE++;
} int n,m,d[MAXN],cnt[MAXN];
bool vis[MAXN];
bool SPFA(){
for(int i=; i<=n; ++i){
vis[i]=; cnt[i]=; d[i]=INF;
}
vis[n+]=; cnt[n+]=; d[n+]=;
queue<int> que;
que.push(n+);
while(!que.empty()){
int u=que.front(); que.pop();
if(cnt[u]>n+) return ;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(d[v]>d[u]+edge[i].cost){
d[v]=d[u]+edge[i].cost;
if(!vis[v]){
vis[v]=;
++cnt[v];
que.push(v);
}
}
}
vis[u]=;
}
return ;
}
int main(){
int a,b,c,cap[MAXN];
while(~scanf("%d%d",&n,&m)){
for(int i=; i<=n; ++i) scanf("%d",cap+i); memset(head,-,sizeof(head));
NE=;
for(int i=; i<=n; ++i) addEdge(i-,i,cap[i]);
for(int i=; i<=n; ++i) addEdge(n+,i,);
while(m--){
scanf("%d%d%d",&a,&b,&c);
addEdge(b,a-,-c);
}
if(SPFA()){
int tmp=INF;
for(int i=; i<=n; ++i) tmp=min(tmp,d[i]);
if(tmp<) d[n]-=tmp;
printf("%d\n",d[n]);
}else puts("Bad Estimations");
}
return ;
}
ZOJ2770 Burn the Linked Camp(差分约束系统)的更多相关文章
- zoj 2770 Burn the Linked Camp (差分约束系统)
// 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...
- zoj2770 Burn the Linked Camp --- 差分约束
有n个营地,每一个营地至多容纳Ci人.给出m个条件:第i到第j个营地之间至少有k人. 问n个营地总共至少有多少人. 此题显然差分约束.要求最小值.则建立x-y>=z方程组,建图求最长路. 用d[ ...
- ZOJ 2770 Burn the Linked Camp 差分约束
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2770 Burn the Linked Camp Time Limi ...
- ZOJ 2770 Burn the Linked Camp 差分约束 ZOJ排名第一~
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...
- Burn the Linked Camp(bellman 差分约束系统)
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
- zoj Burn the Linked Camp (查分约束)
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
- zoj 2770 Burn the Linked Camp
今天刚刚学差分约束系统.利用最短路求解不等式.世界真的好奇妙!感觉不等式漏下几个会导致WA!! #include<cstdio> #include<cstring> #incl ...
- ZOJ 2770 Burn the Linked Camp(spfa&&bellman)
//差分约束 >=求最长路径 <=求最短路径 结果都一样//spfa#include<stdio.h> #include<string.h> #include< ...
- zoj2770 差分约束系统
zoj1770 x1- x2 <= t1 x3 - x5 <= t2 x2 - x3 <= t3 .... 可以用最短路的方法来求的解. 最短路的松弛操作,和这些式子很相近. 如果 ...
随机推荐
- linux配置ant
第1步:下载ant ,地址:http://ftp.twaren.net/Unix/Web/apache//ant/binaries/apache-ant-1.9.6-bin.tar.gz apache ...
- Unity3D中定时器的使用
源地址:http://unity3d.9tech.cn/news/2014/0402/40149.html 在游戏设计过程中定时器是必不可少的工具,我们知道update方法是MonoBehavior中 ...
- urllib2加入header及解决中文乱码问题
def main(): url = "http://www.douban.com" #伪装桌面浏览器 headers = {'User-Agent':'Mozilla/5.0 (W ...
- VirtualBox共享文件夹等高级特性
转自: http://blog.csdn.net/longerzone/article/details/32119457 http://www.oschina.net/translate/10-vir ...
- MySQL使用索引的场景及真正利用索引的SQL类型
1. 为什么使用索引 在无索引的情况下,MySQL会扫描整张表来查找符合sql条件的记录,其时间开销与表中数据量呈正相关.对关系型数据表中的某些字段建索引可以极大提高查询速度(当然,不同字段是否sel ...
- jdk新特性
自动拆装箱子: import org.junit.Test; public class Demo { /* * 自动拆装箱 * */ @Test public void ZhuangXiang() { ...
- C# 支持多种语言
通过Resource文件建立本地化. net 资源文件名(这里是Resource1.resx)由根名称(即Resource1),本地语言名称(默认情况下还没有)及扩展名组成,在读取资源时,资源管理器会 ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- HTML、CSS选择题
Java EE软件工程师认证考试 试题库-选择题 一. 选择题(包括单选和双选) 1.D 以下( )是HTML常用的块状标签(选择一项) A. <span> B. < ...
- Step deep into GLSL
1 Lighting computation is handled in eye space(需要根据眼睛的位置来计算镜面发射值有多少进入眼睛), hence, when using GLSL (GP ...