zoukankan      html  css  js  c++  java
  • ZOJ2770-Burn The Linked Camp(火烧连营Orz 差分约束-线性约束+最长路(OR反向最短路))

    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

    题解:只是一道差分约束的线性约束问题。首先用dis[i]代表从0到i的人数,则0=<dis[i]-dis[i-1]<=a[i],又有 U,V,W得到

    dis[V]-dis[U-1]>=W , dis[V]-dis[U-1]<=sum[V]-sum[U-1] ;然后写SPFA就可以啦。

    参考代码为(反向最短路):

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    #include<cmath> 
    #include<queue>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const int maxm = 2e5+5;
    const int maxn = 1005;
    int n,m;
    struct edge
    {
        int v;
        int c;
        edge(){}
        edge(int v,int c):v(v),c(c){}
    };
    vector<edge>e[maxn];
    int a[maxn],sum[maxn],dis[maxn],cnt[maxn];
    void addedge(int u,int v,int c)
    {
        e[u].push_back(edge(v,c));
    }
    
    void spfa()
    {
        bool vis[maxn];memset(vis,0,sizeof vis);
        memset(dis,INF,sizeof dis);
        dis[n]=0;vis[n]=1;
        queue<int>q; q.push(n);
        while(!q.empty())
        {
            int pre=q.front();q.pop();
            vis[pre]=0;
            for(int i=0;i<e[pre].size();i++)
            {
                if(dis[e[pre][i].v]>dis[pre]+e[pre][i].c)
                {
                    dis[e[pre][i].v]=dis[pre]+e[pre][i].c;
                    if(!vis[e[pre][i].v])
                    {
                        vis[e[pre][i].v]=1;
                        q.push(e[pre][i].v);
                        if(++cnt[e[pre][i].v]>sqrt(n))
                        {
                            printf("Bad Estimations
    ");
                            return ;
                        }
                    }
                }
            }
        }
        printf("%d
    ",-dis[0]);
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            memset(sum,0,sizeof sum);
            memset(cnt,0,sizeof cnt);
            memset(e,0,sizeof e);
            int u,v,c;
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
                sum[i]=sum[i-1]+a[i];
            }
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%d",&u,&v,&c);
                addedge(v,u-1,-c);
                addedge(u-1,v,sum[v]-sum[u-1]);
            }
            for(int i=1;i<=n;i++)
            {
                addedge(i,i-1,0);
                addedge(i-1,i,a[i]);
            }
            spfa();
        }
        return 0;
    }
     
     
  • 相关阅读:
    Debian如何永久添加静态路由
    一句话换个说法的软件
    高级程序员简历,技术总监喜欢什么简历?
    CP936实际上是GBK,编码问题再次让我熬夜
    句子说法转换的软件,基于AI技术
    输入关键词自动生成文章(2020年人工智能写作)
    自动写文章的智能软件(基于AI写作)
    python调用接口,python接收post请求接口(附完整代码)
    人工智能是铁饭碗还是铁坑,看看人工智能博士怎么说
    远离外包公司,就是远离码农的血汗工厂
  • 原文地址:https://www.cnblogs.com/csushl/p/9386776.html
Copyright © 2011-2022 走看看