您的位置:首页 > 其它

ZOJ2770 Burn the Linked Camp(差分约束)

2014-08-10 13:45 363 查看
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


Author: ZHOU, Yuan

Source: ZOJ Monthly, October 2006

差分约束,dis[i]-dis[i-1]>=-s,dis[b]-dis[a-1]>=c,dis[i]-dis[i-1]>=0,就这三个方程,找最长路,

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN=1005;
const int MAXM=1000005;
int first[MAXN],dis[MAXN],inq[MAXN],done[MAXN];
int u[MAXM],v[MAXM],w[MAXM],nex[MAXM];
int cnt,n;
int spfa(int x)
{
    queue<int>q;
    for(int i=1;i<=n;i++)dis[i]=-INF;
    memset(done,0,sizeof done);
    memset(inq,0,sizeof inq);
    q.push(x);
    dis[x]=0;
    inq[x]=1;
    done[x]=1;
    while(!q.empty())
    {
        int e,x=q.front();q.pop();
        inq[x]=0;
        for(e=first[x];e!=-1;e=nex[e])
            if(dis[x]+w[e]>dis[v[e]])
            {
                dis[v[e]]=dis[x]+w[e];
                if(!inq[v[e]])
                {
                    q.push(v[e]);
                    inq[v[e]]=1;
                    done[v[e]]++;
                    if(done[v[e]]>n)return 0;
                }
            }
    }
    return 1;
}
void add_(int a,int b,int c)
{
    u[cnt]=a;
    v[cnt]=b;
    w[cnt]=c;
    nex[cnt]=first[a];
    first[a]=cnt++;
}
int main()
{
    int i,j,m,c,cas=1,a,b;
    while(~scanf("%d%d",&n,&m))
    {
        cnt=0;
        memset(first,-1,sizeof first);
        memset(nex,-1,sizeof nex);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a);
            add_(i-1,i,0);
            add_(i,i-1,-a);
        }
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add_(a-1,b,c);
        }
        if(!spfa(0))printf("Bad Estimations\n");
        else printf("%d\n",dis
);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: