1 条题解

  • 0
    @ 2025-10-8 17:03:29
    #include<bits/stdc++.h>
    using namespace std;
    const int N=11100,M=1e6;
    const int dx[4]={0,1,0,-1};
    const int dy[4]={1,0,-1,0};
    struct edge{int x,y,f,pre;}a[M];int alen,last[N],cur[N];
    void ins(int x,int y,int f)
    {
        a[++alen]={x,y,f,last[x]};last[x]=alen;
        a[++alen]={y,x,0,last[y]};last[y]=alen;
    }
    int n,m,st,ed,h[N];
    bool bfs()
    {
        queue<int> q;q.push(st);
        memset(h,0,sizeof(h));h[st]=1;
        while(!q.empty())
        {
            int x=q.front();q.pop();
            for(int k=last[x];k;k=a[k].pre)if(a[k].f)
            {
                int y=a[k].y;
                if(!h[y])
                {
                    h[y]=h[x]+1;
                    q.push(y);
                }
            }    
        }
        return h[ed]>0;
    }
    int ans;
    int dinic(int x,int f)
    {
        if(x==ed) return f;
        int sx=0;
        for(int k=cur[x];k;k=a[k].pre)if(a[k].f)
        {
            cur[x]=k;
            int y=a[k].y;
            if(h[y]==h[x]+1)
            {
                int sy=dinic(y,min(f-sx,a[k].f));
                a[k].f-=sy,a[k^1].f+=sy;
                sx+=sy;if(sx==f) return f;
            }
        }
        if(sx==0)h[x]=0;
        return sx;
    }
    int id(int i,int j){return (i-1)*m+j;}
    int main()
    {
        scanf("%d%d",&n,&m);
        alen=1;memset(last,0,sizeof(last));
        st=n*m+1,ed=st+1;
        int sum=0;
        for(int i=1,c;i<=n;i++)for(int j=1;j<=m;j++)
        {
            scanf("%d",&c);sum+=c;
            if((i+j)&1)ins(st,id(i,j),c);
            else ins(id(i,j),ed,c);
             
            if((i+j)&1)
            {
                for(int t=0;t<4;t++)
                {
                    int xx=i+dx[t],yy=j+dy[t];
                    if(1<=xx&&xx<=n && 1<=yy && yy<=m)ins( id(i,j), id(xx,yy), 1e9);
                }
            }
        }
         
        ans=0;
        while(bfs())
        {
            memcpy(cur,last,sizeof(cur));
            ans+=dinic(st,1<<30);
        }
        printf("%d\n",sum-ans);
        return 0;
    }
    • 1

    信息

    ID
    2977
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    2
    已通过
    2
    上传者