zoukankan      html  css  js  c++  java
  • 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
    ", hungry());
        }
        return 0;
    }
  • 相关阅读:
    PHP 魔术常量
    PHP 魔术方法
    php函数serialize()与unserialize()
    10 件有关 JavaScript 让人费解的事情
    windows下安装Python2和Python3共存
    SQL 行转列===列转行
    Redis 分布式锁
    RabbitMQ
    @Autowired
    AOP 日志切面
  • 原文地址:https://www.cnblogs.com/KirinSB/p/10476524.html
Copyright © 2011-2022 走看看