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)<=-k2.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;
}

时间: 2024-10-10 03:06:16

ZOJ 2770 差分约束+SPFA的相关文章

ZOJ 2770 Burn the Linked Camp 差分约束+SPFA

第一道正儿八经的差分约束题 有排成一列的n个点,首先告诉你每个点的值最多是多少(最少显然要大于0),然后告诉你m段i,j,k,表示第i个点到第j个点的值的和至少有k,问你总和至少为多少. 要注意的是,告诉你的所有关系式都不要忘记建边,一开始漏了大于0的条件调半天o(╯□╰)o 不等式的形式是a-b<=c这样的= = 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <

POJ 3169 Layout (差分约束+SPFA)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6832   Accepted: 3292 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

【bzoj2330】: [SCOI2011]糖果 图论-差分约束-SPFA

[bzoj2330]: [SCOI2011]糖果 恩..就是裸的差分约束.. x=1 -> (A,B,0) (B,A,0) x=2 -> (A,B,1)  [这个情况加个A==B无解的要特判] x=3 -> (B,A,0)  [恩这个是不少于一开始zz建反了] x=4 -> (B,A,1) x=5 -> (A,B,0) 然后源点到所有点建1的边[恩据说有条链所以要反着连]跑最长路就好了 1 /* http://www.cnblogs.com/karl07/ */ 2 #inc

poj3159 差分约束 spfa

1 //Accepted 2692 KB 1282 ms 2 //差分约束 -->最短路 3 //TLE到死,加了输入挂,手写queue 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std;

poj3159——Candies(差分约束+SPFA堆栈)

Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse's class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and ofte

(简单) POJ 3169 Layout,差分约束+SPFA。

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe

poj3169——Layout(差分约束+SPFA判断负环)

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe

BZOJ 2330 [SCOI2011]糖果 差分约束spfa版

题意:自行百度,(之前做过一道candy的升级版). 方法:差分约束 解析:最近在学差分约束什么的,这道是做的第一个bz上的题,感觉还是较简单的.以下我对5种操作进行描述. case 转换不等式 转换不等式2 1 A>=0+B B>=0+A 2 B>=1+A 3 A>=0+B 4 A>=1+B 5 B>=0+A 如上表按照差分约束的原理加边,然后再观察上表不等式方向->为求大边,即最长路. 这些边是不够的,所有人应最少为1糖果,即创出个源点到各点距离为1. 后记:

POJ 3159 Candies(差分约束+spfa+链式前向星)

题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A的糖果数<=C . 最后求n 比 1 最多多多少颗糖果. 解题思路:经典差分约束的题目,具体证明看这里<数与图的完美结合——浅析差分约束系统>. 不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值,我们得到 dis[B]-dis[A]<=w(A,B).看到这里,我们可以联想到