CUGB图论专场2:B - Asteroids 二分图:最小顶点覆盖=最大匹配数
来源:互联网 发布:网店管家云端版mac 编辑:程序博客网 时间:2024/05/17 22:25
B - Asteroids
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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).
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).
题意:就是横纵坐标求出x或者y,即点覆盖边,即是求最小点覆盖。
例如:样例中的取x=1和y=2就可以灭掉所以的星星,就是二分图中左顶点集的1点和右顶点集的2点,就可以把二分图覆盖完了(横坐标x为左顶点集,纵坐标y为右顶点集。
最小点覆盖:假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖图的所有的边。
这题看了学习资料,学习匈牙利算法就懂了。不过还要学习二分图最大匹配的König定理:最小点覆盖数 = 最大匹配数。
所以求最小顶点覆盖等于求最大匹配数,而最大匹配数用匈牙利算法可以求出……
因为要用二分图求,所以得有左顶点集和右顶点集,而从样例中可以知道x和y是互斥的,所以可以设x为左顶点集,y为右顶点集。
邻接表:0ms可以AC。
#include <iostream>#include <cstdio>#include <fstream>#include <algorithm>#include <cmath>#include <deque>#include <vector>#include <list>#include <queue>#include <string>#include <cstring>#include <map>#include <set>#define PI acos(-1.0)#define mem(a,b) memset(a,b,sizeof(a))#define sca(a) scanf("%d",&a)#define pri(a) printf("%d\n",a)#define MM 10002#define MN 505#define INF 168430090using namespace std;typedef long long ll;int n,k,m,pre[MN],vis[MN];//pre为y匹配点x,即匹配的前一结点int cnt,head[MM]; //邻接表,邻接矩阵也可以,不过邻接表空点不用判断,省时struct node{ int x,y,next;} e[MM];int dfs(int x){ for(int y,i=head[x]; i!=-1; i=e[i].next) { y=e[i].y; if(!vis[y]) { vis[y]=1; if(pre[y]==0||dfs(pre[y])) //dfs前向结点,如果为真说明找到增广路 { pre[y]=x; //把匹配的变成未匹配,未匹配的变成匹配,故匹配数+1,因为未匹配数比匹配数大1 return 1; } } } return 0;}void search(){ for(int x=1; x<=n; x++) //从左顶点集开始判断,匈牙利算法思想 { mem(vis,0); m+=dfs(x); }}int main(){ cin>>n>>k; mem(head,-1); for(int x,y,i=0; i<k; i++) { scanf("%d%d",&x,&y); e[cnt].y=y; e[cnt].next=head[x]; head[x]=cnt++; } search(); cout<<m<<endl; return 0;}
邻接矩阵:32ms可以AC,因为空的点也需要判断,导致了不必要的操作,所以时间会慢些。
#include <iostream>#include <cstdio>#include <fstream>#include <algorithm>#include <cmath>#include <deque>#include <vector>#include <list>#include <queue>#include <string>#include <cstring>#include <map>#include <set>#define PI acos(-1.0)#define mem(a,b) memset(a,b,sizeof(a))#define sca(a) scanf("%d",&a)#define pri(a) printf("%d\n",a)#define MM 10002#define MN 505#define INF 168430090using namespace std;typedef long long ll;int n,k,m,pre[MN],vis[MN],Map[MN][MN];int dfs(int x){ for(int y=1;y<=n;y++) { if(!vis[y]&&Map[x][y]) { vis[y]=1; if(pre[y]==0||dfs(pre[y])) //dfs前向结点,如果为真说明找到增广路 { pre[y]=x; //把匹配的变成未匹配,未匹配的变成匹配,故匹配数+1,因为未匹配数比匹配数大1 return 1; } } } return 0;}void search(){ for(int x=1; x<=n; x++) //从左顶点集开始判断,匈牙利算法思想 { mem(vis,0); m+=dfs(x); }}int main(){ cin>>n>>k; for(int x,y,i=0; i<k; i++) { scanf("%d%d",&x,&y); Map[x][y]=1; } search(); cout<<m<<endl; return 0;}
0 0
- CUGB图论专场2:B - Asteroids 二分图:最小顶点覆盖=最大匹配数
- poj3041-Asteroids , 二分图的最小顶点覆盖数 = 最大匹配数
- POJ 3041 Asteroids ( 最小点覆盖数=最大匹配数, 二分图匹配)
- poj 3041 Asteroids (二分图最大匹配 == 最小点覆盖数)
- poj3020 匈牙利算法+公式:二分无向图的最小路径覆盖 = 顶点数 - 最大二分匹配数 / 2
- CUGB图论专场2:A - Antenna Placement 二分图最小边覆盖
- Asteroids POJ3041 二分图最小顶点覆盖
- Asteroids(POJ--3041 【二分图的最小顶点集 = 二分图的最大匹配】
- POJ 3401 Asteroids 二分图最大匹配 最小点覆盖
- POJ 3041 Asteroids(二分图最大匹配-最小点覆盖)
- 二分图:最大独立集&最大匹配&最小顶点覆盖
- Machine Schedule HDoj (二分图最小顶点覆盖--最大匹配数)
- poj3020_最小覆盖路径(顶点个数-二分图最大匹配数)
- POJ3041《Asteroids》方法:匈牙利算法(最小点覆盖数==最大二分匹配数)
- POJ 3020 最小路径覆盖 = 顶点数-最大匹配数 二分匹配
- HDU 4160 最小路径覆盖 = 顶点数 - 最大匹配数 二分匹配
- poj 3041 Asteroids 二分图的最大匹配==最小覆盖点。
- POJ3041 Asteroids 二分图最小顶点覆盖 Dinic求解最大流
- Setting shared (dynamic) library in Eclipse C++ in Linux
- MyEclipse 格式化代码调整每行长度
- 黑马程序员--java中String创建几个对象
- java学习笔记<二>隐藏和封装
- 单片机毕业设计
- CUGB图论专场2:B - Asteroids 二分图:最小顶点覆盖=最大匹配数
- Web服务
- Android 开发之使用Eclipse Debug调试详解
- Java数据库编程---JDBC2.0操作
- Linux Socket编程(不限Linux)
- 个税计算器支持相关请回复本帖
- 指针和引用当参数传递的一个奇怪现象——目前没在书上找到原因
- Your PHP installation appears to be missing the MySQL extension which is required by WordPress.
- js中toFixed() 的用法