poj 3041 Asteroids 题解


Asteroids

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20686   Accepted: 11239

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

Source

USACO 2005 November Gold

[Submit]   [Go Back]   [Status]   [Discuss]

——————————————————-——————————我是分割线——————————————————————————————————————

二分图最大匹配。

一个小行星,要么清理该行,要么该列。

所以也就是每个小行星对应的行列中至少选择一样来清理。

下面建图,如果我们把每行看成集合一中的点,每列看成集合二中的点,一个小行星看成是其对应行列的连线,那么也就是说不能存在某一条连线两边的点都没有被选中的情况。

这恰好就是二分图最小点集覆盖的要求。

再利用二分图最大匹配的König定理:

  最小点覆盖数 = 最大匹配数

因此本题自然转化为求 二分图的最大匹配问题

 1 /*
 2     Problem:
 3     OJ:
 4     User:
 5     Time:
 6     Memory:
 7     Length:
 8 */
 9 #include<iostream>
10 #include<cstdio>
11 #include<cstring>
12 #include<cmath>
13 #include<algorithm>
14 #include<queue>
15 #include<cstdlib>
16 #include<iomanip>
17 #include<cassert>
18 #include<climits>
19 #include<vector>
20 #include<list>
21 #include<map>
22 #define maxn 501
23 #define F(i,j,k) for(int i=j;i<=k;i++)
24 #define M(a,b) memset(a,b,sizeof(a))
25 #define FF(i,j,k) for(int i=j;i>=k;i--)
26 #define inf 0x7fffffff
27 #define maxm 2016
28 #define mod 1000000007
29 //#define LOCAL
30 using namespace std;
31 int read(){
32     int x=0,f=1;char ch=getchar();
33     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
34     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
35     return x*f;
36 }
37 int n,m,k;
38 int g[maxn][maxn];
39 int px[maxn],py[maxn];
40 bool vis[maxn];
41 inline int path(int u)
42 {
43     F(i,1,n){
44         if(g[u][i]&&!vis[i])
45         {
46             vis[i]=true;
47             if(py[i]==-1||path(py[i]))
48             {
49                 px[u]=i;
50                 py[i]=u;
51                 return 1;
52             }
53         }
54     }
55     return 0;
56 }
57 inline int solve()
58 {
59     int cnt=0;
60     M(px,-1);M(py,-1);
61     F(i,1,n){
62         if(px[i]==-1){
63             M(vis,false);
64             cnt+=path(i);
65         }
66     }
67     return cnt;
68 }
69 int main()
70 {
71     std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
72     #ifdef LOCAL
73     freopen("data.in","r",stdin);
74     freopen("data.out","w",stdout);
75     #endif
76     cin>>n>>k;
77     F(i,1,k){
78         int x,y;
79         cin>>x>>y;
80         g[x][y]=1;
81     }
82     cout<<solve()<<endl;
83     return 0;
84 }

时间: 09-27

poj 3041 Asteroids 题解的相关文章

POJ 3041 Asteroids (匈牙利算法)

Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14388 Accepted: 7828 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 astero

poj 3041——Asteroids

poj       3041——Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22604   Accepted: 12247 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 g

二分图匹配(匈牙利算法) POJ 3041 Asteroids

题目传送门 1 /* 2 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 3 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 4 趣味入门:http://blog.csdn.net/dark_scope/article/details/8880547 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include

POJ 3041 Asteroids (图论-最小点覆盖)

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15079   Accepted: 8240 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 as

POJ 3041 Asteroids 二分图

原题连接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17985   Accepted: 9798 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <

poj 3041 Asteroids(最小点覆盖)

http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17543   Accepted: 9548 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <

POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))

题目链接: http://poj.org/problem?id=3041 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

POJ 3041.Asteroids 最小顶点覆盖

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22905   Accepted: 12421 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 a

POJ 3041 Asteroids(二分匹配模板题)

题目链接:http://poj.org/problem?id=3041 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 l