# 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[250001];
bool used[250001];

{
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;
}

{
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]=0;
dis[0]=0;
tail=1;
{
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;
for (i=1;i<=m;i++)
{
}
for (i=1;i<=n;i++)
{
}
ans=0;
while (bfs())
while (sum=dfs(0,0x7fffffff))
ans+=sum;
printf("%d",ans);
return 0;
}
• 1
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

08-02 18

08-19 1190
09-13 26
09-05 24
07-28 32

DCrusher

¥2 ¥4 ¥6 ¥10 ¥20