问题描述
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.
详情见: https://vjudge.net/problem/POJ-3041
思考:
- 把行作为左集合,列作为右集合
- map[][]邻接矩阵表示图,map[i][j]表示此点有障碍即有边相连
- 因为要用最少的炮弹清除障碍,那么求最小覆盖点即可;
实现:
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static int n;//左边集合的顶点数
public static int k;//障碍物数量
public static int ans = 0;//最大匹配数
public static int[][] map = new int[505][505];//图
public static int[] link = new int[10001]; //匹配的顶点,i为右集合的点,link[i]为左集合的点,初始为-1表示无匹配
public static int[] use = new int[10001];//i是否被标记过初始为0表示未被标记
static void Init(){//初始化link
for (int i = 0; i < link.length ; i++) {
link[i] = -1;
}
}
static void hungary(){//匈牙利算法
Init();
for (int i = 1; i <= n ; i++) {
for (int j = 1; j <= n; j++) {
use[j] = 0;
}
if(hungary_dfs(i) == true)ans++;
}
System.out.println(ans);
}
static boolean hungary_dfs(int u) {
if(u == -1)return true;
for (int i = 1; i <= n ; i++) {
if(use[i] == 1 || map[u][i] != 1)continue;
use[i] = 1;
if(link[i] == -1 || hungary_dfs(link[i]) == true){
//i未被匹配或者询问后可以被匹配
link[i] = u;
return true;
}
}
return false;
}
public static void main(String[] args) {
InputReader inputReader = new InputReader(new BufferedInputStream(System.in));
n = inputReader.nextInt();
k = inputReader.nextInt();
for (int i = 0; i < k ; i++) {
int r = inputReader.nextInt();
int c = inputReader.nextInt();
map[r][c] = 1;//i为左集合j为右集合 (i,j) = 1表示此处有障碍
}
hungary();
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreElements()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e) {
return false;
}
}
return true;
}
public int nextInt() {
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
BigInteger nextBigInteger()
{
return new BigInteger(next());
}
BigDecimal nextBigDecimal()
{
return new BigDecimal(next());
}
}