1 条题解

  • 0
    @ 2026-2-7 18:47:53
    #include<cstdio>
    #include<cstring>
    using  namespace  std;
    typedef  long  long  ll;
    struct  node
    {
        ll  y,next,other,c,d;
    }a[210000];ll  last[5100],len,n,m,x1,x2,y1,y2,flow[5100],dis[5100],d[5100],b[5100],st,ed,cost,ans;
    bool  v[5100];
    ll  list[5100],head,tail;
    inline  ll  mymin(ll  x,ll  y){return  x<y?x:y;}
    void  ins(ll  x,ll  y,ll  c,ll  d)
    {
        len++;
        a[len].y=y;a[len].c=c;a[len].d=d;a[len].next=last[x];last[x]=len;
        len++;
        a[len].y=x;a[len].c=0;a[len].d=-d;a[len].next=last[y];last[y]=len;
        a[len].other=len-1;
        a[len-1].other=len;
    }
    bool  spfa()
    {
        memset(v,true,sizeof(v));v[st]=false;
        memset(dis,20,sizeof(dis));dis[st]=0;
        head=1;tail=2;list[head]=st;
        ll  inf=dis[st+1];
        while(head!=tail)
        {
            ll  x=list[head];
            for(ll  k=last[x];k;k=a[k].next)
            {
                ll  y=a[k].y;
                if(a[k].c>0  &&  dis[x]+a[k].d<dis[y])
                {
                    dis[y]=dis[x]+a[k].d;
                    flow[y]=mymin(flow[x],a[k].c);
                    d[y]=x;b[y]=k;
                    if(v[y]==true)
                    {
                        v[y]=false;
                        if(dis[list[head+1]]>dis[y])
                        {
                            ll  all=head;
                            head--;if(head==0)head=n;
                            list[head]=list[all];list[all]=y;
                        }
                        else
                        {
                            list[tail++]=y;if(tail==n+1)tail=1;
                        }
                    }
                }
            }
            head++;v[x]=true;
            if(head==n+1)head=1;
        }
        if(dis[ed]!=inf)
        {
            ll  y=ed,root=0;
            while(y>0)
            {
                root=b[y];y=d[y];
                a[root].c-=flow[ed];a[a[root].other].c+=flow[ed];
            }
            ans+=flow[ed];cost+=flow[ed]*dis[ed];
            return  true;
        }
        return  false;
    }
    int  main()
    {
        scanf("%lld",&n);scanf("%lld%lld%lld%lld%lld",&m,&x1,&y1,&x2,&y2);
        st=0;ed=n*2+1;
        for(ll  i=1;i<=n;i++)
        {
            ll  x;scanf("%lld",&x);
            ins(st,i*2,x,0);ins(i*2-1,ed,x,0);
        }
        for(ll  i=1;i<n;i++)
        {
            ins(i*2,i*2+2,999999999,0);
            ins(st,i*2-1,999999999,m);
        }
        ins(st,n*2-1,99999999,m);
        ll  edd=n-x1;
        for(ll  i=1;i<=edd;i++)ins(i*2,(i+x1)*2-1,999999999,y1);
        edd=n-x2;
        for(ll  i=1;i<=edd;i++)ins(i*2,(i+x2)*2-1,999999999,y2);
        flow[st]=999999999;n=n*2+2;
        while(spfa()==true);
        printf("%lld\n",cost);
        return  0;
    }
    
    • 1

    信息

    ID
    963
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    17
    已通过
    5
    上传者