题目链接:

http://poj.org/problem?id=3041

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.

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

题意描述:
输入矩阵的大小和小行星的个数及坐标
计算并输出至少需要多少颗能量弹消灭掉所有的小行星
解题思路:
首先建立二分图,行和列为两个集合,有小行星的位置存入邻接矩阵为1,使用匈牙利算法,计算二分最大匹配,最后求的最小顶点覆盖。
AC代码:
 #include<stdio.h>
#include<string.h>
int n,k,e[][],pred[],queue[],cx[],cy[];
int maxmatch();
int main()
{
int i,x,y;
while(scanf("%d%d",&n,&k) != EOF)
{
memset(e,,sizeof(e));
for(i=;i<=k;i++)
{
scanf("%d%d",&x,&y);
e[x][y]=;
}
printf("%d\n",maxmatch());//输出最小顶点覆盖数
}
return ;
}
int maxmatch()
{
int i,j,y;
int cur,tail,res=;
memset(cx,0xff,sizeof(cx));
memset(cy,0xff,sizeof(cy)); for(i=;i<=n;i++)
{
if(cx[i] != -)//找到x集合中每个未盖点i进行一次找交错轨
continue; for(j=;j<=n;j++)
pred[j]=-;//初始化为-2 cur=;//队列初始化
tail=; for(j=;j<=n;j++)//将i的邻接顶点加入队列
{
if(e[i][j])
{
pred[j]=-;//-1表示遍历到,是邻接顶点
queue[tail++]=j;
}
} while(cur < tail)//BFS
{
y=queue[cur];
if(cy[y]==-)
break;//找到了一个未匹配的点,则找到了一条交错轨
cur++;
//已经匹配给cy[y]了,从cy[y]出发,将其邻接点加入队列
for(j=;j<=n;j++)
{
if(pred[j] == - && e[ cy[y ]][j])
{
pred[j]=y;
queue[tail++]=j;
}
}
}
if(cur == tail)//没有找到交错轨
continue; while(pred[y] > -)//更改交错轨上的匹配状态
{
cx[ cy[pred[y]] ] = y;
cy[y]=cy[ pred[y] ];
y=pred[y];
}
cy[y]=i;
cx[i]=y; res++;//匹配数加1
}
return res;
}

POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))的更多相关文章

  1. POJ 3041 Asteroids 二分图之最大匹配

    题意:在一个网格中有若干个点,每一次可以清除一行或者一列,问最少几次可以将网格中的点全部清除. 思路:这个题是一个入门的最大匹配题(这个好像不是思路..).一般的方式就是将 行 看作集合A,列 看作集 ...

  2. poj 3041 Asteroids (二分图的最大匹配 第一题)

    题目:http://poj.org/problem?id=3041 题意:在某个n*n的空间内,分布有一些小行星,某人在里面打炮,放一枪后某一行或某一列的行星就都没了,让求最少的打炮数. 然后把每行x ...

  3. poj 3041 Asteroids 最小点覆盖/最大匹配

    Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16242 Accepted: 8833 Descriptio ...

  4. POJ 3041 Asteroids(二分图最大匹配)

    ###题目链接### 题目大意: 给你 N 和 K ,在一个 N * N 个图上有 K 个 小行星.有一个可以横着切或竖着切的武器,问最少切多少次,所有行星都会被毁灭. 分析: 将 1~n 行数加入左 ...

  5. POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)

    POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配) Description Bessie wants to navigate her spa ...

  6. 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids

    题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...

  7. POJ 3041 Asteroids (对偶性,二分图匹配)

    题目:POJ 3041 Asteroids http://poj.org/problem?id=3041 分析: 把位置下标看出一条边,这显然是一个二分图最小顶点覆盖的问题,Hungary就好. 挑战 ...

  8. poj 3041——Asteroids

    poj       3041——Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22604   Accep ...

  9. poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

随机推荐

  1. bat常用命令

    1.@它的作用是隐藏它后面这一行的命令本身(只能影响当前行).2.echo中文为"反馈"."回显"的意思.它其实是一个开关命令,就是说它只有两种状态:打开和关闭 ...

  2. 扩展1 - Python 获取当前时间的用法

    1.先导入库:import datetime 2.获取当前日期和时间:now_time = datetime.datetime.now() 3.格式化成我们想要的日期:strftime() 比如:“2 ...

  3. HtmlTestRunner无法生成HTML报告问题

    环境: Python3.6 + Selenium3.3.0 + HtmlTestRunner1.1.0 ON Windows10 IDE: PyCharm HtmlTestRunner地址: http ...

  4. DES加解密、JavaScript、Java

    JavaScript代码    DES.js /** * Created by Andy on 2017/11/30. */ /** * DES加密/解密 * @Copyright Copyright ...

  5. 解决SVN造成的桌面图标问号

    之前不小心直接将版本库 的内容检出 到桌面,后才发现桌面上的文件 都变成了问号,本来也以为没有多大问题,删除.svn 即可,可是删除所有的.svn后,桌面上还是显示问号,刷新了很多次,还重启电脑 了, ...

  6. 使用Template格式化Python字符串

    对Python字符串,除了比较老旧的%,以及用来替换掉%的format,及在python 3.6中加入的f这三种格式化方法以外,还有可以使用Template对象来进行格式化. from string ...

  7. 中文版microbit:TurnipBit显示动态滚动字符教程实例

    随着当今社会的发展,社会的进步,家长们越来越忙碌,致使家长们在孩子成长过程中陪孩子的互动的时间越来越少,为此,TurnipSmart公司制作的一款MicroPython开发板--TurnipBit,这 ...

  8. python基础(八)生成器,迭代器,装饰器,递归

    生成器 在函数中使用yield关键字就会将一个普通的函数变成一个生成器(generator),普通的函数只能使用return来退出函数,而不执行return之后的代码.而生成器可以使用调用一个next ...

  9. Oracle 存储过程以及存储函数

    以下的一些例子是基于scott用户下的emp表的数据,一和二使用的均为in,out参数,最后一个综合练习使用了 in out参数 一.存储过程 1.创建无参的存储过程示例  ------ hello ...

  10. ABAP开发人员未来应该学些什么

    2007年1月我大学毕业进入SAP Chengdu从事开发工作,到今天已经整整11年了. 这张图在ABAP开发圈子里曾广为流传: 为了避免这种窘境发生在ABAPer身上,我们应该做点什么呢? 2017 ...