真心给byvoid和gyh神犇跪了,神建模,我开始想的时DP,sb....

题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1061

题解:https://www.byvoid.com/blog/noi-2008-employee/

蒟蒻的代码:

#include<iostream>
#include<cstdio>
using namespace std;
const int INF=214748364;
struct eg
{
       int u,v,c,w,pr;
}x[500011]={};
int n,m,need[1011]={},bia[1011]={},num=0,s=0,t;
void add(int l,int r,int ci,int wi)
{
     //cout<<l<<" "<<r<<" "<<ci<<" "<<wi<<"**"<<endl;
     x[num].u=l;
     x[num].v=r;x[num].c=ci;
     x[num].w=wi;x[num].pr=bia[l];
     bia[l]=num;
     num++;
     x[num].u=r;
     x[num].v=l;x[num].c=0;
     x[num].w=-wi;x[num].pr=bia[r];
     bia[r]=num;
     num++;
}
int sp[500011]={},he=0,d[1011]={},pre[1011]={},preg[1011]={},a[1011];
bool spfa()
{
     int i,q,p,l,r;
     for (i=1;i<=t;i++) a[i]=d[i]=INF;
     d[s]=0;a[s]=INF;
     sp[1]=s;q=0;p=1;
     while (q<p)
     {
           l=sp[++q];
           for (i=bia[l];i!=-1;i=x[i].pr)
           {
               r=x[i].v;
               if (x[i].c>0&&d[l]+x[i].w<d[r])
               {
                  d[r]=d[l]+x[i].w;
                  pre[r]=l;preg[r]=i;
                  sp[++p]=r;
                  a[r]=min(a[l],x[i].c);
               }
           }
           //我sb一样的学最大流在这里加了一个跳出判断,sb了一个小时...
     }
     if (d[t]==INF) return false;
     he+=a[t]*d[t];
     for (i=t;i!=s;i=pre[i])
     {
         x[preg[i]].c-=a[t];
         x[preg[i]^1].c+=a[t];
     }
     return true;
}
int main()
{
    int i,j,a,b,c;
    scanf("%d%d",&n,&m);t=n+2;
    for (i=s;i<=t;i++) bia[i]=-1;
    for (i=1;i<=n;i++)
    {
        scanf("%d",&need[i]);
        add(i+1,i,INF,0);
    }
    for (i=1;i<=m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        add(a,b+1,INF,c);
    }
    need[n+1]=0;
    for (i=1;i<=n+1;i++)
    {
        a=need[i]-need[i-1];
        if (a>0) add(s,i,a,0);
        if (a<0) add(i,t,-a,0);
    }
     
  
    i=0;
    while (spfa()) i++;
     
    printf("%d",he);
    system("pause");
    return 0;
}

​
View Code