# BZOJ1693Asteroids

1693: [Usaco2007 Demo]Asteroids
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 219 Solved: 163
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
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).
Source
Gold

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
struct node
{
int to,next,v;
};
node edge;
int n,m,h,head,tail,dis,q,ans,sum=0,cnt=1;
bool used;

int read()
{
int w=0,c=1;
char ch=getchar();
while (ch<'0' || ch>'9')
{
if (ch=='-')
c=-1;
ch=getchar();
}
while (ch>='0' && ch<='9')
{
w=w*10+ch-'0';
ch=getchar();
}
return w*c;
}

void add(int u,int v,int w)
{
cnt++;
edge[cnt].next=h[u];
h[u]=cnt;
edge[cnt].to=v;
edge[cnt].v=w;
}

bool bfs()
{
int j,p;
memset(dis,-1,sizeof(dis));
q=0;
dis=0;
head=0;
tail=1;
while (head<tail)
{
head++;
j=q[head];
p=h[j];
while (p)
{
if (dis[edge[p].to]<0 && edge[p].v>0)
{
dis[edge[p].to]=dis[j]+1;
tail++;
q[tail]=edge[p].to;
}
p=edge[p].next;
}
}
if (dis[2*n+1]>0)
return true;
else
return false;
}

int dfs(int x,int f)
{
int w,used=0,i=h[x];
if (x==2*n+1)
return f;
while (i)
{
if (edge[i].v && dis[edge[i].to]==dis[x]+1)
{
w=f-used;
w=dfs(edge[i].to,min(w,edge[i].v));
edge[i].v-=w;
edge[i^1].v+=w;
used+=w;
if (used==f)
return f;
}
i=edge[i].next;
}
if (!used)
dis[x]=-1;
return used;
}

int main()
{
int i,x,y;
n=read();
m=read();
for (i=1;i<=m;i++)
{
x=read();
y=read();
add(x,y+n,1);//连边的时候把行与列分离开
add(y+n,x,0);
}
for (i=1;i<=n;i++)
{
add(0,i,1);
add(i,0,0);
add(i+n,2*n+1,1);
add(2*n+1,i+n,0);
}
ans=0;
while (bfs())
while (sum=dfs(0,0x7fffffff))
ans+=sum;
printf("%d",ans);
return 0;
}

08-02 18                                                                          02-18 46
01-08 283
09-16 255
08-19 1190
07-13 178
12-19 387
09-13 26
09-05 24
07-08 52
10-20 1068
08-18 228
04-02 539
11-24 504
07-28 32
06-22 9677
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 DCrusher

¥2 ¥4 ¥6 ¥10 ¥20  余额支付 (余额：-- )  扫码支付 获取中  扫码支付