POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))
题目链接:
http://poj.org/problem?id=3041
Description
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
* 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
Sample Input
3 4
1 1
1 3
2 2
3 2
Sample Output
2
Hint
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).
#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增广))的更多相关文章
- POJ 3041 Asteroids 二分图之最大匹配
题意:在一个网格中有若干个点,每一次可以清除一行或者一列,问最少几次可以将网格中的点全部清除. 思路:这个题是一个入门的最大匹配题(这个好像不是思路..).一般的方式就是将 行 看作集合A,列 看作集 ...
- poj 3041 Asteroids (二分图的最大匹配 第一题)
题目:http://poj.org/problem?id=3041 题意:在某个n*n的空间内,分布有一些小行星,某人在里面打炮,放一枪后某一行或某一列的行星就都没了,让求最少的打炮数. 然后把每行x ...
- poj 3041 Asteroids 最小点覆盖/最大匹配
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16242 Accepted: 8833 Descriptio ...
- POJ 3041 Asteroids(二分图最大匹配)
###题目链接### 题目大意: 给你 N 和 K ,在一个 N * N 个图上有 K 个 小行星.有一个可以横着切或竖着切的武器,问最少切多少次,所有行星都会被毁灭. 分析: 将 1~n 行数加入左 ...
- POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)
POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配) Description Bessie wants to navigate her spa ...
- 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids
题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...
- POJ 3041 Asteroids (对偶性,二分图匹配)
题目:POJ 3041 Asteroids http://poj.org/problem?id=3041 分析: 把位置下标看出一条边,这显然是一个二分图最小顶点覆盖的问题,Hungary就好. 挑战 ...
- poj 3041——Asteroids
poj 3041——Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22604 Accep ...
- poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
随机推荐
- Java_Date_02_截断日期到日
oracle 的 trunc 函数能很方便的将日期截断.现在有个需求,需要用java实现与 oracle 的 trunc 函数 相同的功能. 1.需求:将日期截断到日 即 将格式为 2018-01-0 ...
- 【liferay】2、可配置portlet
定义:edit和config模式一般没有使用,对于使用editor和config等模式的portlet,我们可以将他们称为可配置portlet. 我们先新建一个portlet项 添加可配置的控制元素, ...
- 深入理解 React JS 中的 setState
此文主要探讨了 React JS 中的 setState 背后的机制,供深入学习 React 研究之用. 在课程 React.js入门基础与案例开发 中,有些同学会发现 React JS 中的 set ...
- Ubuntu(Linux)下如何用源码文件安装软件
在Ubuntu中附带了丰富的软件,这些软件一般使用图形化的自动方式(“添加/删除”或“新立得”)即可轻松安装,但是对于那些刚刚问世的新软件,Ubuntu的源中还未收录其中,这时我们就需要用到一种更通用 ...
- 求字符数组逆序数(poj1007)
int InversionNumber(char* s,int len) { int ans=0; //s逆序数 int A,C,G; //各个字母出现次数,T是最大的,无需计算T出现次数 A=C ...
- 人工智能一:Al学习路线
想要跨入AI的大门,如何跨?终于找到了一套学习方法 努力向你靠近 2017-12-03 07:14:51 当下人工智能领域的发展已经有了燎原之势,麦肯锡全球研究院就认为人工智能促进对社会的转变速度将比 ...
- 微信小程序参数二维码6问6答
微信小程序参数二维码[基础知识篇],从6个常见问题了解小程序参数二维码的入门知识. 1.什么是小程序参数码? 微信小程序参数二维码:针对小程序特定页面,设定相应参数值,用户扫描后进入相应的页面. 2. ...
- Redis学习笔记(三)Redis支持的5种数据类型的总结
继续Redis学习笔记(二)来说说剩余的三种数据类型. 三.列表类型(List) 1.介绍 列表类型可以存储一个有序的字符串列表,常用的操作是向列表两端添加元素,或者获得列表的一段片段.列表类型内部是 ...
- 这是要逆天么,看我控制台程序玩Microsoft XPS Document 打印
主要是想试试Microsoft XPS Document 打印时怎样去掉那个“将打印输出另存为”对话框 using System; using System.Drawing; using System ...
- Mysql:执行source sql脚本时,出现:error 2
Centos下部署mysql: 1.yum -y install mysql*; 2.service mysqld start; 3.chkconfig mysqld on; 4.设置用户名和密码:m ...