zoukankan      html  css  js  c++  java
  • POJ3041(最小顶点覆盖)

    Asteroids
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 18611   Accepted: 10134

    Description

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid. 

    Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.

    Input

    * Line 1: Two integers N and K, separated by a single space. 
    * Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.

    Output

    * Line 1: The integer representing the minimum number of times Bessie must shoot.

    Sample Input

    3 4
    1 1
    1 3
    2 2
    3 2
    

    Sample Output

    2
    思路:把光束当作图的顶点,而把小行星当作光束的边。光束的方案为求一个顶点集合S,使得图中所有的边的一端至少有一个顶点在S中,即最小顶点覆盖问题。在二分图中,|最小顶点覆盖|=|最大匹配|

    #include <cstdio>
    #include <cstring>
    #include <vector>
    using namespace std;
    const int MAXN=1005;
    int n,m;
    vector<int> arc[MAXN];
    int match[MAXN],vis[MAXN];
    bool dfs(int u)
    {
        for(int i=0;i<arc[u].size();i++)
        {
            int to=arc[u][i];
            if(!vis[to])
            {
                vis[to]=1;
                int w=match[to];
                if(w==-1||dfs(w))
                {
                    match[u]=to;
                    match[to]=u;
                    return true;
                }
            }
        }
        return false;
    }
    int max_flow()
    {
        int ans=0;
        memset(match,-1,sizeof(match));
        for(int i=1;i<=n;i++)
        {
            if(match[i]==-1)
            {
                memset(vis,0,sizeof(vis));
                if(dfs(i))    ans++;
            }
        }
        return ans;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=1;i<=n;i++)    arc[i].clear();
            for(int i=0;i<m;i++)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                v+=n;
                arc[u].push_back(v);
                arc[v].push_back(u);
            }
            n+=n;
            int res=max_flow();
            printf("%d
    ",res);
        }
        return 0;
    }
    
    
  • 相关阅读:
    iOS遍历程序内某个文件夹下所有文件的属性
    CATransition 转场动画
    Xcode安装的推送证书所在目录
    UIMenuController 实现长按显示自定义菜单功能
    ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件
    购物车界面,不同section,点击增减物品,确定取消选中的逻辑判断
    iOS UINavigationController
    iOS9 URL Schme 白名单
    iOS9 HTTPS
    iOS9 后台定位
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5180499.html
Copyright © 2011-2022 走看看