zoj 2770 ( 差分约束 spfa )

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 个点 表示 营寨 的个数  然后给你n 个数 表示每个营寨的最大容量。  然后给你m  个描述  u,v,  w  表示从u 营寨到v营寨 至少有w  个人 ,那么问你描述是否可靠  如果不可靠 就输出 Bad Estimations 否则输出这些营寨中的最少人数。

思路: 也算是一个比较好的差分约束的题目:

当然差分约束 的最重要的就是列出不等式

1 首先 第i 个点的容量为 w 那么 就可以列出 sum[i]-sum[i-1]<=w 这一个式子 表示第i 个营寨的人数必须小于等于 w

2: 其次 第i 个营寨的人数肯定>=0 那么就可以列出 sum[i]-sum[i-1]>=0 这一个式子

3: 其次 m种 描述中 描述了 从 u 到v 人数为 w 那么就可以列出 sum[v]-sum[u-1] >=w

 那么我们就可以从v 到 u-1 的一条长为 w 的路径

差分约束 简单的来说 就是 给你一些 形如 x[a]-x[b]<= w 的公式 那么你就要从 b 到 a 建一条权为

w的路径 然后最后求 最短路径

不然也可以 转化成 x[a]-x[b]>= w 那么就可以建一条从 b 到 a 权为 w 的路径 然后求最长路。

看是否有解。。

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<algorithm>
#define N 1005
#define M 10005

using namespace std;
typedef long long ll;

const ll inf =1e18+5;

ll dis[N];
int vis[N];
int head[N];
int n,m;
int cnt[N];

int len;

struct node
{
    int v;
    int next;
    ll c;
}edge[M*2];

void add(int u,int v,ll w)
{
    edge[++len].v=v;
    edge[len].c=w;
    edge[len].next=head[u];
    head[u]=len;
    return ;
}

ll spfa()
{
    queue<int >q;
    for(int i=0;i<=n;i++) dis[i]=inf;
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));

    dis[n]=0;
    vis[n]=1;  cnt[n]=1;
    q.push(n);

    int u,v;
    ll w;
    while(!q.empty()){
        u=q.front();  q.pop();
        vis[u]=0;

        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].v;  w=edge[i].c;
            if(dis[v]>w+dis[u])
            {
                dis[v]=w+dis[u];
                if(vis[v]==0)
                {
                    cnt[v]++;
                    if(cnt[v]>=n+1) return -1;
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }

    return dis[0];
}

int main()
{
    ll c;
    int u,v;
    while(scanf("%d %d",&n,&m)!=EOF)
    {

    len=0;
    memset(head,-1,sizeof(head));

    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&c); add(i-1,i,c);  add(i,i-1,0);
    }

    for(int i=1;i<=m;i++)
    {
        scanf("%d %d %lld",&u,&v,&c);
        add(v,u-1,-c);
    }

    ll ans=spfa();
    if(ans==-1) printf("Bad Estimations\n");
    else printf("%lld\n",-ans);

    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值