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 ...
随机推荐
- Bmob 移动后端云服务器平台实现登录注册
源码下载:http://download.csdn.net/download/jjhahage/10034519 PS:一般情况下,我们在写android程序的时候,想要实现登录注册功能,可以选择自己 ...
- ES6 函数的扩展3
箭头函数 基本用法 ES6允许使用"箭头"(=>)定义函数 var f = v => v; 上面的箭头函数等同于: var f = function(v) { retu ...
- Python爬虫——爬豆瓣登录页面
直接上代码 import urllib.request import http.cookiejar from lxml import etree # from spiderImg import get ...
- 7、正确的赚钱方式 - CEO之公司管理经验谈
创业者创办公司,最初的目的就是为了赚钱,而普通的员工来公司上班,为了生计,也是以赚钱为目的.今天我们就讲讲正确的赚钱方式. 一.去公司上班: 来公司上班是第一个主要的赚钱方式.不管是员工还是公司领导, ...
- SQL 多列合并一列
select rtrim(姓)+ rtrim(名) as 姓名 from tb
- 12.python进程\协程\异步IO
进程 创建进程 from multiprocessing import Process import time def func(name): time.sleep(2) print('hello', ...
- 使用ui-route实现多层嵌套路由
一.预期实现效果: https://liyuan-meng.github.io/uiRouter-app/index.html (项目地址:https://github.com/liyuan-meng ...
- 第十二章:Python の 网络编程进阶(一)
本課主題 RabbitMQ 的介紹和操作 Hello RabbitMQ RabbitMQ 的工作队列 消息确应.消息持久化和公平调度模式 RabbitMQ的发布和订阅 RabbitMQ的主题模式 Ra ...
- mycat安装与配置
1.安装jdk 测试jdk是否已经安装 [root@node002 ~]# java -version-bash: java: command not found 创建解压目录 [root@node0 ...
- SQLAlchemy基础操作二
多线程示例 import time import threading from sqlalchemy.ext.declarative import declarative_base from sqla ...