[poj3041]Asteroids(二分图的最小顶点覆盖)
题目大意:$N*N$的网格中有$n$颗行星,若每次可以消去一整行或一整列,求最小的攻击次数使得消去所有行星。
解题关键:将光束当做顶点,行星当做连接光束的边建图,题目转化为求该图的最小顶点覆盖,图的最小顶点覆盖是$NP$问题,又因为该图是二分图(水平方向的点和竖直方向的点),而二分图的最大匹配=最小顶点覆盖,即可求解该问题。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#define maxn 20020
using namespace std;
typedef long long ll;
int n,m;
struct Edge{
int nxt;
int to;
int w;
}e[maxn];
int head[maxn],cnt;
void add_edge(int u,int v){
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt++;
}
int pre[maxn];
bool vis[maxn];
bool dfs(int u){
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
if(!vis[v]){
vis[v]=true;
if(pre[v]==-||dfs(pre[v])){
pre[v]=u;
//pre[u]=v;
return true;
}
}
}
return false;
} int hungary(){
int ans=;
memset(pre,-,sizeof pre);
for(int i=;i<=*n;i++){
if(pre[i]==-){
memset(vis,,sizeof vis);
if(dfs(i)) ans++;
}
}
return ans;
}
int a,b,k; int main(){
while(scanf("%d%d",&n,&k)!=EOF){
memset(head, -, sizeof head);
cnt=;
for(int i=;i<=k;i++){
scanf("%d%d",&a,&b);
add_edge(a,b+n);
}
printf("%d",hungary());
}
return ;
}
[poj3041]Asteroids(二分图的最小顶点覆盖)的更多相关文章
- poj3041 Asteroids 匈牙利算法 最小点集覆盖问题=二分图最大匹配
/** 题目:poj3041 Asteroids 链接:http://poj.org/problem?id=3041 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一把枪,每一发子弹可 ...
- POJ3041 Asteroids(二分图最小点覆盖)
Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...
- poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ3041 Asteroids 二分图匹配 匈牙利算法
原文链接http://www.cnblogs.com/zhouzhendong/p/8229200.html 题目传送门 - POJ3041 题意概括 有一个n*n的矩阵,有些点是障碍物. 现在每次可 ...
- POJ3041 Asteroids(二分图最大匹配)
题目链接. 分析: 暂略. AC代码: #include <iostream> #include <cstdio> #include <cstring> #incl ...
- poj3041 Asteroids(二分图最小顶点覆盖、二分图匹配)
Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...
- hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】
Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu3861 强连通分量缩点+二分图最最小路径覆盖
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1054(KB10-H 最小顶点覆盖)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- Nhibernate系列学习之(四) 数据操作
数据操作,在这里主要介绍INSERT.UPDATE.DELETE.我们在使用NHibernate的时候,如果只是查询数据,不需要改变数据库的值,那么是不需要提交或者回滚到数据库的. 一.INSERT ...
- L123
My heart, the bird of the wilderness, has found its sky in your eyes. 我的心是旷野的鸟,在你的双眼中找到了天空.His main ...
- charles mock数据时解决乱码问题
1.新建文件,不要后缀名 2.下载Notepad++ 软件,使用该软件打开文件:设置:格式---->以UTF-8无BOM格式编码 3.正常在文件中写入数据,maplocal 就可以了
- np.unique 的实现
1. 简单实现 import numpy as np def unique(ar): perm = ar.argsort() aux = ar[perm] flag = np.concatenate( ...
- ACM学习历程—SNNUOJ1214 矩阵1(二分)
题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1214 这是这次微软实习面试的一道题,题目大意就是:有一个n*m的矩阵,已知它每一行都是 ...
- C# Message 消息处理
一.消息概述 Windows下应用程序的执行是通过消息驱动的.消息是整个应用程序的工作引擎,我们需要理解掌握我们使用的编程语言是如何封装消息的原理.C#自定义消息通信往往采用事件驱动的方式实现,但有时 ...
- RequireJS 也可以引入 VUE
RequireJS 也可以引入 VUE 由于 FastAdmin 是使用 RequireJS 导入 JS 模块的. 有人想把 VUE 也引入进去,虽然说也是可以,VUE 还是推荐使用 Webpack ...
- 伪元素after,before,css/js控制样式
CSS<style> body { font: 200%/1.45 charter; } ref::before { content: '\00A7'; letter-spacing: . ...
- Zabbix通过SNMPv2监控DELL服务器的硬件信息
(一)zabbix监控DELL服务器 (1)简述:监控DELL服务器硬件一般有两种途径:1.操作系统上安装OMSA,编写脚本调用omreport命令进行监控(需要在操作系统上安装比较麻烦):2.使用i ...
- 在Azure New Portal上创建基于ARM的带SLB的VM
目前Azure的New Portal在国内已经上线了.本文将介绍最常见的一种场景:通过Azure的New Portal创建带有Server Load Balance的多台虚拟机. 1 创建Resour ...