Asteroids - poj 3041(二分图最大匹配问题)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17258   Accepted: 9386

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.

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.

Output

* Line 1: The integer representing the minimum number of times Bessie must shoot.

Sample Input

3 4
1 1
1 3
2 2
3 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).

这题利用的是二分图的最大匹配问题,将每一行,每一列看做一个点,将所有行构成的点组成一个集合,而所有列构成的的点看成一个集合,将输入的数据看成是连接两个集合的边,这样就是一个二分图了。

 1 #include <iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 using namespace std;
 5 int map[500][500];
 6 int vis[500],match[500];
 7 int m,n;
 8 int DFS(int p){
 9     int v;
10     for(int i=1;i<=m;i++){
11         if(map[p][i]&&!vis[i]){
12             vis[i]=1;
13             v=match[i];
14             if( v==-1 || DFS(v)){
15                 match[i]=p;
16                 return 1;
17             }
18
19         }
20     }
21     return 0;
22 }
23 int main() {
24     while(~scanf("%d %d",&m,&n)){
25         memset(map,0,500*500*sizeof(int));
26         for(int i=0;i<n;i++){
27             int a,b;
28             scanf("%d %d",&a,&b);
29             map[a][b]=1;
30         }
31         int result=0;
32         memset(match,-1,500*sizeof(int));
33         for(int i=1;i<=m;i++){
34             memset(vis,0,500*sizeof(int));
35             result+=DFS(i);
36         }
37         printf("%d\n",result);
38     }
39     return 0;
40 }
时间: 2025-01-02 22:11:17

Asteroids - poj 3041(二分图最大匹配问题)的相关文章

Asteroids POJ - 3041 二分图最小点覆盖

Asteroids POJ - 3041 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 point

poj 3041 二分图最大匹配

题意:给定一个NxN的网格,其中有k个格点上有障碍物,每次可以消除一行或一列障碍物,问最少几次可以消除全部的障碍物. 思路:二分图的经典模型,将所有的行看作二分图中左边的端点,将所有的列看作右边的端点,当格点上有障碍物时,连一条边,问题转变为求二分图的最小顶点覆盖,根据König定理由二分图的最大匹配得到 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm

POJ 2226二分图最大匹配

匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是二部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法. #include<stdio.h> #include<string.h> #include<stdlib.h> int n1,n2; char map[1005][1005]; //数组开大点 int mapx[1005][1005],mapy[1005]

POJ 1469 二分图最大匹配

思路:裸的二分图最大匹配,居然没看出来,我还是要学习一个 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<cstdlib> 7 #include<sstream> 8 #include<iomanip> 9 using namespace

poj 3692 二分图最大匹配

题意: 已知班级有g个女孩和b个男孩,所有女生之间都相互认识,所有男生之间也相互认识,给出m对关系表示哪个女孩与哪个男孩认识.现在要选择一些学生来组成一个团,使得里面所有人都互相认识,求此团最大人数. 限制: 1 <= g,b <= 200; 0 <= m <= b*g 思路: 求最大团. 最大独立集=|V|-最大匹配 最大团=补图的最大独立集 由题意可得,互相不认识的连边,构成一个二分图,ans=|V|-最大匹配,匈牙利算法. /*poj 3692 题意: 已知班级有g个女孩和b

Asteroids POJ - 3041 【最小点覆盖集】

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. Fortun

POJ 3041 -- 二分图匹配

题意:有个N*N的网格,有一部分格子里有陨石,小明有很牛逼的武器,打一枪后一行或一列的陨石就没了,给出陨石分布,求最小打炮数. 分析:其实就是Konig定理.记最小打炮数为m,在网格里你最多可以找出M个互相之间既不在同一行又不在同一列的元素,也就是说你再找一个元素它必定与已有的某个元素同行或同列,Konig定理就是:m = M.至于具体打时是打某一行还是某一列,要看剩下的元素与现有元素共行或共列的情况了. 那么问题就转化成找出这M个陨石,把行和列看做一个二分图,那么找M就是找最大匹配了. 和PO

poj 3041 二分图最小点集覆盖

定理:二分图的最大匹配=最小点覆盖. 思路:将所有行看做点集X,所有列看做点集Y,如果在[i, j]处有小行星,则建立一条从i到j的边,然后求最大匹配即为最小点覆盖数即为答案. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 501; 7 const int M = N * N; 8 int head[N]; 9

poj 2446 二分图最大匹配

题意:给一张m*n的棋盘,上面有k个格子有洞,用1*2的骨牌将棋盘覆盖,每个格子必须被恰好覆盖一次,有洞的地方不能覆盖,问是否存在这样的方案. 思路:容易想到将格点作为二分图中的点,骨牌作为二分图中的边(骨牌可能放置时),求二分图的最大匹配,如果匹配数与n*m-k相等,则方案存在,否则不存在:容易想到当n*m-k为奇数时方案一定不存在: 构思时的难点在于将格点用一维坐标表示出来,后来发现是自己傻逼了.....标号就可以解决问题. 坑点:读入洞的坐标时列号在前,行号在后. 不算坑点的坑点:题目有多