ZOJ 2770 差分约束+SPFA
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
题意:给出n个点表示n个军营,c[i]表示第i个军营可容纳的士兵的最大值,接着给出m条边(i,j,k)表示从第i到第j个军营最少有的的士兵数。求在满足以上条件下最少有多少士兵!
我们不妨设S(i)表示从第一个兵营到第i个兵营最少的士兵数,保存在d[i]中
接着就是找出所有的不等式组。
1.(i,j,k) --> S(j)-S(i-1)>=k 即S(i-1)-S(j)<=-k
2.S(j)-S(i-1)<=c=d[j]-d[i-1];
3.设A(i)表示每个军营的实际人数,显然 0<=A(i)<=c[i]
即 S(i)-S(i-1)>=0&&S(i)-S(i-1)<=c[i];
接着将不等式转化为边存入图中
我们令 S(u)<=S(v)+w 表示连接一条从v到u且权值为w的有向边.
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
inline int read()
{
int s=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {s=s*10+ch-'0';ch=getchar();}
return s*f;
}
struct Edge
{
int to;
int w;
int next;
}edges[50005];
int cnt,dis[10005];
int first[10005];
int n,m;
bool vis[10005];
int counts[10005];
int c[10005];
void add(int a,int b,int c)
{
//cout<<a<<" "<<b<<" "<<c<<endl;
edges[cnt].to=b;
edges[cnt].w=c;
edges[cnt].next=first[a];
first[a]=cnt++;
}
bool spfa(int st,int ed)
{
queue<int>Q;
dis[st]=0;
vis[st]=1;
counts[st]++;
Q.push(st);
while(!Q.empty()){
int u=Q.front();Q.pop();
vis[u]=0;
if(counts[u]>n) return false;
for(int i=first[u];i+1;i=edges[i].next){
int v=edges[i].to;
if(dis[v]>dis[u]+edges[i].w){
dis[v]=dis[u]+edges[i].w;
if(!vis[v]) {Q.push(v);vis[v]=1;counts[v]++;}
}
}
}
//for(int i=1;i<=n;++i) cout<<dis[i]<<" ";cout<<endl;
cout<<-dis[0]<<endl;
return true;
}
int main()
{
int t,i,j;
//cin>>t;
while(cin>>n>>m){
memset(first,-1,sizeof(first));
memset(vis,0,sizeof(vis));
memset(dis,inf,sizeof(dis));
memset(counts,0,sizeof(counts));
cnt=0;c[0]=0;
for(i=1;i<=n;++i) {c[i]=read();
add(i,i-1,0);
add(i-1,i,c[i]);
c[i]+=c[i-1];
}
int u,v,w;
for(i=1;i<=m;++i)
{
u=read(),v=read(),w=read();
add(v,u-1,-w);
add(u-1,v,c[v]-c[u-1]);
}
if(!spfa(n,0)) puts("Bad Estimations");
}
return 0;
}
ZOJ 2770 差分约束+SPFA的更多相关文章
- 【poj3169】【差分约束+spfa】
题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...
- O - Layout(差分约束 + spfa)
O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...
- poj3159 差分约束 spfa
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...
- 【BZOJ】2330: [SCOI2011]糖果(差分约束+spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束运用了最短路中的三角形不等式,即d[v]<=d[u]+w(u, v),当然,最长 ...
- (简单) POJ 3169 Layout,差分约束+SPFA。
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- poj Layout 差分约束+SPFA
题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...
- BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)
BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...
- POJ-3159.Candies.(差分约束 + Spfa)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 40407 Accepted: 11367 Descri ...
- 图论分支-差分约束-SPFA系统
据说差分约束有很多种,但是我学过的只有SPFA求差分: 我们知道,例如 A-B<=C,那么这就是一个差分约束. 比如说,著名的三角形差分约束,这个大家都是知道的,什么两边之差小于第三边啦,等等等 ...
随机推荐
- sql server 数据排名
城市排名列表 )) AS px, ) pm25, ) pm10, ) co, ) no2, ) so2,) o3_8,) indexs,) aqi FROM monitor_city_hour m,c ...
- JavaScript计算星期几
function zeller(dateStr) { var c = parseInt(dateStr.substr(0, 2)); var y = parseInt(dateStr.substr(2 ...
- linux 挂载硬盘 + 对硬盘 分区
parted命令可以划分单个分区大于2T的GPT格式的分区,也可以划分普通的MBR分区 fdisk命令对于大于2T的分区无法划分,所以用fdisk无法看到parted划分的GPT格式的分区 1. 用 ...
- http://bugs.mysql.com/bug.php?id=72123
今天某个环境发生了这个bug. http://bugs.mysql.com/bug.php?id=72123
- linux内核分析 第3章读书笔记
第三章 进程管理 一.进程 1.进程 进程就是处于执行期的程序. 进程就是正在执行的程序代码的实时结果. 进程是处于执行期的程序以及相关的资源的总称. 进程包括代码段和其他资源. 2.线程 执行线程, ...
- tf.equal的使用
tf.equal(A, B)是对比这两个矩阵或者向量的相等的元素,如果是相等的那就返回True,反正返回False,返回的值的矩阵维度和A是一样的 import tensorflow as tf im ...
- Spring Aop的理解和简单实现
1.AOP概念 所说的面向切面编程其实就是在处理一系列业务逻辑的时候这一系列动作看成一个动作集合.比如连接数据库来说: 加载驱动-----获取class--------获取连接对象-------访问数 ...
- BZOJ 1503 郁闷的出纳员(splay)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 题意:给出一个数列(初始为空),给出一个最小值Min,当数列中的数字小于Min时自动 ...
- JavaScript callee caller
caller是function的属性 callee是arguments的属性 callee:返回正在执行的函数对象. var sum = function (n) { == n) ; ); } con ...
- HDU 1285 确定比赛名次(拓扑排序)题解
Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...