zoj2770 Burn the Linked Camp --- 差分约束
有n个营地,每一个营地至多容纳Ci人。给出m个条件:第i到第j个营地之间至少有k人。
问n个营地总共至少有多少人。
此题显然差分约束。要求最小值。则建立x-y>=z方程组,建图求最长路。
用d[i]表示[1,i]个帐篷中一共多少人。依据题意可得到不等关系:
1、0<=d[i]-d[i-1]<=C[i]
2、d[j]-d[i]>=k
此外,我们加入0为附加结点,则0到其它点也要建边。
再求解0为源点的最长路就可以。
我的坑点是,判负环返回0。否则返回d[n]。
而d[n]本身就可能是0。
这里要注意下
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#define eps 1e-6
#define ll __int64
#define maxm 20010
#define maxn 1010
using namespace std; struct node
{
int v,w,next;
}e[maxm]; int head[maxn],d[maxn],inq[maxn],outq[maxn],n,m,h; void init()
{
memset(head,-1,sizeof head);
h=0;
} void addedge(int a,int b,int c)
{
e[h].v=b;
e[h].w=c;
e[h].next=head[a];
head[a]=h++;
} int spfa(int st)
{
memset(inq,0,sizeof inq);
memset(outq,0,sizeof outq);
for(int i=0;i<=n;i++)
d[i]=-0xFFFFFF;
d[st]=0;
inq[st]=1;
queue<int> q;
q.push(st);
while(!q.empty())
{
int x=q.front();
q.pop();
inq[x]=0;
outq[x]++;
if(outq[x]>n) return 0;//存在负环
for(int i=head[x];i!=-1;i=e[i].next)
{
if(d[e[i].v]<d[x]+e[i].w)
{
d[e[i].v]=d[x]+e[i].w;
if(!inq[e[i].v])
{
inq[e[i].v]=1;
q.push(e[i].v);
}
}
}
}
return 1;
} int main()
{
int a,b,c;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=1;i<=n;i++)
{
scanf("%d",&c);
addedge(i-1,i,0);
addedge(i,i-1,-c);
addedge(0,i,0);//全部边与附加结点0相连
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a-1,b,c);
}
int ans=spfa(0);
// for(int i=0;i<=n;i++)
// printf("%d ",d[i]);
// puts("");
if(ans==0)
printf("Bad Estimations\n");
else printf("%d\n",d[n]);
}
return 0;
}
zoj2770 Burn the Linked Camp --- 差分约束的更多相关文章
- 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 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...
- ZOJ2770 Burn the Linked Camp(差分约束系统)
区间和一定要联系到前缀和. 这题,把前缀和看作点,从s0到sn: 对于每一个营地i的容量capi,有这么个关系si-si-1<=capi: 对于每一个区间的评估i,j,k,有sj-si-1> ...
- zoj 2770 Burn the Linked Camp (差分约束系统)
// 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...
- 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(spfa&&bellman)
//差分约束 >=求最长路径 <=求最短路径 结果都一样//spfa#include<stdio.h> #include<string.h> #include< ...
- zoj 2770 Burn the Linked Camp
今天刚刚学差分约束系统.利用最短路求解不等式.世界真的好奇妙!感觉不等式漏下几个会导致WA!! #include<cstdio> #include<cstring> #incl ...
- ZOJ 2770 差分约束+SPFA
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
随机推荐
- 阿里云RDS(云数据库)之产品简介
参考阿里产品文档:https://docs.aliyun.com/?spm=5176.100054.3.1.ywnrMX#/pub/rds/product-introduce/overview& ...
- Scala快学笔记(一)
一,基本概念: 1,Scala是一种基于JVM的面向对象和函数式编程语言 2,基本类型:数值类型 ->:Byte,Short,Int,Long,Float,Double和布尔类型:Boolean ...
- Storm如何保证消息不丢失
storm保证从spout发出的每个tuple都会被完全处理.这篇文章介绍storm是怎么做到这个保证的,以及我们使用者怎么做才能充分利用storm的可靠性特点. 一个tuple被"完全处理 ...
- hadoop2.2.0伪分布式安装
修改主机名和IP的映射关系 vi /etc/hosts 192.168.61.134 hadoop 关闭防火墙 #查看防火墙状态 service iptables status #关闭防火墙 serv ...
- N的阶层(王道)
题目描述: 输入一个正整数N,输出N的阶乘. 输入: 正整数N(0<=N<=1000) 输出: 输入可能包括多组数据,对于每一组输入数据,输出N的阶乘 样例输入: 4 5 15 样例输出: ...
- Netty4详解三:Netty架构设计(转)
http://blog.csdn.net/suifeng3051/article/details/28861883?utm_source=tuicool&utm_medium=referral ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- Kinect 1.8 体感开发,手势,姿态(Pose) 捕捉判断方法以及一些辅方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 爪哇国新游记之二十九----访问URL获取输入流
代码: import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import ...
- mac os x 使用scp取代sz rz
在windows下,能够通过secureCRT的sz来下载文件,而用rz来上传文件.在unix/linux下则不必.由于scp命令能够完毕这项目工作. sz file替代方案为:scp usernam ...