POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)

POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)

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

Http

POJ:https://vjudge.net/problem/POJ-3041
UESTC:https://vjudge.net/problem/UESTC-253

Source

二分图匹配

翻译

Bessie 驾驶着他的太空飞船呆呆2号在太空旅行,途径一段危险地带,他希望自己能安全通过这段区域,于是他将这片区域的地图扫描进入了太空飞船,地图是一个N x N的网络 (1 <= N <= 500),其中有K颗小行星 (1 <= K <= 10,000)。

还好Bessie有一个强力的武器能够一发光束将一整行或者一整列的小行星轰成灰烬。 这种光束的价格高昂,材料稀有,所以他希望更少的使用这个光束。现在把地图给你,你能帮Bessie计算一下,摧毁掉这些小行星至少需要几发光束。

解决思路

对于这道题目,我们把每一个点(x,y)的横纵坐标分开。对于所有的横坐标我们设一个集合X,而对于纵坐标用一个集合Y。若有障碍物(x,y),我们在X中的x连一条边到Y中的y。这样原来的所有障碍点就成了图中的一条边。

最小点覆盖的意思是,求出最少的点的集合使得每一条边都至少有一个端点在集合中。在二分图中,最小点覆盖=最大匹配数

至于什么是最大匹配呢,就是求一个最大的边集使得没有任意两个边有同样的端点。

更多关于二分图的相关问题可以看一看这一篇文章:http://blog.csdn.net/tham_/article/details/72872199

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

const int maxN=600;
const int inf=2147483647;

int n,K;
vector<int> E[maxN];
bool vis[maxN];//Y集合中的元素是否访问过了
int Mark[maxN];//Y集合的元素对应的X中的元素,若为-1表示还未标记

bool dfs(int u);

int main()
{
    int T;
    //cin>>T;    /在UESTC上有多组数据,注意
    //for (int ti=1;ti<=T;ti++)
    //{
        memset(Mark,-1,sizeof(Mark));
        int x,y;
        int cnt=0;
        cin>>n>>K;
        for (int i=1;i<=n;i++)
            E[i].clear();
        for (int i=1;i<=K;i++)
        {
            cin>>x>>y;
            E[x].push_back(y);
        //E[y].push_back(x);注意是单向边,要不然dfs里会一直卡住
        }
        for (int i=1;i<=n;i++)
        {
        //if (Mark[i]==-1)//Mark是Y集合对应的点,不是X集合的,所以不要判
            {
                memset(vis,0,sizeof(vis));
                if (dfs(i))
                    cnt++;
            }
        }
        cout<<cnt<<endl;
        /*for (int i=1;i<=n;i++)
            cout<<Mark[i]<<' ';
        cout<<endl;*/
    //}
    return 0;
}

bool dfs(int u)
{
    //cout<<u<<' '<<vis[u]<<endl;
    //if (vis[u]==1)
    //    return 1;
    //vis[u]=1;
    for (int i=0;i<E[u].size();i++)
    {
        int v=E[u][i];
        if (vis[v]==0)
        {
            vis[v]=1;
            if ((Mark[v]==-1) || (dfs(Mark[v])) )//注意是dfs(Mark[v])而不是dfs(v),因为边都是从X集合指向Y集合,而Mark[v]相当于一条v->Mark[v]的反向边(Mark[v]代表v对应的X集合中的点),所以直接传递给下一层dfs的是v的对边;
           //又因为C++的特性是||前的表达式如果为真,则不再计算后边表达式的值,这可以避免无限递归
            {
                Mark[v]=u;//更新v相对的点
                return 1;
            }
        }
    }
    return 0;
}
posted @ 2017-07-08 22:04  SYCstudio  阅读(295)  评论(0编辑  收藏  举报