Asteroids
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21065 | Accepted: 11437 |
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.
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.
* 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
Hint
INPUT DETAILS:
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
OUTPUT DETAILS:
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
OUTPUT DETAILS:
1、有一个n*n的矩阵,在矩阵上有k个行星,用武器射击一次可以消灭一行或者一列的行星,求消灭所有的行星的最少射击次数。
2、最小点覆盖数 = 最大匹配数
主要在于转化:看图:
这样,在建成的二分图中,一条边代表一个行星,左边的一个点代表横向射击,右边的一个点代表竖向射击。
要求最少的射击次数把所有的行星消灭,即选择最少的点,把所有的边覆盖。这不正是求最小点覆盖数吗。
ps:
顶点覆盖:在顶点集合中,选取一部分顶点,这些顶点能够把所有的边都覆盖了。这些点就是顶点覆盖集
最小顶点覆盖:在所有的顶点覆盖集中,顶点数最小的那个叫最小顶点集合。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int u,v,p[1004][1001],vis[1005],match[1005];
int dfs(int e)
{
int i,j;
for(i=1;i<=u;i++)
{
if(p[e][i]&&!vis[i])
{
vis[i]=1;
if(dfs(match[i])||match[i]==-1)
{
match[i]=e;//(e,i)匹配
return 1;
}
}
}
return 0;
}
int main()
{
int i,j,k,n;
while(~scanf("%d%d",&n,&k))
{
u=n;
v=n;
int ans=0;
memset(p,0,sizeof(p));
memset(match,-1,sizeof(match));
for(i=1;i<=k;i++)
{
int x,y;
scanf("%d%d",&x,&y);
p[x][y]=1;
}
for(i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i)) ans++;
}
printf("%d\n",ans);
}
return 0;
}