BZOJ 1741 [Usaco2005 nov]Asteroids 穿越小行星群 二分图最小边覆盖

来源:互联网 发布:samba windows 编辑:程序博客网 时间:2024/05/07 04:15

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.

贝茜想驾驶她的飞船穿过危险的小行星群.小行星群是一个NxN的网格(1≤N≤500),在网格内有K个小行星(1≤K≤10000). 幸运地是贝茜有一个很强大的武器,一次可以消除所有在一行或一列中的小行星,这种武器很贵,所以她希望尽量地少用.给出所有的小行星的位置,算出贝茜最少需要多少次射击就能消除所有的小行星.

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.

    第1行:两个整数N和K,用一个空格隔开.
    第2行至K+1行:每一行有两个空格隔开的整数R,C(1≤R,C≤N),分别表示小行星所在的行和列.

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

INPUT DETAILS:

The following diagram represents the data, where "X" is an
asteroid and "." is empty space:
X.X
.X.
.X.

Sample Output

2

OUTPUT DETAILS:

Bessie may fire across row 1 to destroy the asteroids at (1,1) and
(1,3), and then she may fire down column 2 to destroy the asteroids
at (2,2) and (3,2).

HINT





传送门
挺早之前用贪心做,wa成狗狗……

可以看到对于任意X,行和列里面必须至少有一个被选。
我们把在坐标(x,y)的小行星看做一条边(x,y),
那么也就是说对于所有的边,必须要有至少一个端点被选中。
题目要求的就是最小被选中的端点数目。

显然这是一个二分图……
行向列连边,然后要选出一些点,使得所有边都被覆盖到。
这就是所谓二分图最小点覆盖。
二分图最小点覆盖=二分图最大匹配。
所以直接跑匈牙利就ok了。

不是很懂跑网络流的大神做法……


#include<bits/stdc++.h>using namespace std;int read(){    int x=0,f=1;char ch=getchar();    while (ch<'0' || ch>'9'){if (ch=='-') f=-1;ch=getchar();}    while (ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}const int N=505;int n,K,tag;bool mp[N][N];int vis[N],match[N];bool dfs(int u){int t;for (int i=1;i<=n;i++)if (mp[u][i] && vis[i]!=tag){vis[i]=tag;t=match[i];match[i]=u;if (!t || dfs(t)) return 1;match[i]=t;}return 0;}int main(){n=read(),K=read();for (int i=1;i<=K;i++) mp[read()][read()]=1;tag=0;int ans=0;for (int i=1;i<=n;i++){tag++;if (dfs(i)) ans++;}printf("%d\n",ans);return 0;}

原创粉丝点击