Burn the Linked Camp(bellman 差分约束系统)
Burn the Linked Camp
Time Limit: 2 Seconds Memory Limit: 65536 KB
It is well known that, in the period of The Three Empires, Liu Bei, the emperor of the Shu Empire, was defeated by Lu Xun, a general of the Wu Empire. The defeat was due to Liu Bei's wrong decision that he divided his large troops into a number of camps, each of which had a group of armies, and located them in a line. This was the so-called "Linked Camps".
Let's go back to that time. Lu Xun had sent many scouts to obtain the information about his enemy. From his scouts, he knew that Liu Bei had divided his troops into n camps, all of which located in a line, labeled by 1..n from left to right. The ith camp had a maximum capacity of Ci soldiers. Furthermore, by observing the activities Liu Bei's troops had been doing those days, Lu Xun could estimate the least total number of soldiers that were lived in from the ith to the jth camp. Finally, Lu Xun must estimate at least how many soldiers did Liu Bei had, so that he could decide how many troops he should send to burn Liu Bei's Linked Camps.
Input:
There are multiple test cases! On the first line of each test case, there are two integers n (0<n<=1,000) and m (0<=m<=10,000). On the second line, there are n integers C1��Cn. Then m lines follow, each line has three integers i, j, k (0<i<=j<=n, 0<=k<2^31), meaning that the total number of soldiers from the ith camp to the jth camp is at least k.
Output:
For each test case, output one integer in a single line: the least number of all soldiers in Liu Bei's army from Lu Xun's observation. However, Lu Xun's estimations given in the input data may be very unprecise. If his estimations cannot be true, output "Bad Estimations" in a single line instead.
Sample Input:
3 2
1000 2000 1000
1 2 1100
2 3 1300
3 1
100 200 300
2 3 600
Sample Output:
1300
Bad Estimations 注意dij只能处理非负权值的最短路问题, 所以差分约束系统一律用 bellman()
注意:本题应该是用spfa做的,不然bellman O(nm)算法很容易超时, 但此题不但没有超时,竟然能在100ms的时间内通过。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
using namespace std;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; struct edge
{
int u,v,w;
}edges[*MS]; int n,m,esize;
int dis[MS];
int C[MS];
int maxv[MS]; int input()
{
if(scanf("%d%d",&n,&m)==EOF)
return ;
/*
c[i]>=s[i]-s[i-1]>=0;
maxv[v]-maxv[u-1]>=s[v]-s[u-1]>=w;
四个约束条件
*/
//因为 s[n]-s[i]>=0; ==> s[i]<=s[n]+0, n->i w=0;
// 所以dis初始化为 0;
memset(dis,,sizeof(dis));
memset(maxv,,sizeof(maxv));
esize=;
for(int i=;i<=n;i++)
{
scanf("%d",&C[i]);
maxv[i]=C[i]+maxv[i-];
edges[esize].u=i-;
edges[esize].v=i;
edges[esize++].w=C[i]; edges[esize].u=i;
edges[esize].v=i-;
edges[esize++].w=;
}
int u,v,w;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
edges[esize].u=v;
edges[esize].v=u-;
edges[esize++].w=-w; edges[esize].u=u-;
edges[esize].v=v;
edges[esize++].w=maxv[v]-maxv[u-];
}
return ;
} bool bellman()
{
for(int i=;i<=n;i++) //千万注意定点个数是n+1,不然会错。
{
for(int j=;j<esize;j++)
{
if(dis[edges[j].u]+edges[j].w<dis[edges[j].v])
{
dis[edges[j].v]=dis[edges[j].u]+edges[j].w;
if(i==n)
return false;
}
}
}
return true;
} int main()
{
while(input())
{
if(bellman())
printf("%d\n",dis[n]-dis[]);
else
printf("Bad Estimations\n");
}
return ;
}
Burn the Linked Camp(bellman 差分约束系统)的更多相关文章
- 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 差分约束
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2770 Burn the Linked Camp Time Limi ...
- 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 (差分约束系统)
// 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...
- 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 Burn the Linked Camp --- 差分约束
有n个营地,每一个营地至多容纳Ci人.给出m个条件:第i到第j个营地之间至少有k人. 问n个营地总共至少有多少人. 此题显然差分约束.要求最小值.则建立x-y>=z方程组,建图求最长路. 用d[ ...
- ZOJ 2770 Burn the Linked Camp 差分约束 ZOJ排名第一~
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...
- zoj2770 差分约束系统
zoj1770 x1- x2 <= t1 x3 - x5 <= t2 x2 - x3 <= t3 .... 可以用最短路的方法来求的解. 最短路的松弛操作,和这些式子很相近. 如果 ...
随机推荐
- CodeForces 711B Chris and Magic Square (暴力,水题)
题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...
- js set
function ff(){ var sArray = ['a','b','b','b','b','b','d','b']; var iSet = {}; for(var i=0;i<sArra ...
- .NET文件上传的大小限制配置
<system.web> <!--maxRequestLength单位是Kb--> <httpRuntime maxRequestLength="20971 ...
- 用来用去还是觉得SDCMS好用
用来用去还是觉得SDCMS好用 现在可以算是精通了.呵呵,欢迎交流
- [SQL]sql语句bug
sql语句格式必须严格检查,一个空格的错误都会导致执行错误. 常见有 1:字符串的值要用 ‘ ’ 括起来 2:用 , 分隔语句段 3:切记判断条件前有一空格:eg:这里最容易出错 4:属性名是否 ...
- 设置ul阴影效果和边框圆角
ul.box {position: relative;z-index: 1; /* prevent shadows falling behind containers with backgrounds ...
- UI:基础
App的生命周期 参考 多态的使用 // // main.m #import <Foundation/Foundation.h> #import "SingleDog.h&quo ...
- BeanFactory和ApplicationContext的作用和区别
BeanFactory和ApplicationContext的作用和区别 作用: 1. BeanFactory负责读取bean配置文档,管理bean的加载,实例化,维护bean之间的依赖关系,负责be ...
- javaScript hook
今天在网上搜索了不少资料,基本概念如下: 钩子(Hook),是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的.当消息到 ...
- sudo权限集中管理用法
#定义一组命令集合,名称DBA_CMD,禁止使用的命令前加!即可Cmnd_Alias DBA_CMD = /bin/touch,/bin/mkdir,/sbin/service,/sbin/chkc ...