1741: [Usaco2005 nov]Asteroids 穿越小行星群
Time Limit:5 Sec Memory Limit:64 MBSubmit: 399 Solved: 286
[Submit][Status][Discuss]
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
1 1
1 3
2 2
3 2
INPUT DETAILS:
The following diagram represents the data, where "X" is an
asteroid and "." is empty space:
X.X
.X.
.X.
Sample Output
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).
由于对于一个点的消除,非横即纵,我们发现它是一个二分图
所以,我们就可以二分图匹配啦,匈牙利算法
#include<cmath>
#include<cstdio>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<set>
using namespace std;
inline 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<<3)+(x<<1)+ch-'0';ch=getchar();}
return f*x;
}
const int N=510;
int n,m,ecnt=0,last[N],match[N],ans=0;
bool book[N];
struct EDGE{int to,nt;}e[N<<5];
inline void add(int u,int v)
{e[++ecnt]=(EDGE){v,last[u]};last[u]=ecnt;}
bool hungray(int u)
{
for(int i=last[u];i;i=e[i].nt)
{
if(!book[e[i].to])
{
book[e[i].to]=1;
if(!match[e[i].to]||hungray(match[e[i].to]))
{
match[e[i].to]=u;
return true;
}
}
}
return false;
}
int main()
{
n=read();m=read();int x,y;
for(int i=1;i<=m;i++)
x=read(),y=read(),add(x,y);
for(int i=1;i<=n;i++)
{
memset(book,0,sizeof(book));
if(hungray(i))ans++;
}
printf("%d\n",ans);
return 0;
}
ps:
2017.5.16 增加时间戳优化版,快到不知道哪里去了
#include<cmath>
#include<cstdio>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<set>
using namespace std;
inline 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<<3)+(x<<1)+ch-'0';ch=getchar();}
return f*x;
}
const int N=510;
int n,m,ecnt=0,last[N],match[N],ans=0,ts,book[N];
struct EDGE{int to,nt;}e[N<<5];
inline void add(int u,int v)
{e[++ecnt]=(EDGE){v,last[u]};last[u]=ecnt;}
bool hungray(int u)
{
for(int i=last[u];i;i=e[i].nt)
{
if(ts!=book[e[i].to])
{
book[e[i].to]=ts;
if(!match[e[i].to]||hungray(match[e[i].to]))
{
match[e[i].to]=u;
return true;
}
}
}
return false;
}
int main()
{
n=read();m=read();int x,y;
for(int i=1;i<=m;i++)
x=read(),y=read(),add(x,y);
for(int i=1;i<=n;i++)
{
ts++;
if(hungray(i))ans++;
}
printf("%d\n",ans);
return 0;
}