CodeForces E. Goods transportation [maximum flow + dp minimum cut]

First of all, it
is very simple to violently build a map and run the maximum flow. The flow of s to each i is connected to p[i], each i to t is connected to the edge of s[i], and the flow of each i to j is The edge of c (i<j), but T and M will
consider the maximum flow = minimum cut
and then dp to find the minimum cut, let f[i][j] be the cut to the i-th point, there are j connected to s ( Because a point in the minimum cut is either connected to s or connected to t), the transition is
\[ f[i][j]=min(f[i-1][j]+1*j*c+p[i],f [i-1][j-1]+s[i]) \]

#include<iostream>
#include<cstdio>
using namespace std;
const int N=10005;
int n,c,p[N],s[N];
long long f[2][N],ans=1e18;
int read()
{
    int 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;
}
int main()
{
    n=read(),c=read();
    for(int i=1;i<=n;i++)
        p[i]=read();
    for(int i=1;i<=n;i++)
        s[i]=read();
    for(int i=1;i<=n;i++)
    {
        int a=i&1,b=a^1;
        f[a][0]=f[b][0]+p[i];
        for(int j=1;j<i;j++)
            f[a][j]=min(f[b][j]+1ll*j*c+p[i],f[b][j-1]+s[i]);
        f[a][i]=f[b][i-1]+s[i];
    }
    for(int i=0;i<=n;i++)
        ans=min(ans,f[n&1][i]);
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324827326&siteId=291194637