有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;
}

最新文章

  1. Atitit 深入理解抽象类与接口 attilax总结
  2. ZZULI 1876: 蛤玮的项链 Hash + 二分
  3. php 时间倒计时
  4. source insight新建工程,添加文件时出现“no files found”
  5. winForm 程序开发界面参数传递
  6. (转)分布式缓存GemFire架构介绍
  7. C#通过代码注册COM组件
  8. ERROR: The node /hbase is not in ZooKeeper. It should have been written by the master. Check the value configured in &#39;zookeeper.znode.parent&#39;. There could be a mismatch with the one configured in the
  9. Single Number,Single Number II
  10. (中等) HDU 4979 A simple math problem. , DLX+重复覆盖+打表。
  11. [原创]Nexus5 源码下载、编译、真机烧录过程记录
  12. 国内第一本micropython的书出版《机器人Python极客编程入门与实战》
  13. oracle 表空间管理相关(原创)
  14. 安装splash
  15. [2]朝花夕拾-JAVA注解、PHP注解?
  16. JavaScript学习总结(二、隐式类型转换、eval())
  17. Oracle_高级功能(7) 数据字典视图和动态性能视图
  18. python日志,支持彩色打印和文件大小切片写入和写入mongodb
  19. asp.net &lt;asp:Repeater&gt;下的 asp:LinkButton CommandArgument点击事件
  20. file_get_contents failed to open stream: HTTP request failed(一个字符决定成败)

热门文章

  1. C# 操作mongodb 简单实例
  2. React Router V4发布
  3. ubuntu cp(copy) command
  4. zh-cn en-uk表示语言(文化)代码与国家地区对照表
  5. 对非正确使用浮点型数据而导致项目BUG的问题探讨
  6. 使用history.pushState()和popstate事件实现AJAX的前进、后退功能
  7. 利用pandas进行数据分析之一:pandas数据结构Series
  8. 将python对象序列化成php能读取的格式(即能反序列化到对象)
  9. Linux下默认的目录介绍
  10. unity, write/read txt file