ZOJ 2770--Burn the Linked Camp 【差分约束】

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

差分约束,我们定义x[ i ]为第 i 个营的人数,定义dist[ i ] = x[ 0 ] + x[ 1 ] + ...x[ i ],其中x [ 0 ] = 0,则可知我们要求的为dist [ n ]。 

对于差分不等式,a - b <= c ,建一条 b 到 a 的权值为 c 的边,求的是最短路,得到的是最大值;

对于不等式 a - b >= c ,建一条 b 到 a 的权值为 c 的边,求的是最长路,得到的是最小值。这题由于求最小值,所以我们用的是第二种。 

对于题目,我们可以有如下约束 :

      x[ i ] 代表每个军营的人数。 s[ i ] 前 n 个军营的中人数。

    (1)第 i 个军营有 num 人 ,人数肯定不会超过 num ,则有约束条件 s[ i ] - s[ i - 1] <= sum , 变为 :s[ i - 1] > s[ i ] >=  - num。

    (2)第 i 个大营到第 j 个军营士兵总数至少有 num 个。则有约束条件  s[ j ] - s[ i - 1 ] >= num 。

    (3)设第 0 个 军营的人数为 0 ,军营的总人数肯定 >= 0 ,所以 s [ i ] - s[ 0 ] >=0  
    因为是求最小值,同时图的建立是按照小于等于进行,所以我们就要从负无穷约束到最小值,用SPFA求最长路即可。 

    SPFA过程中判断是否有环。 


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
#define maxn 1100
using namespace std;

struct node {
    int u, v, w, next;
};

node edge[maxn * 10];
int dist[maxn], used[maxn], head[maxn], cnt;
bool vis[maxn];
int N, M;

void init(){
    memset(head, -1, sizeof(head));
    cnt = 0;
}

void add(int u, int v, int w){
    edge[cnt] = {u, v, w, head[u]};
    head[u] = cnt++;
}

void getmap(){
    int a, b, num;
    for(int i = 1; i <= N; ++i){
        scanf("%d", &num);
        add(i, i - 1, -num);
        add(i - 1, i, 0);
        add(0, i, 0);
    }
    while(M--){
        scanf("%d%d%d", &a, &b, &num);
        add(a - 1, b, num);
    }
}

void SPFA(){
    for(int i = 0; i <= N; ++i){
        dist[i] = -INF;
        vis[i] = 0;
        used[i] = 0;
    }
    vis[0] = 1;
    used[0] = 1;
    dist[0] = 0;
    queue<int>q;
    q.push(0);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for(int i = head[u]; i != -1; i = edge[i].next){
            int v = edge[i].v;
            int w = edge[i].w;
            if(dist[v] < dist[u] + w){
                dist[v] = dist[u] + w;
                if(!vis[v]){
                    vis[v]  = 1;
                    used[v]++;
                    if(used[v] > N){
                        printf("Bad Estimations\n");
                        return ;
                    }
                    q.push(v);
                }
            }
        }
    }
    printf("%d\n", dist[N]);
}

int main(){
    while(scanf("%d%d", &N,&M)!=EOF){
        init();
        getmap();
        SPFA();
    }
    return 0;
}



你可能感兴趣的:(ZOJ 2770--Burn the Linked Camp 【差分约束】)