题目大意:$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(二分图的最小顶点覆盖)的更多相关文章

  1. poj3041 Asteroids 匈牙利算法 最小点集覆盖问题=二分图最大匹配

    /** 题目:poj3041 Asteroids 链接:http://poj.org/problem?id=3041 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一把枪,每一发子弹可 ...

  2. POJ3041 Asteroids(二分图最小点覆盖)

    Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...

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

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

  4. POJ3041 Asteroids 二分图匹配 匈牙利算法

    原文链接http://www.cnblogs.com/zhouzhendong/p/8229200.html 题目传送门 - POJ3041 题意概括 有一个n*n的矩阵,有些点是障碍物. 现在每次可 ...

  5. POJ3041 Asteroids(二分图最大匹配)

    题目链接. 分析: 暂略. AC代码: #include <iostream> #include <cstdio> #include <cstring> #incl ...

  6. poj3041 Asteroids(二分图最小顶点覆盖、二分图匹配)

    Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...

  7. hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. hdu3861 强连通分量缩点+二分图最最小路径覆盖

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. HDU1054(KB10-H 最小顶点覆盖)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. 清新大气的ListView下拉上拉刷新--第三方开源--PullDownListView

    下载地址:https://github.com/guojunyi/PullDownListView 使用: xml: <com.pulldownlistview.PullDownListView ...

  2. Android 关于后台杀死App之后改变服务器状态的一些尝试

    前言: 如题,我的需求是:我需要在App在后台运行(未退出),调出最近运行记录,杀死App服务时,程序能够向服务器发送一条指令,以此达到我想要的目的. Android方面刚刚才开始玩,我一开始想的是可 ...

  3. rpy2的安装问题?【解决】

    https://www.zhihu.com/question/46555829 http://blog.sciencenet.cn/blog-365459-901335.html

  4. 无状态服务 VS 有状态服务

    无状态服务 VS 有状态服务 https://blog.csdn.net/mysee1989/article/details/51381435 对服务器程序来说,究竟是有状态服务,还是无状态服务,其判 ...

  5. [转]express 路由控制--next

    next() express的路由控制有个next()功能,在定义了多个路由的时候,对匹配的url会按顺序执行, 例如,有这样两个路由,第一个路由会对满足“/”的地址,在req中添加一个user的属性 ...

  6. Windows平台下gitbook的安装与使用

    最近要用到gitbook去阅读电子书,整理了一份在windows系统下安装gitbook的过程. * 安装nodejs * 安装cnpm * 搭建gitbook平台 1.在windows上安装node ...

  7. 9.Selenium+HTMLTestRunner无法生成测试报告(Pycharm)

    1.若编辑器为Pycharm,代码无任何错误,且运行成功,但是无法生成测试报告,原因如下:(本质是编辑器原因) 若光标在如图位置,右键显示为“run 'unittest in XX'”,若是显示这种, ...

  8. sourcetree 安装与操作

    sourcetree操作 http://www.jianshu.com/p/be9f0484af9d SourceTree安装教程和GitLab配置详解 http://www.cnblogs.com/ ...

  9. 免费的xshell下载

    平时从网站上下载的xshell,都是有有效期的,用一个月就生效了 所以我们可以这样: 学习源头: https://blog.csdn.net/zhoukikoo/article/details/794 ...

  10. laravel 导出导入excel和csv文件的 使用

    在项目中用到的常用功能数据导入导出 在laravel有插件可以直接使用 方便快捷 学习源头: https://www.cnblogs.com/martianShu/p/5869270.html htt ...