POJ 3041 Asteroids(最小点覆盖)题解

题意:n*n的网格中有k个点,开一枪能摧毁一行或一列的所有点,问最少开几枪

思路:我们把网格看成两个集合,行集合和列集合,如果有点x,y那么就连接x->y,所以我们只要做最小点覆盖就好了。

参考:POJ3041-Asteroids

代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 500 + 10;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int linker[maxn], n;
int g[maxn][maxn];
bool used[maxn];
bool dfs(int u){
    for(int v = 1; v <= n; v++){
        if(g[u][v] && !used[v]){
            used[v] = true;
            if(linker[v] == -1 || dfs(linker[v])){
                linker[v] = u;
                return true;
            }
        }
    }
    return false;
}
int hungry(){
    int res = 0;
    memset(linker, -1, sizeof(linker));
    for(int u = 1; u <= n; u++){
        memset(used, false, sizeof(used));
        if(dfs(u)) res++;
    }
    return res;
}

int main(){
    int k;
    while(~scanf("%d%d", &n, &k)){
        memset(g, 0, sizeof(g));
        for(int i = 1; i <= k; i++){
            int x, y;
            scanf("%d%d", &x, &y);
            g[x][y] = 1;
        }
        printf("%d\n", hungry());
    }
    return 0;
}

 

posted @ 2019-03-05 14:23  KirinSB  阅读(166)  评论(0编辑  收藏  举报