HDU2119Matrix(最小点集覆盖)

题意: n*m的矩阵由0,1组成,现在每次操作可以选择一行或者一列,把这一行或者一列的1都变成0,问最少操作几次,可以把1去完。
思路:把矩阵按X,Y坐标分为二分图的A,B俩集合,1的点,可以由其的横坐标向纵坐标连边,最后就是求建立的图的最小点集覆盖(因为你选择一个横坐标的话,肯定是想能最多的把这个横坐标对应的所有的纵坐标都选择到,画个图就很清晰了),最小点集覆盖等于最大匹配

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<vector>
#include<limits>
#include<ctime>
#include<cassert>
#include<cstdlib>
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define cl(a,b) memset(a,b,sizeof(a));
#define LL long long
#define P pair<int,int>
#define X first
#define Y second
#define pb push_back
#define fread(a) freopen(a,"r",stdin);
#define fwrite(a) freopen(a,"w",stdout);
using namespace std;
const int maxn=105;
const int inf=999999;

vector<int> G[maxn];
int matching[maxn];
bool vis[maxn];
int num;
bool dfs(int u){
    int N=G[u].size();
    for(int i=0;i<N;i++){
        int v=G[u][i];
        if(vis[v])continue;
        vis[v]=true;
        if(matching[v]==-1||dfs(matching[v])){
            matching[v]=u;
            return true;
        }
    }
    return false;
}
int hungar(){
    int ans=0;
    cl(matching,-1);
    for(int i=0;i<num;i++){
        cl(vis,false);
        if(dfs(i))ans++;
    }
    return ans;
}


int main(){
    int n,m;
    while(~scanf("%d",&n)&&n){
        scanf("%d",&m);
        for(int i=0;i<maxn;i++)G[i].clear();
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                int x;
                scanf("%d",&x);
                if(x){
                    G[i].pb(j);
                }
            }
        }
        num=max(n,m);
        printf("%d\n",hungar());
    }
    return 0;
}

这道题,好像还可以用精确覆盖,一年前贪心没做出来,今天终于用二分匹配做了,等有时间学了精确覆盖在来做一做。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值