Asteroids
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13653 | Accepted: 7436 |
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:
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).
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
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).
题意:
给出 N(1 ~ 500) 和 K (1 ~ 10000),代表有一个 N * N 的地图,后给出 K 个小行星,每次武器只能炸毁一行或者一列的小行星,问如何炸毁使花费的武器数最少。
思路:
二分图最大匹配。匈牙利算法。乍一看不觉得是二分图,想了一下突然就想到了。行与列对应会有一种关系,左边集是行编号,右边集是列编号,每次增加的点,都增加一条该边对该列的边。要求所有点都被炸到,那就是所有边都被覆盖,并且用最少的点,那么就是最少顶点覆盖。最少顶点覆盖 = 最大匹配边,所以求最大匹配即可。
为什么最少顶点覆盖 = 最大匹配边?
最少顶点覆盖为用最少的顶点覆盖所有的边,覆盖指的是一个顶点能覆盖它所能直接相连的边。首先先看匹配边,匹配边只要算其中一个点,这条边就被覆盖了,所以对于匹配边来说,最大匹配 = 最少顶点覆盖。然后再看未匹配边,未匹配边的两个顶点的其中一个必定是匹配点,如果不是的话,说明两个都是未匹配点,那么自然就会增加一条匹配,那么就不会是最大匹配了。所以最大匹配 = 最少顶点覆盖。
AC:
#include
#include
using namespace std;
int n;
int w[505][505],vis[505],linker[505];
bool dfs(int u) {
for(int v = 1;v <= n;v++)
if(w[u][v] && !vis[v]) {
vis[v] = 1;
if(linker[v] == -1 || dfs(linker[v])) {
linker[v] = u;
return true;
}
}
return false;
}
int hungary() {
int res = 0;
memset(linker,-1,sizeof(linker));
for(int u = 1;u <= n;u++) {
memset(vis,0,sizeof(vis));
if(dfs(u)) res++;
}
return res;
}
int main() {
int m;
scanf("%d%d",&n,&m);
memset(w,0,sizeof(w));
while(m--) {
int f,t;
scanf("%d%d",&f,&t);
w[f][t] = 1;
}
printf("%d\n",hungary());
return 0;
}