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 ...
随机推荐
- 手把手教你实现热更新功能,带你了解 Arthas 热更新背后的原理
文章来源:https://studyidea.cn/java-hotswap 一.前言 一天下午正在摸鱼的时候,测试小姐姐走了过来求助,说是需要改动测试环境 mock 应用.但是这个应用一时半会又找不 ...
- 深入理解计算机系统 第八章 异常控制流 Part2 第二遍
第二遍读这本书,每周花两到三小时时间,能读多少读多少(这次看了第 508~530 页,共 23 页) 第一遍对应笔记链接 https://www.cnblogs.com/stone94/p/10206 ...
- tomcat 部署springboot 项目
Springboot项目默认jar包,且内置Tomcat.现需要将项目打成war包,并部署到服务器tomcat中. 1.修改pom.xml文件.将jar修改为war. <packaging> ...
- [笔记]IDEA使用笔记
1.IDEA的目录结构 2.所有的源文件都必须写在src文件夹下, 3.输入psvm再按回车,就会生成主函数: 4.输入sout就会生成输出语句的格式: 5.ALT+4 调出上次运行的结果出来看看 ...
- hdu 2516 取石子游戏 (Fibonacci博弈)
取石子游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- win10添加启动项目
Win10启动文件夹一般位于C:\ProgramData\Microsoft\Windows\Start Menu(开始菜单)\Programs(程序)\StartUp(启动)目录,我们主要讲希望添加 ...
- opencv 1 HighGUI图形用户界面初步
1图像载入 显示和输出到文件 Opencv的命名空间 Mat类 图像的载入:imread()函数 图片的显示:imshow()函数 创建窗口:namedWindow()函数 输出图像到文件:imwri ...
- react-router-dom路由
- K8s & Openshift案例学习
1. openshift排错技巧:https://mp.weixin.qq.com/s?__biz=MzAwMDc2NjQ4Nw==&mid=2663494178&idx=1& ...
- 使用Docker搭建maven私服 及常规使用方法
安装-登录-配置 下载镜像 docker pull sonatype/nexus3 运行 docker run -d -p 9998:8081 --name nexus --restart=alway ...