题意:

N*N网格中有小行星,光束能将一整行或者一整列的行星消灭,问消灭所有行星至少需要多少光束?

分析:

最小顶点覆盖问题,将每个小行星看成边,左右顶点为横纵坐标,可以转化为二分图,利用二分图中最小顶点覆盖等于最大二分匹配的性质,求出最大二分匹配(匈牙利算法)即可。

代码:

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxn = 10005;
int x[maxn], y[maxn];
int used[maxn], match[maxn];
int N, K;
vector<int>G[maxn];
void add_edge(int u, int v)
{
G[v].push_back(u);
G[u].push_back(v);
}
int dfs(int v)
{
used[v] = 1;
for(int i = 0; i < G[v].size(); i++){
int u = G[v][i], w = match[u];
if(w<0||!used[w]&&dfs(w)){
match[v] = u;
match[u] = v;
return 1;
}
}
return 0;
}
int bipartite_matching()
{
int res = 0;
memset(match, -1, sizeof(match));
for(int i = 1; i <= 2 * N; i++){
if(match[i]<0){
memset(used, 0, sizeof(used));
if(dfs(i))
res++;
}
}
return res;
}
int main (void)
{
scanf("%d%d",&N,&K);
memset(match, -1, sizeof(match));
for(int i = 0; i < K; i++){
scanf("%d%d",&x[i],&y[i]);
add_edge(x[i], y[i] + N);
}
printf("%d\n", bipartite_matching());
}

POJ 3041_Asteroids的更多相关文章

  1. POJ训练计划3041_Asteroids(二分图/最小点覆盖=最大匹配)

    解题报告 http://blog.csdn.net/juncoder/article/details/38135053 题目传送门 题意: 给出NxN的矩阵,有M个点是障碍 每次仅仅能删除一行或者一列 ...

  2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  3. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  4. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  9. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

随机推荐

  1. 快速开发框架天梭(Tissot)

    天梭(Tissot)集成SpringBoot+Dubbo等主流互联网技术栈,高度集成.优化方便快速搭建应用.某互金科技公司内部孵化框架,已应用于公司90%业务系统. 框架划分模块有: tissot-c ...

  2. elasticsearch学习笔记-倒排索引以及中文分词

    我们使用数据库的时候,如果查询条件太复杂,则会涉及到很多问题 1.无法维护,各种嵌套查询,各种复杂的查询,想要优化都无从下手 2.效率低下,一般语句复杂了之后,比如使用or,like %,,%查询之后 ...

  3. fedora安装gcc

    查看gcc版本 gcc --version 命令行编译 g++ -std=c++11 -o main main.cpp 查看程序是否编译成功 echo $? 返回0表示编译成功 新版的Fedora(2 ...

  4. CSS3 四边形 凹角写法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. shell高级用法

    参考链接: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=218853&page=7#pid1628522

  6. 使用VS Code调试Flutter(检查用户页面)

    官方提供的是Flutter Widget Inspector,详见https://flutterchina.club/inspector/ 我用的是另外一种好用的调试工具 Dart DevTools ...

  7. 常用的HTTP方法有哪些?

    GET: 用于请求访问已经被URI(统一资源标识符)识别的资源,可以通过URL传参给服务器POST:用于传输数据给服务器,主要功能与GET方法类似,但一般推荐使用POST方式.PUT: 传输数据,报文 ...

  8. CAD使用DeleteXData删除数据(com接口)

    主要用到函数说明: MxDrawEntity::DeleteXData 删除扩展数据,详细说明如下: 参数 说明 pzsAppName 删除的扩展数据名称,如果为空,删除所有扩展数据 c#代码实现如下 ...

  9. svn 版本库信息修改

    root@hpcstack hpcweb]# svn info 路径: . URL: http://svn.pyindex.com/hpcweb 版本库根: http://svn.pyindex.co ...

  10. kafka可视化客户端工具(Kafka Tool)安装

    参考:https://www.cnblogs.com/frankdeng/p/9452982.html