ZOJ 2770 Burn the Linked Camp( 差分约束啊 )

来源:互联网 发布:mac上的ps软件多少钱 编辑:程序博客网 时间:2024/05/18 22:42

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770


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 21000 2000 10001 2 11002 3 13003 1100 200 3002 3 600

Sample Output:

1300Bad Estimations


Author: ZHOU, Yuan
Source: ZOJ Monthly, October 2006


题意:

给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵。

PS:

定义c[i]为第i个营的人数,定义dis[i] = c[0]+c[1]+...c[i],其中c[0]=0,则可知我们要求的为dis[n];

所以我们得到:0 <= dis[i] - dis[i-1] <= c[i];

将:0 <= dis[i] - dis[i-1] 转化为 dis[i-1] - dis[i] <= 0;

给出限制 u , v ,w;

得到:dis[v] - dis[u-1] >= w;

转化后得到:dis[u-1] - dis[v] <= -w;


所以我们加边:


Addedge(i-1,i,c[i]);


Addedge(i,i-1,0);


Addedge(v,u-1,-w);


因为求的是最小值,即:a[n] - a[1] >= minn;


得到:a[1] - a[n] <= -minn;


所以我们以n点为起点,得到的dis[0]的相反数就是最小值!


代码如下:

#include <cstdio>#include <cstring>#include <stack>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define maxn 50017int n, m, k;int Edgehead[maxn], dis[maxn];struct Edge{    int v,w,next;} Edge[3*maxn];bool vis[maxn];int cont[maxn];void Addedge(int u, int v, int w){    Edge[k].next = Edgehead[u];    Edge[k].w = w;    Edge[k].v = v;    Edgehead[u] = k++;}int SPFA( int start)//stack{    int sta[maxn];    memset(cont,0,sizeof(cont));    int top = 0;    for(int i = 0 ; i <= n ; i++ )        dis[i] = INF;    dis[start] = 0;    ++cont[start];    memset(vis,false,sizeof(vis));    sta[++top] = start;    vis[start] = true;    while(top)    {        int u = sta[top--];        vis[u] = false;        for(int i = Edgehead[u]; i != -1; i = Edge[i].next)//注意        {            int v = Edge[i].v;            int w = Edge[i].w;            if(dis[v] > dis[u] + w)            {                dis[v] = dis[u]+w;                if( !vis[v] )//防止出现环                {                    sta[++top] = v;                    vis[v] = true;                }                if(++cont[v] > n)//有负环                    return -1;            }        }    }    return dis[0];}int main(){    int u, v, w;    int c[maxn], s[maxn];    while(~scanf("%d%d",&n,&m))    {        k = 1;        memset(Edgehead,-1,sizeof(Edgehead));        memset(Edge,0,sizeof(Edge));        for(int i = 1; i <= n; i++)        {            scanf("%d",&c[i]);        }        for(int i = 1 ; i <= m ; i++)        {            scanf("%d%d%d",&u,&v,&w);            Addedge(v,u-1,-w);        }        for(int i = 1; i <= n; i++)        {            Addedge(i-1,i,c[i]);            Addedge(i,i-1,0);        }        int ans = SPFA(n);//从n到0        if(ans == -1)        {            printf("Bad Estimations\n");            continue;        }        printf("%d\n",-ans);    }    return 0;}



1 0