题目大意:$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. 如何直接在网页中显示PDF文件

    通过的浏览器:360.Firefox.IE.Chrome 2. 下面这个完整点: <param name="_Version" value="65539" ...

  2. wiredtiger引擎性能——比levelDB更牛叉!

    WE'VE JOINED MONGODB!   We're proud to announce that MongoDB has acquired WiredTiger, and we've join ...

  3. RabbitMQ学习系列三:.net 环境下 C#代码订阅 RabbitMQ 消息并处理

    上一篇已经讲了Rabbitmq如何在Windows平台安装 不懂请移步: RabbitMQ学习系列二:.net 环境下 C#代码使用 RabbitMQ 消息队列 一.理论 .net环境下,C#代码订阅 ...

  4. MySQL 索引 视图 触发器 存储过程 函数

    1.索引 索引相当于图书的目录,可以帮助用户快速的找到需要的内容. 数据库利用各种各样的快速定位技术,能够大大提高查询效率.特别是当数据量非常大,查询涉及多个表时,使用索引往往能使查询速度加快成千上万 ...

  5. linux下的第一个C程序及其编译方法

    #include <stdio.h> #include <stdlib.h>   int main(int argc, char ** argv) {     printf(& ...

  6. AtCoder Petrozavodsk Contest 001 A - Two Integers

    Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement You are given positive ...

  7. 畅通工程(kruskal算法)

    个人心得:日了狗,WR了俩个小时才发现是少了个vector清理,我也是醉了,不过后面还是对这个有了更好得了解,一是我得算法,而是学长改进 后的算法,改进后得算法还要判断所有村庄是否在连在一起,其实我觉 ...

  8. 【LeetCode】028. Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. HL7 标准及实现指南 必看的网址

    V2: http://hl7book.net/index.php?title=HL7_version_2 包括HL7 的介绍,主要章节,每个版本的变化, 各种语言实现的HL7库,免费HL7工具, 最新 ...

  10. Jmeter & TICK

    背景:   本来只是想在将Jmeter的测试结果写入InfluxDB, 但发现从InfluxDB V1.3后开始, 已经不支持Web Admin interface, 才发现InfluxData 搞了 ...