题目链接:戳我

拆点。第一种情况流量为1,第二种情况同一个点之间连inf,第三种情况除了第一行连1,其他都inf。

做完了。注意空间还是要开够,而且数值还有负数。

跑最大费用最大流即可。

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define MAXN 500010
using namespace std;

int n,m,S=0,T,cnt,c,f,t=1,tot;
int head[MAXN],val[30][41],pre_v[MAXN],pre_e[MAXN],dis[MAXN],done[MAXN],id[30][41];
struct Edge{int nxt,to,dis,cost;}edge[MAXN<<1];

inline void add(int from,int to,int dis,int cost)
{
    edge[++t].nxt=head[from],edge[t].to=to,edge[t].dis=dis,edge[t].cost=cost,head[from]=t;
    edge[++t].nxt=head[to],edge[t].to=from,edge[t].dis=0,edge[t].cost=-cost,head[to]=t;
}

inline bool spfa()
{
    queue<int>q; 
    memset(dis,0x3f,sizeof(dis));
    memset(done,0,sizeof(done));
    q.push(S);dis[S]=0;done[S]=1;
    while(!q.empty())
    {
        int u=q.front();q.pop();done[u]=0;
        for(int i=head[u];i;i=edge[i].nxt)
        {
            int v=edge[i].to;
            if(edge[i].dis&&dis[u]+edge[i].cost<dis[v])
            {
                dis[v]=dis[u]+edge[i].cost;
                pre_e[v]=i,pre_v[v]=u;
                if(!done[v])
                    q.push(v),done[v]=1;
            }
        }
    }
    if(dis[T]==0x3f3f3f3f) return false;
    int flow=0x3f3f3f3f;
    for(int i=T;i!=S;i=pre_v[i]) flow=min(flow,edge[pre_e[i]].dis);
    for(int i=T;i!=S;i=pre_v[i]) edge[pre_e[i]].dis-=flow,edge[pre_e[i]^1].dis+=flow;
    f+=flow;
    c+=flow*dis[T];
    return true;
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("ce.in","r",stdin);
    #endif
    scanf("%d%d",&m,&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i+m-1;j++)
        {
            scanf("%d",&val[i][j]);
            id[i][j]=++cnt;
        }
    }
    tot=cnt;
    T=cnt*2+1;
    //printf("S=%d T=%d tot=%d\n",S,T,tot);
    //subtask1
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i+m-1;j++)
        {
            add(id[i][j],id[i][j]+tot,1,0);
            if(i==1) add(S,id[i][j],1,-val[i][j]);
            if(i==n) add(id[i][j]+tot,T,1,0);
            if(i!=n) 
            {
                add(id[i][j]+tot,id[i+1][j],1,-val[i+1][j]);
                add(id[i][j]+tot,id[i+1][j+1],1,-val[i+1][j+1]);
            }
        }
    }
    while(spfa());
    printf("%d\n",-c);
    //subtask2
    c=f=0;t=1;
    memset(head,0,sizeof(head));
    memset(pre_e,0,sizeof(pre_e));
    memset(pre_v,0,sizeof(pre_v));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i+m-1;j++)
        {
            add(id[i][j],id[i][j]+tot,0x3f3f3f3f,0);
            if(i==1) add(S,id[i][j],1,-val[i][j]);
            if(i==n) add(id[i][j]+tot,T,0x3f3f3f3f,0);
            if(i!=n) 
            {
                add(id[i][j]+tot,id[i+1][j],1,-val[i+1][j]);
                add(id[i][j]+tot,id[i+1][j+1],1,-val[i+1][j+1]);
            }
        }
    }
    while(spfa());
    printf("%d\n",-c);
    //subtask3
    c=f=0;t=1;
    memset(head,0,sizeof(head));
    memset(pre_e,0,sizeof(pre_e));
    memset(pre_v,0,sizeof(pre_v));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i+m-1;j++)
        {
            add(id[i][j],id[i][j]+tot,0x3f3f3f3f,-val[i][j]);
            if(i==1) add(S,id[i][j],1,0);
            if(i==n) add(id[i][j]+tot,T,0x3f3f3f3f,0);
            if(i!=n) 
            {
                add(id[i][j]+tot,id[i+1][j],0x3f3f3f3f,0);
                add(id[i][j]+tot,id[i+1][j+1],0x3f3f3f3f,0);
            }
        }
    }
    while(spfa());
    printf("%d\n",-c);
    return 0;
}