Zoj 2770 Burn the Linked Camp 差分约束

Description

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个军营,每个军营最多能装一定上限的人数,给出一些区间,这个区间至少有k个人,求出总人数的最小值。如果不成立则输出“Bad Estimations”。

我设置的cap[i]为每个军营的容量,total[i]为前i个军营的总人数;

首先每个军营的人数限制 0<=每个军营实际人数total[i]-total[i-1] <=cap[i];
区间[a,b]人数限制 k<=[a,b]<=total[b]-total[a];
可以用差分约束来做,差分约束必须的条件是所有关系式拥有相同的约束关系,就是所有不等式必须是>=或者<=
所以转换不等式 total[i]-total[i-1] <= cap[i];
total[i-1]-total[i] <= 0;
[b]-[a-1] <= total[b]-total[a-1];
[a-1]-[b] <= 0;
这样就全部转换为相同的符号。接着利用差分约束的规则进行建图,即建一个被减的数指向减数的一个有向图。接着跑模版spfa,有答案就输出最少最少人数dis[n]-dis[0],因为dis[n]==0,所以直接输出-dis[0]就行。如果没有负环,就有最小值,否则输出“Bad Estimations”。

#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"string.h"
#include"queue"
#define maxn 1010
#define inf 77777777
using namespace std;

struct node    ///邻接表的边
{
    int to;
    int w;
    int next;
}edge[30010];
int n,m,top;
int dis[maxn];    ///用来跑spfa
int cap[maxn];    ///每个军营最大容量
int head[maxn];    ///头结点
int flag[maxn];    ///spfa判断节点进队是否超过n次
int total[maxn];    ///前i个军营总人数
bool vis[maxn];    ///spfa标记

void init()
{
    top = 0;
    for(int i = 0;i < maxn;i++)   ///初始化
    {
        head[i] = -1;
        vis[i] = false;
        dis[i] = inf;
        flag[i] = 0;
        total[i] = 0;
    }
}

void add(int u,int v,int w)   ///加边,邻接表
{
    edge[top].w = w;
    edge[top].to = v;
    edge[top].next = head[u];
    head[u] = top++;
}

bool spfa()    ///模版spfa
{
    dis[n] = 0;
    vis[n] = true;
    queue<int>q;
    q.push(n);
    flag[n]++;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = head[u];i != -1;i = edge[i].next)
        {
            node e = edge[i];
            if(dis[e.to] > dis[u]+e.w)
            {
                dis[e.to] = dis[u]+e.w;
                if(!vis[e.to])
                {
                    flag[e.to]++;
                    vis[e.to] = true;
                    if(flag[u] > n)
                        return false;
                    q.push(e.to);
                }
            }
        }
    }
    return true;
}
int main(void)
{
    while(scanf("%d%d",&n,&m) !=EOF)
    {
        init();
        cap[0] = 0;
        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&cap[i]);
            total[i]+=cap[i]+total[i-1];   ///前缀和
            add(i-1,i,cap[i]);      ///i-1到i有一条权值cap[i]的边 total[i]-total[i-1]<=cap[i]
            add(i,i-1,0);       ///i到i-1有一条权值为0的边 total[i-1]-total[i]<=0
        }
        for(int i = 1;i <= m;i++)
        {
            int a,b,k;
            scanf("%d%d%d",&a,&b,&k);
            add(a-1,b,total[b]-total[a-1]);    ///a-1到b有条权值为total[b]-total[a]的边,人数必须小于最大值
            add(b,a-1,-k);     ///b到a-1有一条权值为-k的边,即人数必须大于k
        }
        if(spfa())
        {
            /*for(int i = 0;i <= n;i++) printf("%d ",dis[i]);*/
            printf("%d\n",-dis[0]);
        }
        else
            printf("Bad Estimations\n");

    }
    return 0;
}

你可能感兴趣的:(ZOJ,差分约束)