Poj3041Asteroids(java)匈牙利算法求最小覆盖点

问题描述

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

  1. Line 1: Two integers N and K, separated by a single space.
  2. 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
  3. Line 1: The integer representing the minimum number of times Bessie must shoot.
    详情见: https://vjudge.net/problem/POJ-3041

思考:

  1. 把行作为左集合,列作为右集合
  2. map[][]邻接矩阵表示图,map[i][j]表示此点有障碍即有边相连
  3. 因为要用最少的炮弹清除障碍,那么求最小覆盖点即可;

实现:

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());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值