Burn the Linked Camp ZOJ - 2770(差分约束系统)

Burn the Linked Camp

ZOJ - 2770

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。   i -> i-1

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

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

    SPFA过程中判断是否有环。

code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 1100;
const int INF = 0x3f3f3f3f;
struct node{
    int u,v,w,Next;
};
node edge[maxn*10];
int dis[maxn],used[maxn],head[maxn],cnt;
int 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);//约束1 s[i-1]-s[i] >= -num; i -> i-1
        add(i-1,i,0);//约束2 s[i] - s[i-1] >= num;  i-1 -> i
        add(0,i,0);//约束3 s[i] - s[0] >= 0; 0 -> i
    }
    while(M--){
        scanf("%d%d%d",&a,&b,&num);
        add(a-1,b,num);//约束2 s[b] - s[a-1] >= num; a-1 -> b
    }
}
void SPFA(){
    for(int i = 0; i <= N; i++){
        dis[i] = -INF;
        vis[i] = 0;//判断是否在队列内
        used[i] = 0;//用于判断环
    }
    vis[0] = 1;
    used[0] = 1;
    dis[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(dis[u] + w > dis[v]){
                dis[v] = dis[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",dis[N]);
}
int main(){
    while(scanf("%d%d",&N,&M) != EOF){
        init();
        getmap();
        SPFA();
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值