BZOJ 1741: [Usaco2005 nov]Asteroids 穿越小行星群
2017-04-02 10:55
441 查看
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 locatedat 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.
贝茜想驾驶她的飞船穿过危险的小行星群.小行星群是一个NxN的网格(1≤N≤500),在网格内有K个小行星(1≤K≤10000). 幸运地是贝茜有一个很强大的武器,一次可以消除所有在一行或一列中的小行星,这种武器很贵,所以她希望尽量地少用.给出所有的小行星的位置,算出贝茜最少需要多少次射击就能消除所有的小行星.
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.
第1行:两个整数N和K,用一个空格隔开.
第2行至K+1行:每一行有两个空格隔开的整数R,C(1≤R,C≤N),分别表示小行星所在的行和列.
Output
* Line 1: The integer representing the minimum number of times Bessie must shoot.一个整数表示贝茜需要的最少射击次数,可以消除所有的小行星
Sample Input
3 41 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
2OUTPUT 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 ,match ,ans=0; bool book ; 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 ,match ,ans=0,ts,book ; 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; }
相关文章推荐
- BZOJ 1741: [Usaco2005 nov]Asteroids 穿越小行星群
- bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群
- 【BZOJ】1741: [Usaco2005 nov]Asteroids 穿越小行星群
- BZOJ 1741 [Usaco2005 nov]Asteroids 穿越小行星群题解
- bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群 网络流
- BZOJ 1741 [Usaco2005 nov]Asteroids 穿越小行星群 二分图最小覆盖
- bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群
- BZOJ 1741 [Usaco2005 nov]Asteroids 穿越小行星群 二分图最小边覆盖
- bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群 网络流
- bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群
- bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群【最大点覆盖】
- 1741: [Usaco2005 nov]Asteroids 穿越小行星群
- BZOJ1741: [Usaco2005 nov]Asteroids 穿越小行星群
- 【bzoj1741/Usaco2005 nov】Asteroids 穿越小行星群——最小割
- [Usaco2005 nov]Asteroids 穿越小行星群
- USACO 2005 Nov Asteroids 小行星群
- BZOJ 1742 Usaco2005 nov Grazing on the Run 边跑边吃草 动态规划
- 【bzoj1742】[Usaco2005 nov]Grazing on the Run 边跑边吃草 区间dp
- [bzoj1742][Usaco2005 nov][DP]Grazing on the Run 边跑边吃草
- BZOJ 3384: [Usaco2004 Nov]Apple Catching 接苹果/BZOJ 1750: [Usaco2005 qua]Apple Catching dp