POJ 3041 Asteroids(最小点覆盖)
来源:互联网 发布:opengl编程指南 第9版 编辑:程序博客网 时间:2024/06/05 18:20
题目链接:
POJ 3041 Asteroids
题意:
给出m个点的所在行列,每次可以选择清除一行或者一列,问清除这些点的最小操作次数是多少?
分析:
将所有点的行看成一组,所有的点列看成一组,对每个点的行和列建一条边,每条边就代表一个点,清除一行或者一列可以看成选择二分图中的一个点,清除所有的点,就相当于选择二分图中的若干点连接所有的边。
这样一来就符合最小点覆盖的定义了!再根据最小点覆盖数=最大匹配数,利用匈牙利算法即可。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <climits>#include <cmath>#include <ctime>#include <cassert>#include <map>#define IOS ios_base::sync_with_stdio(0); cin.tie(0);using namespace std;typedef long long ll;const int MAX_N = 510;int n, m, total, cntx, cnty;int head[MAX_N], vis[MAX_N], match[MAX_N];struct Edge{ int to, next; Edge() {} Edge(int _to,int _next) : to(_to), next(_next) { }}edge[MAX_N * MAX_N];inline void AddEdge(int from, int to){ edge[total].to = to; edge[total].next = head[from]; head[from] = total++;}inline bool dfs(int u){ for(int i = head[u]; i != -1; i = edge[i].next){ int to = edge[i].to; if(!vis[to]){ vis[to] = 1; if(match[to] == -1 || dfs(match[to])){ match[to] = u; return true; } } } return false;}inline void hungary(){ int ans = 0; for(int i = 0; i < cntx; ++ i){ memset(vis,0,sizeof(vis)); if(dfs(i)) ans++; } cout << ans << endl;}int main(){ freopen("E.in", "r", stdin); IOS; while(cin >> n >> m){ memset(head, -1, sizeof(head)); memset(match, -1, sizeof(match)); cntx = cnty = total = 0; map<int, int> mpx; map<int, int> mpy; for(int i = 0; i < m; ++ i){ int x, y; cin >> x >> y; if(mpx.find(x) == mpx.end()){ mpx[x] = cntx++; } if(mpy.find(y) == mpy.end()){ mpy[y] = cnty++; } AddEdge(mpx[x], mpy[y]); } hungary(); } return 0;}
0 0
- POJ 3041 Asteroids(最小点覆盖)
- POJ 3041 Asteroids(最小点覆盖)
- POJ 3041 Asteroids (最小点覆盖)
- POJ 3041 Asteroids(最小点覆盖)
- POJ 3041 Asteroids(最小点覆盖)
- POj 3041 Asteroids 最小点覆盖
- POJ 3041:Asteroids:最小点覆盖
- poj 3041 Asteroids (最小点覆盖)
- POJ 3041 Asteroids(最小点覆盖)
- POJ 3041 Asteroids 最小点覆盖
- Poj 3041 Asteroids【最小点覆盖】
- poj 3041 Asteroids (最小点覆盖)
- POJ 3041 Asteroids 最小点覆盖
- poj 题目3041 Asteroids (最小点覆盖)
- POJ 3041 Asteroids (图论-最小点覆盖)
- 【POJ】3041 - Asteroids(最小点覆盖,好题)
- poj 3041 Asteroids 点覆盖(匹配)
- POJ 3041 Asteroids 匈牙利算法/最小点覆盖
- leetcode67:Add Binary
- MyEclipse中开发web项目遇到的一些问题
- LeetCode-58.Length of Last Word
- 关于回调函数的一些理解
- Maven在IntelliJ IDEA中的学习笔记(2)-----配置maven私服
- POJ 3041 Asteroids(最小点覆盖)
- MFC tab控件使用
- WebSocket不同版本的三种握手方式以及一个Netty实现JAVA类
- STM32+freertos+lwip
- vim的配置(自动换行、显示行号和Tab的宽度)
- C语言实现修改文本文件中特定行的实现代码(百度一天没找到,bing搜索十分钟就找到了)
- leetcode 075 Sort Colors
- IdentityHashMap 与HashMap的对比
- C# 获取天气 JSON解析