loj # 6079. "2017 Shandong round training Day7" maximum cost maximum flow cats []

First assume that the whole bed, then consideration of the balance with cost flow stream requires the establishment of a network
to 1 ~ n points regarded i-k + 1 ~ k and this section of the connection (i, i + k, 1 , e [i] -s [i]), i represents the change meal, have an impact on i i + k-1 of this section point ~; then connect (i, i + 1, k -ms-me, 0), which is in addition to the limits can pick the (i + k, i + 1 > n even to t)
is then established ss, even to the point 1 ~ k (ss, i, inf, 0 ), then even (s, ss, k -ms, 0) current limit
and maximum cost maximum flow can run

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int N=2005;
long long n,k,ma,mb,a[N],b[N],s,t,ss,h[N],cnt=1,dis[N],ans,p[N],fr[N];
bool v[N];
struct qwe
{
    long long ne,no,to,va,c;
}e[N*50];
long long read()
{
    long long r=0,f=1;
    char p=getchar();
    while(p>'9'||p<'0')
    {
        if(p=='-')
            f=-1;
        p=getchar();
    }
    while(p>='0'&&p<='9')
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
void add(long long u,long long v,long long w,long long c)
{
    cnt++;
    e[cnt].ne=h[u];
    e[cnt].no=u;
    e[cnt].to=v;
    e[cnt].va=w;
    e[cnt].c=c;
    h[u]=cnt;
}
void ins(long long u,long long v,long long w,long long c)
{
    add(u,v,w,c);
    add(v,u,0,-c);
}
bool spfa()
{
    for(long long i=s;i<=t;i++)
        dis[i]=-1e9;
    queue<long long>q;
    q.push(s);
    v[s]=1;
    dis[s]=0;
    while(!q.empty())
    {
        long long u=q.front();
        q.pop();
        v[u]=0;
        for(long long i=h[u];i;i=e[i].ne)
            if(e[i].va>0&&dis[e[i].to]<dis[u]+e[i].c)
            {
                dis[e[i].to]=dis[u]+e[i].c;
                fr[e[i].to]=i;
                if(!v[e[i].to])
                {
                    v[e[i].to]=1;
                    q.push(e[i].to);
                }
            }
    }
    return dis[t]!=-1e9;
}
void mcf()
{
    long long x=1e9;
    for(long long i=fr[t];i;i=fr[e[i].no])
        x=min(x,e[i].va);
    for(long long i=fr[t];i;i=fr[e[i].no])
    {
        e[i].va-=x;
        e[i^1].va+=x;
        ans+=e[i].c*x;
    }
}
int main()
{
    n=read(),k=read(),ma=read(),mb=read();
    s=0,ss=n+1,t=n+2;
    for(int i=1;i<=n;i++)
        a[i]=read();
    for(int i=1;i<=n;i++)
        b[i]=read();
    for(long long i=1;i<=n;i++)
    {
        ans+=a[i];
        if(i+1<=n)
            ins(i,i+1,k-ma-mb,0);
        else
            ins(i,t,k-ma-mb,0);
        if(i+k<=n)
            ins(i,i+k,1,b[i]-a[i]);
        else
            ins(i,t,1,b[i]-a[i]);
        p[i]=cnt;
        if(i<=k)
            ins(ss,i,1e9,0);
    }
    ins(s,ss,k-ma,0);
    while(spfa())
        mcf();
    printf("%lld\n",ans);
    for(long long i=1;i<=n;i++)
        printf(e[p[i]].va?"E":"S");
    return 0;
}

Guess you like

Origin www.cnblogs.com/lokiii/p/10991314.html