ZOJ2770-Burn The Linked Camp(火烧连营Orz 差分约束-线性约束+最长路(OR反向最短路))
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
题解:只是一道差分约束的线性约束问题。首先用dis[i]代表从0到i的人数,则0=<dis[i]-dis[i-1]<=a[i],又有 U,V,W得到
dis[V]-dis[U-1]>=W , dis[V]-dis[U-1]<=sum[V]-sum[U-1] ;然后写SPFA就可以啦。
参考代码为(反向最短路):
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxm = 2e5+;
const int maxn = ;
int n,m;
struct edge
{
int v;
int c;
edge(){}
edge(int v,int c):v(v),c(c){}
};
vector<edge>e[maxn];
int a[maxn],sum[maxn],dis[maxn],cnt[maxn];
void addedge(int u,int v,int c)
{
e[u].push_back(edge(v,c));
} void spfa()
{
bool vis[maxn];memset(vis,,sizeof vis);
memset(dis,INF,sizeof dis);
dis[n]=;vis[n]=;
queue<int>q; q.push(n);
while(!q.empty())
{
int pre=q.front();q.pop();
vis[pre]=;
for(int i=;i<e[pre].size();i++)
{
if(dis[e[pre][i].v]>dis[pre]+e[pre][i].c)
{
dis[e[pre][i].v]=dis[pre]+e[pre][i].c;
if(!vis[e[pre][i].v])
{
vis[e[pre][i].v]=;
q.push(e[pre][i].v);
if(++cnt[e[pre][i].v]>sqrt(n))
{
printf("Bad Estimations\n");
return ;
}
}
}
}
}
printf("%d\n",-dis[]);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(sum,,sizeof sum);
memset(cnt,,sizeof cnt);
memset(e,,sizeof e);
int u,v,c;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
}
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&c);
addedge(v,u-,-c);
addedge(u-,v,sum[v]-sum[u-]);
}
for(int i=;i<=n;i++)
{
addedge(i,i-,);
addedge(i-,i,a[i]);
}
spfa();
}
return ;
}
ZOJ2770-Burn The Linked Camp(火烧连营Orz 差分约束-线性约束+最长路(OR反向最短路))的更多相关文章
- ZOJ2770 Burn the Linked Camp(差分约束系统)
区间和一定要联系到前缀和. 这题,把前缀和看作点,从s0到sn: 对于每一个营地i的容量capi,有这么个关系si-si-1<=capi: 对于每一个区间的评估i,j,k,有sj-si-1> ...
- zoj2770 Burn the Linked Camp --- 差分约束
有n个营地,每一个营地至多容纳Ci人.给出m个条件:第i到第j个营地之间至少有k人. 问n个营地总共至少有多少人. 此题显然差分约束.要求最小值.则建立x-y>=z方程组,建图求最长路. 用d[ ...
- 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 差分约束
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2770 Burn the Linked Camp Time Limi ...
- Burn the Linked Camp(bellman 差分约束系统)
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 差分约束 ZOJ排名第一~
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...
- ZOJ 2770 Burn the Linked Camp(spfa&&bellman)
//差分约束 >=求最长路径 <=求最短路径 结果都一样//spfa#include<stdio.h> #include<string.h> #include< ...
- zoj 2770 Burn the Linked Camp
今天刚刚学差分约束系统.利用最短路求解不等式.世界真的好奇妙!感觉不等式漏下几个会导致WA!! #include<cstdio> #include<cstring> #incl ...
随机推荐
- 网站搭建 - IIS 填坑 - 终于建好站了 linux + Windows
之前的IIS可以运行Windows的网页,但是对于php的网页,还是不能够支持,于是决定重新来一遍. (把踩的坑重新描述一下,在下载完php之后,解压后不要急着改文件,跳到最后的页面去改.) 以便能够 ...
- 配置SElinux环境,将SELinux设置为enforcing
SELinux是 美国国家安全局 (NSA) 对于 强制访问控制的实现 =>可以使root受限的权限 关闭SELinux=>修改配置文件,永久生效; sed -i 's/SELINUX=e ...
- nyoj 8-一种排序 (贪心)
8-一种排序 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:9 submit:18 题目描述: 现在有很多长方形,每一个长方形都有一个编号,这个编号 ...
- hdu 3342 Legal or Not (topsort)
Legal or NotTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Java,你告诉我 fail-fast 是什么鬼?
本篇我们来聊聊 Java 的 fail-fast 机制,文字一如既往的有趣哦. 01.前言 说起来真特么惭愧:十年 IT 老兵,Java 菜鸟一枚.今天我才了解到 Java 还有 fail-fast ...
- opencv MatchTemplate()模板匹配寻找最匹配部分
通常,随着从简单的测量(平方差)到更复杂的测量(相关系数),可以获得越来越准确的匹配,然而,这同时也会以越来越大的计算量为代价.比较科学的方法是对所有这些方法多次测试实验,以便为自己的应用选择同时兼顾 ...
- 安装eclipse血泪史
从大一到大三,屡次卸掉eclipse又屡次安装上,每次都要卡壳,所以这里开帖贴出自己的血泪史,以帮助大家 首先找一篇安装教程,网上有很多,这里不再赘述.举例 https://blog.csdn.net ...
- 如何打造一款m3u8视频爬虫
0.前言 m3u8是一种很常见的网页视频播放器的视频源,比如说中国大学MOOC中课程就是使用了该种视频格式. 随便打开一门课程,就可以发现在网络请求中存在一个m3u8的文件,在preview中预览,它 ...
- JAVA语 言 的 特 点
Java到 底 是 一 种 什 么 样 的 语 言 呢? Java是 一 种 简 单 的 面 象 对 象 的 分 布 式 的 解 释 的 健 壮 的 安 全 的 结 构 中 立 的 可 移 植 的 性 ...
- 经典sql面试题(学生表_课程表_成绩表_教师表)
转载:https://www.cnblogs.com/qixuejia/p/3637735.html 表架构 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cna ...