POJ 3041 Asteroids(最小点覆盖)

题目链接:
POJ 3041 Asteroids
题意:
给出m个点的所在行列,每次可以选择清除一行或者一列,问清除这些点的最小操作次数是多少?
分析:
将所有点的行看成一组,所有的点列看成一组,对每个点的行和列建一条边,每条边就代表一个点,清除一行或者一列可以看成选择二分图中的一个点,清除所有的点,就相当于选择二分图中的若干点连接所有的边。
这样一来就符合最小点覆盖的定义了!再根据最小点覆盖数=最大匹配数,利用匈牙利算法即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#include <map>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 510;

int n, m, total, cntx, cnty;
int head[MAX_N], vis[MAX_N], match[MAX_N];

struct Edge{
    int to, next;

    Edge() {}
    Edge(int _to,int _next) : to(_to), next(_next) {
    }
}edge[MAX_N * MAX_N];

inline void AddEdge(int from, int to)
{
    edge[total].to = to;
    edge[total].next = head[from];
    head[from] = total++;
}

inline bool dfs(int u)
{
    for(int i = head[u]; i != -1; i = edge[i].next){
        int to = edge[i].to;
        if(!vis[to]){
            vis[to] = 1;
            if(match[to] == -1 || dfs(match[to])){
                match[to] = u;
                return true;
            }
        }
    }
    return false;
}

inline void hungary()
{
    int ans = 0;
    for(int i = 0; i < cntx; ++ i){
        memset(vis,0,sizeof(vis));
        if(dfs(i)) ans++;
    }
    cout << ans << endl;
}

int main()
{
    freopen("E.in", "r", stdin);
    IOS;
    while(cin >> n >> m){
        memset(head, -1, sizeof(head));
        memset(match, -1, sizeof(match));
        cntx = cnty = total = 0;
        map<int, int> mpx;
        map<int, int> mpy;
        for(int i = 0; i < m; ++ i){
            int x, y;
            cin >> x >> y;
            if(mpx.find(x) == mpx.end()){
                mpx[x] = cntx++;
            }
            if(mpy.find(y) == mpy.end()){
                mpy[y] = cnty++;
            }
            AddEdge(mpx[x], mpy[y]);
        }
        hungary();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值