标签:att oca navigate font pow hot mit dash blog
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 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
题目大意:
给你一个N*N的矩阵,有一些格子里有小行星,现在Bessie有一些威力很大的炮弹,每一次射击都能够消灭掉矩阵中一行或一列的小行星,
但是炮弹很贵,问你需要消灭掉所有小行星所需的最小炮弹数目。
思路:
这道题和上一道题的思路是一样的,我们只需要将每一行和每一列都当做是二分图的一个集合,然后在进行二分图匹配。
这道题就是一个裸的求最小点覆盖的题。在二分图中,最小点覆盖=最大匹配数。so,着我们就直接求最大匹配数就好了。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 550
using namespace std;
bool vis[N];
int n,m,x,y,ans,pre[N],map[N][N];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘) f=-1; ch=getchar();}
while(ch<=‘9‘&&ch>=‘0‘){x=x*10+ch-‘0‘; ch=getchar();}
return x*f;
}
int find(int x)
{
for(int i=1;i<=n;i++)
if(!vis[i]&&map[x][i])
{
vis[i]=true;
if(!pre[i]||find(pre[i]))
{
pre[i]=x;
return 1;
}
}
return 0;
}
int main()
{
n=read(),m=read();
for(int i=1;i<=m;i++)
x=read(),y=read(),map[x][y]=1;
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(find(i)) ans++;
}
printf("%d",ans);
return 0;
}
poj 3041——Asteroids
标签:att oca navigate font pow hot mit dash blog
原文地址:http://www.cnblogs.com/z360/p/7105069.html