有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 --- 差分约束的更多相关文章

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

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

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

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

  3. ZOJ2770 Burn the Linked Camp(差分约束系统)

    区间和一定要联系到前缀和. 这题,把前缀和看作点,从s0到sn: 对于每一个营地i的容量capi,有这么个关系si-si-1<=capi: 对于每一个区间的评估i,j,k,有sj-si-1> ...

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

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

  5. Burn the Linked Camp(bellman 差分约束系统)

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

  6. zoj Burn the Linked Camp (查分约束)

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

  7. ZOJ 2770 Burn the Linked Camp(spfa&&bellman)

    //差分约束 >=求最长路径 <=求最短路径 结果都一样//spfa#include<stdio.h> #include<string.h> #include< ...

  8. zoj 2770 Burn the Linked Camp

    今天刚刚学差分约束系统.利用最短路求解不等式.世界真的好奇妙!感觉不等式漏下几个会导致WA!! #include<cstdio> #include<cstring> #incl ...

  9. ZOJ 2770 差分约束+SPFA

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

随机推荐

  1. 向Solr数据集提交Json格式数据(Scala,Post)

    import scalaj.http.Http class SolrAdd () {// 方法接受两个参数,dataType为数据集名称,jsonString为数据json字符串 def postTo ...

  2. netty handlers模式

    netty的handler模式真的挺方便的,可以像插件一样随意的插入自己新增的功能而不用队系统进行大的变动. 下面我们来看一下这个模式是如何实现和运行的. 待续...

  3. 游戏AI的综合设计

    原地址:http://www.cnblogs.com/cocoaleaves/archive/2009/03/23/1419346.html 学校的MSTC要出杂志,第一期做游戏专题,我写了一下AI, ...

  4. POJ3525-Most Distant Point from the Sea(二分+半平面交)

    Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3955   ...

  5. Java 中字符串的格式化

    1.格式字符串语法 产生格式化输出的每个方法都需要格式字符串 和参数列表.格式字符串是一个String,它可以包含固定文本以及一个或多个嵌入的格式说明符.请考虑以下示例: Calendar c = C ...

  6. HTML--百度百科

    超文本标记语言,标准通用标记语言下的一个应用. “超文本”就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素. 超文本标记语言的结构包括“头”部分(英语:Head).和“主体”部分(英语:Bo ...

  7. openerp-server.conf 中配置 dbfilter 参数无效的解决办法

    来自:http://shine-it.net/index.php/topic,14517.html 以前就发现过这个问题, 今天重新在群里同大家讨论了一下. 有时候可能我们希望用户不从登陆界面的账套选 ...

  8. Property with &#39;retain (or strong)&#39; attribute must be of object type

    AFNetworking 2.0 当Deployment Target 低于6.0时,AFURLConnectionOperation.h,AFURLSessionManager.h @propert ...

  9. userDao

    比如,我们这里有一个接口IUserDao,里面有,add和del两个方法.我们在项目中,有一个他的实现类:UserDao.但是我们现在想要统一为这个接口的所有实现类都添加一个查询search方法,那么 ...

  10. Lucene3.0详解

    http://www.open-open.com/lib/view/open1331275900374.html