bzoj1741/Usaco2005 novAsteroids 穿越小行星群——最小割

Posted Child-Single

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj1741/Usaco2005 novAsteroids 穿越小行星群——最小割相关的知识,希望对你有一定的参考价值。

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.

贝茜想驾驶她的飞船穿过危险的小行星群.小行星群是一个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 4
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

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

对于每个点,从它的行向它的列连一条inf的边,从源点向每行及每列向汇点连一条权值为1的边,然后跑一次dinic求最小割就好了。
代码:
技术分享
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define mem(a,p) memset(a,p,sizeof(a))
 5 const int N=2205,inf=0x3f3f3f3f;
 6 struct node{int ne,to,w;}e[600000];
 7 int first[N],cur[N],n,k,tot=1,t,d[N],q[N];
 8 using std::min;
 9 int read(){
10     int ans=0,f=1;char c=getchar();
11     while(c<0||c>9){if(c==-)f=-1;c=getchar();}
12     while(c>=0&&c<=9){ans=ans*10+c-48;c=getchar();}
13     return ans*f;
14 }
15 void ins(int u,int v,int w){
16     e[++tot]=(node){first[u],v,w};first[u]=tot;
17 }
18 void insert(int u,int v,int w){ins(u,v,w);ins(v,u,0);}
19 bool bfs(){
20     mem(d,-1);
21     int h=0,tail=1;q[0]=0;d[0]=1;
22     while(h!=tail){
23         int x=q[h];h++;if(h>=1200)h=0;
24         for(int i=first[x];i;i=e[i].ne){
25             int to=e[i].to;
26             if(d[to]==-1&&e[i].w>0){
27                 d[to]=d[x]+1;
28                 q[tail]=to;tail++;if(tail>=1200)tail=0;
29             }
30         }
31     }
32     return d[t]!=-1;
33 }
34 int dfs(int x,int a){
35     if(x==t||a==0)return a;
36     int flow=0,f;
37     for(int&i=cur[x];i;i=e[i].ne){
38         int to=e[i].to;
39         if(d[to]==d[x]+1&&(f=dfs(to,min(a,e[i].w)))>0){
40             e[i].w-=f;
41             e[i^1].w+=f;
42             a-=f;
43             flow+=f;
44             if(!a)break;
45         }
46     }
47     return flow;
48 }
49 int main(){
50     n=read();k=read();t=n*2+1;
51     for(int i=1;i<=n;i++)insert(0,i,1),insert(n+i,t,1);
52     for(int i=1,a,b;i<=k;i++){
53         a=read();b=read();
54         insert(a,n+b,inf);
55     }
56     int ans=0;
57     while(bfs()){
58         for(int i=0;i<=t;i++)cur[i]=first[i];
59         ans+=dfs(0,inf);
60     }
61     printf("%d\n",ans);
62     return 0;
63 }
bzoj1741

 

以上是关于bzoj1741/Usaco2005 novAsteroids 穿越小行星群——最小割的主要内容,如果未能解决你的问题,请参考以下文章

bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群最大点覆盖

BZOJ 1083: [SCOI2005]繁忙的都市

BZOJ 1968: [Ahoi2005]COMMON 约数研究

bzoj1415NOI2005聪聪和可可

bzoj 2005

bzoj 1415: [Noi2005]聪聪和可可