Asteroids POJ - 3041
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.

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).

OJ-ID:
poj-3041

author:
Caution_X

date of submission:
20191002

tags:
二分图最小点覆盖

description modelling:
给定一个N×N的网格,网格上有很多的点,现在可以一次性消除某行或者某列上的所有点,问最少需要几次可以把所有的点都消除

major steps to solve it:
思路:
(1) 建立一个二分图,两边的点分别是横坐标和纵坐标,网格上出现的点(x,y)表示二分图左边的x可以和右边的y匹配
(2) 当我们选择左边图的一个点x清除时,实际上就是把横坐标为x的点清除,那么与该点x匹配的右边图的所有点也会被一起清除,(选择右边点y清除同理)
(3) 现在问题转换成了想要覆盖所有边的最少点数,即:二分图的最小匹配点数
步骤:
(1) 建图
(2) 二分图的最小匹配=二分图的最大覆盖
二分图最大覆盖求法:
(1) 选择左图的一个点x1,遍历与该点有关的边,在右图找到一个匹配点y1,连接2点(x1,y1)
(2) 继续选择左图的点x2,如果该点对应的匹配点是y1,则查找x1有没有新的匹配点y2,若有,则(x1,y2),(x2,y1),若没有,继续找x2的匹配点,如果遍历到最后都没有找到匹配点,则继续左图的下一个点,直到左图无点可用

AC code:

#include<cstdio>
#include<cstring>
using namespace std;
int line[][];
int used[],g[];
int N,K;
bool found(int x)
{
for(int i=;i<=N;i++) {
if(line[x][i]&&!used[i]) {
used[i]=;
if(g[i]==-||found(g[i])) {
g[i]=x;
return ;
}
}
}
return ;
}
int main()
{
//freopen("input.txt","r",stdin);
memset(line,,sizeof(line));
memset(g,-,sizeof(g));
scanf("%d%d",&N,&K);
for(int i=;i<K;i++) {
int x,y;
scanf("%d%d",&x,&y);
line[x][y]=;
}
int sum=;
for(int i=;i<=N;i++) {
memset(used,,sizeof(used));
if(found(i)) sum++;
}
printf("%d\n",sum);
return ;
}

Asteroids POJ - 3041 二分图最小点覆盖的更多相关文章

  1. Asteroids POJ - 3041 【最小点覆盖集】

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  2. POJ 3041(最小点覆盖)

    题意: 假如你如今正处在一个N*N的矩阵中,这个矩阵里面有K个障碍物,你拥有一把武器,一发弹药一次能消灭一行或一列的障碍物,求最小的弹药消灭所有障碍物 输入为:     N K 接下来有K行,每行包括 ...

  3. poj 2226 二分图 最小点覆盖 , 最大流

    题目就是问怎样用最小的板覆盖全部的草地.能够横着放.也能够竖着放,同意一个草地放多个点. 建图方法就是 每一个横向的草地作为X,纵向连续的草地作为Y.     X连接Y的边表示,  这里有他们的公共点 ...

  4. Asteroids - poj 3041(二分图最大匹配问题)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17258   Accepted: 9386 Description Be ...

  5. 二分图 最小点覆盖 poj 3041

    题目链接:Asteroids - POJ 3041 - Virtual Judge  https://vjudge.net/problem/POJ-3041 第一行输入一个n和一个m表示在n*n的网格 ...

  6. Asteroids POJ - 3041 匈牙利算法+最小点覆盖König定理

    题意: 给出一个N*N的地图N   地图里面有K个障碍     你每次可以选择一条直线 消除这条直线上的所有障碍  (直线只能和列和行平行) 问最少要消除几次 题解: 如果(x,y)上有一个障碍 则把 ...

  7. [POJ] 2226 Muddy Fields(二分图最小点覆盖)

    题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...

  8. Asteroids POJ - 3041

    Asteroids POJ - 3041 题目大意:N*N的地图里,存在一些小行星,Bessie有个很牛x但又很耗蓝的武器,一次可以消灭一行或者一列的所有小行星,问最少使用多少次这个武器可以消灭所有的 ...

  9. POJ2226 Muddy Fields(二分图最小点覆盖集)

    题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作 ...

随机推荐

  1. UnityScript基础

    基本格式 1 cc.Class({ 2 extends: cc.Component, 3 4 properties: { 5 }, 6 7 // use this for initialization ...

  2. Docker Machine的使用

    前面两篇文章(<Asp.Net Core 发布到 Docker(Linux Centos 虚拟机,使用Dockerfile)> <Docker Compose的使用>),简单介 ...

  3. springboot--事务的使用

    @Transactional原理 事务是一些sql语句对数据库操作的集合,因此如果在一个Java方法里涉及了对数据库的操作,业务需要的话我们就可以考虑把这些操作作为一个事务.通过在方法上加个@Tran ...

  4. JMeter更改语言为英文

    1. 进入目录apache-jmeter-2.13\bin 2. 打开jmeter.properties 3. 取消“language=en”前的注释 4. 重新打开JMeter,即可看到语言已经变成 ...

  5. CF980B Marlin 构造 思维 二十四

    Marlin time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  6. 关于Ubunto在VMwark中无法全屏。

    1.右键点击Ubunto桌面,进入终端 输入: 1.sudo apt-get install open-vm*   安装依赖项 2.sudo apt-get install open-vm-tools ...

  7. webpack多页应用架构系列(一):一步一步解决架构痛点

    这系列文章讲什么? 前些时间,写过一个项目,前后端分离,没有借助任何框架,项目页面特别的多,页面都是html直接写的,许多公共html,写了好多处,有一个地方需要改就得改好多地方,js也是随意写,每个 ...

  8. 大多数人不知道的:HashMap链表成环的原因和解决方案

    引导语 在 JDK7 版本下,很多人都知道 HashMap 会有链表成环的问题,但大多数人只知道,是多线程引起的,至于具体细节的原因,和 JDK8 中如何解决这个问题,很少有人说的清楚,百度也几乎看不 ...

  9. git 生成密钥

    1.本地安装好git: 2.桌面右键 Git Bash Here 打开git命令行: 3.ssh-keygen -t rsa -C "nideyouxiang@xxx.com"  ...

  10. MySQL性能调优与架构设计(简朝阳)

    https://www.cnblogs.com/crazylqy/category/625963.html