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 .... 可以用最短路的方法来求的解. 最短路的松弛操作,和这些式子很相近. 如果 ...
随机推荐
- 如何用 redis 造一把分布式锁
基本概念 锁 wiki:In computer science, a lock or mutex (from mutual exclusion) is a synchronization mechan ...
- HDU 2147 kiki's game (简单博弈,找规律)
kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/1000 K (Java/Others)Total ...
- js上三行下三行和添加多个附件
function addTr(num) { no ++; var obj = document.getElementById(tableID); var oneRow = obj.insertRow( ...
- area标签circle/rect/poligon坐标
<img src="images/02.gif" title="flower" usemap="#mm" /> <map ...
- JS点击任意标签获得该标签属性,以获得ID为例,以及AJAX的异步原理和 $(document).ready()与window.onload加载方法的区别
js代码: //$(document).click(function (e) { // 在页面任意位置点击而触发此事件 // var select = ""; // var i = ...
- svg base64
好多h5页面有出现data:image/png;base64,后面跟了一串类似乱码的字母 查了下原来这也是svg或者是图片 <img src=“data:image/png;base64,iVB ...
- CCF 201403-2 窗口 (STL模拟)
问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示位于顶层的窗口里的 ...
- VS2008注册码
PYHYP-WXB3B-B2CCM-V9DX9-VDY8T 如果下载的是90天的试用版,下载下来以后把ISO里面的Setup\setup.sdb文件用记事本打开,把其中的[Product Key]下面 ...
- 设置UIButton文字大小颜色不同
_loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(iconX, CGRectGetMaxY(passwordBGView.frame)+25, ...
- 规范打log
在公司工作快3年了,debug用的最多的还是分析程序打出来的log. 怎样打log,打什么样的log,也是很值得研究的事情.好的打log方式,能够很快的分析和解决问题. 下面总结两点: 1.在log中 ...