POJ 3041_Asteroids
2016-02-22 16:46
253 查看
题意:
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()); }
相关文章推荐
- httpclient忽略证书验证允许https请求
- 手把手教你写专利申请书/如何申请专利
- 实现C++ 枚举反射机制
- MongoDB 进阶(GridFS)
- *信封问题的错位排列
- sbt自定义spark参数
- js去除重复项
- init/initWithFrame/自定义init方法执行顺序
- 样式修改时的一般修改与影响修改
- windows下php redis扩展安装
- Java利用Zxing生成二维码
- JSTL标签
- setOnLongClickListener中return值
- 使用qt写的简单的图片浏览器
- 编辑部直接征稿,此内容长期有效,请放心投稿
- [IOS 开发] 常用第三方框架和小工具
- 是谁制造了混乱
- Think in Java反刍笔记(2)---对象是什么(2)
- Factorial Trailing Zeroes
- JavaScript 中 Property 和 Attribute 的区别详解