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 .... 可以用最短路的方法来求的解. 最短路的松弛操作,和这些式子很相近. 如果 ...
随机推荐
- 使用IIS6.0遇到问题后,常用的几种解决方法
1.检查 .Net Framework,是否安装完全,不确定的情况下使用:aspnet_regiis.exe -i 或者 aspnet_regiis.exe -r 2.检查 IIS 6.0 其它相关配 ...
- hdu 5310 Souvenir(BestCoder 1st Anniversary ($))
http://acm.hdu.edu.cn/showproblem.php?pid=5310 题目大意:要买n个纪念品,可以单个买p元每个,可以成套买q元一套,每套有m个,求最少花费 #include ...
- oracle学习 七 拼接变量+日期函数(持续更)
select count(KEYCODE) from STHSGDOC.ZJSJJL where ysrq=to_date(to_char(sysdate,'yyyy')||'/1','yyyy/MM ...
- MetaQ安装部署文档
一.MetaQ安装部署情况: 地点 IP Broker ID Master/Slave Slave ID:Group 合肥 192.168.52.23 Slave 1:meta-slave-group ...
- Struts2 高危漏洞补丁版本为: Struts 2.3.15.1
Struts2 昨天爆出高危漏洞,黑客利用这个漏洞可以执行任意命令(包括恶意的jsp代码),轻松绕过您的验证系统,登陆您的网站后台,使您的网站后台密码形同虚设!! 目前Struts2官方已经发布了一个 ...
- 开机自动播放音乐的vbs
今天无意间看到了vbs这小玩意,就突发奇想,自学了一下,倒弄出如下的小玩意,大牛勿喷!这个可用做撩妹神技也可以用于提醒自己!使用方法:复制程序到txt文本里面保存,然后改后缀为vbs,丢到C:\Pro ...
- Java Service Wrapper配置详解
#encoding=UTF-8 # Configuration files must begin with a line specifying the encoding # of the the fi ...
- [转]Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
Android事件分发机制 该篇文章出处:http://blog.csdn.net/guolin_blog/article/details/9097463 其实我一直准备写一篇关于Android事件分 ...
- ubuntu: qemu+gdb 调试linux kernel 学习笔记
声明: 本笔记内容并非本人原创,90%来自网络资料的整合.同时,由于自己是刚刚接触qemu & gdbserver remote debug,本文也就算不得教程,仅供有缘人参考而已. ---- ...
- ASP.net中GridView中增加一行记录并默认显示为编辑状态
//添加 protected void Button1_Click(object sender, EventArgs e) { DataSet ds = (DataSet)pa.GetDataSet( ...