题目大意:$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. 用createinstallmedia创建可恢复的OSX安装DMG

    准备 从App Store下载OS X安装程序,下载完成,会在应用程序目录 /Applications 下找到类似 Install OS X 10.xxxxxx.app(中文名如:安装 OS X 10 ...

  2. cassandra cqlsh 和 python客户端

    Keyspaces A cluster is a container for keyspaces. A keyspace is the outermost container for data in ...

  3. 项目管理理论与实践(4)——UML应用(上)

    本篇文章介绍UML的相关知识.参考<UML从入门到精通> 一.UML综述 1. UML简介 统一建模语言(UML)是一个通用的可视化建模语言,用于对软件进行描述.可视化处理.构造和建立软件 ...

  4. android 关于Toast重复显示解决方法

    解决思路:   不用计算Toast的时间之类的,就是定义一个全局的成员变量Toast, 这个Toast不为null的时候才去make,否则直接setText.为了按返回键后立即使Toast不再显示,重 ...

  5. LeetCode OJ:Count and Say(数数)

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  6. spring MVC HandlerInterceptorAdapter

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  7. 关于/usr/bin/ld: cannot find -lcrypto 的错误

    Linux下 build code 时,要做 -lssl, -lcrypto 的链接,出现类似下面的错误: /usr/bin/ld: cannot find -lcrypto /usr/bin/ld: ...

  8. SQL夯实基础(四):子查询及sql优化案例

    首先我们先明确一下sql语句的执行顺序,如下有前至后执行: (1)from  (2) on   (3) join  (4) where  (5)group by  (6) avg,sum...  (7 ...

  9. [独孤九剑]Oracle知识点梳理(四)SQL语句之DML和DDL

    本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...

  10. ubuntu更改用户登录密码

    sudo passwd user(root或对应的用户名)