题目链接:

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. ELK日志检索并邮件微信通知

    简介 脚本为通过api检索日志内容,并通过邮件或者微信发送出来. 脚本 index检索脚本 #!/usr/bin/env python # coding:utf-8 from elasticsearc ...

  2. .Net WinForm 控件键盘消息处理剖析

    在WinForm控件上我们可以看到很多关于键盘消息处理的方法,比如OnKeyDown, OnKeyPress, ProcessCmdKey, ProcessDialogKey,IsInputKey等等 ...

  3. Python day 7(1) 模块

    一:模块 1 在Python中,一个.py文件就称之为一个模块(Module) 2 Python的好处,优点: a  提高了代码的可维护性 b  当一个模块编写完毕,就可以被其他地方引用.我们在编写程 ...

  4. StringMVC @RequestParam属性

    1.jsp: <a href="springmvc/testRequestParam?username=allen&age=sss">test RequsetP ...

  5. ssh整合开发

    ssh整合思想 ssh整合 第一步:导入ssh相关jar包 第二步:搭建struts环境   (1)创建 action  struts.xml配置文件, 配置action struts.xml约束 & ...

  6. 中国孩子的micro:bit:TurnipBit自制小乐器教程实例

    孩子们是最贪玩的也是最聪明的,因此在过去的数年中,市面上出现了不少寓教于乐的理工科知识(STEM)学习新方法.如今这类产品中又有了一名新成员,TPYBoard重磅推出一款针对小白.中小学生的可编程计算 ...

  7. css3实现梯形三角

    近期移动端项目中,图片很多 移动端尽量少图片,以便提升加载速度!这时候css3可以大放光芒比如梯形的背景图 --------------------------------- ------------ ...

  8. [Spark内核] 第32课:Spark Worker原理和源码剖析解密:Worker工作流程图、Worker启动Driver源码解密、Worker启动Executor源码解密等

    本課主題 Spark Worker 原理 Worker 启动 Driver 源码鉴赏 Worker 启动 Executor 源码鉴赏 Worker 与 Master 的交互关系 [引言部份:你希望读者 ...

  9. Android 中log 找到关键log

    SYS_ANDROID_EVENT_LOG1. aee_exp文件夹中有 db.fatal.00.SWT. 2. 准备工作: gat打开db.fatal.00.SWT.dbg文件,即会生成.DEC文件 ...

  10. wifipineapple的evilportal

    复制模块: scp -r evilportal root@172.16.42.1:/pineapple/components/infusions 更新数据源, 更新相关依赖: opkg update ...