【二分图】poj 3041 Asteroids
来源:互联网 发布:软件测试的原理 编辑:程序博客网 时间:2024/06/04 18:50
Asteroids
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 22897
Accepted: 12414
Memory Limit: 65536KTotal Submissions: 22897
Accepted: 12414
Description
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid.
Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
Input
* Line 1: Two integers N and K, separated by a single space.
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
Output
* Line 1: The integer representing the minimum number of times Bessie must shoot.
Sample Input
3 41 11 32 23 2
Sample Output
2
Hint
INPUT DETAILS:
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).
///AC代码
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).
思路:
将每行、每列分别看作一个点,对于case的每一个行星坐标(x,y),将第x行和第y列连接起来,例如对于输入:
(1,1)、(1,3)、(2,2)、(3,2)4点构造图G:
这样,每个点就相当于图G的一条边,消灭所有点=消灭图G的所有边,又要求代价最少,即找到图G上的最少的点使得这些点覆盖了所有边。
根据定理吗, 最小点覆盖数=最大匹配数,所以本题转化为二分图的最大匹配问题——用匈牙利算法来解决。
///AC代码
#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>#include <ctime>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 100005#define PI acos(-1.0)using namespace std;typedef long long LL;const int N = 302;/*变种1:二分图的最小顶点覆盖:假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖所有的边二分图的最小顶点覆盖数 = 二分图的最大匹配数变种2:DAG图(无回路有向图)的最小路径覆盖路径覆盖就是在图中找一些路经,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联,如果把这些路径中的每条路径从它的起始点走到它的终点,那么恰好可以经过图中的每个顶点一次且仅一次DAG图的最小路径覆盖数 = 节点数(n)- 最大匹配数(m)变种3: 二分图的最大独立集:在图中选取最多的点,使任意所选两点均不相连二分图的最大独立集数 = 节点数(n)- 最大匹配数(m)*//*=***************************************************二分图匹配(匈牙利算法的DFS实现)INIT:g[][]两边定点划分的情况CALL:res=hungary();输出最大匹配数优点:适于稠密图,DFS找增广路快,实现简洁易于理解时间复杂度:O(VE);***************************************************=*/const int MAXN = 1000;int uN, vN; //u,v数目int g[MAXN][MAXN];//编号是0~n-1的int linker[MAXN];bool used[MAXN];bool dfs(int u){ int v; for (v = 1; v <= vN; v++) if (g[u][v] && !used[v]) { used[v] = true; if (linker[v] == -1 || dfs(linker[v])) { linker[v] = u; return true; } } return false;}int hungary(){ int res = 0; int u; memset(linker, -1, sizeof(linker)); for (u = 1; u <= uN; u++) { memset(used, 0, sizeof(used)); if (dfs(u)) { res++; } } return res;}int main(){ memset(g, 0, sizeof(g)); int n, k; scanf("%d%d", &n, &k); uN = n; vN = n; int x, y; for (int i = 0; i < k; i++) { scanf("%d%d", &x, &y); g[x][y] = 1; } int ans = hungary(); cout << ans << endl; return 0;}
阅读全文
0 0
- poj 3041 Asteroids 二分图
- 【POJ 3041 Asteroids】+ 二分图
- 【二分图】poj 3041 Asteroids
- Asteroids(poj 3041,二分图最大匹配)
- 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(二分图匹配)
- 图像的遍历
- Java服务端过滤敏感词的思路
- Glusterfs学习笔记
- POJ2186-Popular Cows
- leetcode -- 67. Add Binary【统一形式简化代码+双指针 + 二进制规则 + 字符数字转换】
- 【二分图】poj 3041 Asteroids
- Unity3D学习记录——飞盘射击游戏2
- java-代码块、静态代码块、构造代码块
- CSS label textarea 垂直对齐
- Vue.js:轻量高效的前端组件化方案
- poj 1183 反正切函数的应用
- 关注民生民情——华北水利水电大学“情艺”国情社情调查
- USACO1.3.4牛式
- Ubuntu下Tensorflow cpu版本安装介绍