• 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;
    }
    
    
  • 相关阅读:
    软件工程课堂练习-最高折扣
    小组开发项目NABC分析
    《梦断代码》阅读笔记二
    软件工程课堂练习--结对开发
    软件工程课堂练习--结对开发
    结对开发四
    电梯调度需求分析
    软件工程课堂练习——结队开发二
    电梯调度——课堂练习
    团队项目开发——用户需求调研报告
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5180499.html
Copyright © 2020-2023  润新知