方格取数问题

在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数。现要从方格中取数,使任意 2 个数所在方格没有公共边,且取出的数的总和最大。试设计一个满足要求的取数算法。对于给定的方格棋盘,按照取数要求编程找出总和最大的数。

输入格式:
第 1 行有 2 个正整数 m 和 n,分别表示棋盘的行数和列数。接下来的 m 行,每行有 n 个正整数,表示棋盘方格中的数。
输出格式:
程序运行结束时,将取数的最大总和输出

这道题没有什么头绪,看了题解以后得知:
二分图最大点权独立集与最小点权覆盖集是互逆的。这道题的意思是相邻两个点只能选一个,而这个方格图里显然没有奇环可以转换成二分图,因此我们可以染色转化成二分图。因为一条边上只能选一个点,我们把每相邻的两个连上一条容量为INF的边,原来的点权变成相应的容量流入S和T。
跑一遍最大流。
最大流=最小割=最小点权覆盖集。
总点权-最小点权覆盖集=最大点权独立集。

#include<bits/stdc++.h>
using namespace std;

const int INF=1e9;
const int MAXN=2e5+5;

typedef pair<int,int>par;
#define mp make_pair

struct edge{
    int to,next,w;
}e[MAXN<<5];

int fx[]={0,0,1,-1},fy[]={1,-1,0,0};
int head[MAXN],cur[MAXN],cnt=1;
inline void add(int u,int v,int w){e[++cnt]=(edge){v,head[u],w},head[u]=cnt;}

int n,m,s,t;
queue<int>q;
int dep[MAXN];

bool bfs(int x){
    memset(dep,0,sizeof(dep));
    dep[x]=1;q.push(x);
    while(q.size()){
        int u=q.front();q.pop();
        for(int i=head[u];i;i=e[i].next){
            int v=e[i].to,w=e[i].w;
            if(!dep[v]&&w){
                dep[v]=dep[u]+1;q.push(v);
            }
        }
    }
    if(!dep[t])return 0;
    return 1;
}

int dfs(int u,int flow){
    if(flow==0||u==t)return flow;
    for(int &i=cur[u];i;i=e[i].next){
        int v=e[i].to,w=e[i].w;
        if(w&&dep[v]==dep[u]+1){
            int tem=dfs(v,min(flow,w));
            if(tem){
                e[i].w-=tem;e[i^1].w+=tem;return tem;
            }
        }   
    }
    return 0;
}

int dinic(){
    int ans=0;
    while(bfs(s)){
        for(int i=s;i<=t;i++)cur[i]=head[i];
        while(int d=dfs(s,INF))ans+=d;
    }
    return ans;
}

int gezi[105][105],color[105][105];
vector<int>hei,bai;
queue<par>pq;
void getcolor(par x){
    pq.push(x);
    color[x.first][x.second]=1;
    while(pq.size()){
        par tem=pq.front();pq.pop();
        if(color[tem.first][tem.second]==1)
        add(s,(tem.first-1)*n+tem.second,gezi[tem.first][tem.second]),
        add((tem.first-1)*n+tem.second,s,0),
        hei.push_back((tem.first-1)*n+tem.second);
    //  cout<<(tem.first-1)*n+tem.second<<endl;

        if(color[tem.first][tem.second]==2)
        add((tem.first-1)*n+tem.second,t,gezi[tem.first][tem.second]),
        add(t,(tem.first-1)*n+tem.second,0),
        bai.push_back((tem.first-1)*n+tem.second);
    //  cout<<(tem.first-1)*n+tem.second<<"ok"<<endl;

        for(int i=0;i<4;i++){
            int temx=tem.first+fx[i],temy=tem.second+fy[i];
            if(temx<1||temx>m||temy<1||temy>n)continue;
            if(color[tem.first][tem.second]==1){
                if(!color[temx][temy]){
                    color[temx][temy]=2;pq.push(mp(temx,temy));
                }
                add((tem.first-1)*n+tem.second,(temx-1)*n+temy,INF);
                add((temx-1)*n+temy,(tem.first-1)*n+tem.second,0);
            }
            if(!color[temx][temy]&&color[tem.first][tem.second]==2){
                if(!color[temx][temy]){
                    color[temx][temy]=1;pq.push(mp(temx,temy));
                }
            }
        }
    }
} 

int main(){
    int ans=0;
    scanf("%d%d",&m,&n);
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            scanf("%d",&gezi[i][j]);
            ans+=gezi[i][j];
        }
    }
    s=0,t=m*n+1;
    getcolor(mp(1,1));
    printf("%d\n",ans-dinic()); 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值