Gopher II
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6087 | Accepted: 2520 |
Description
The gopher family, having averted the canine threat, must face a new predator.
The are n gophers and m gopher holes, each at distinct (x, y) coordinates. A hawk arrives and if a gopher does not reach a hole in s seconds it is vulnerable to being eaten. A hole can save at most one gopher. All the gophers run at the same velocity v. The gopher family needs an escape strategy that minimizes the number of vulnerable gophers.
The are n gophers and m gopher holes, each at distinct (x, y) coordinates. A hawk arrives and if a gopher does not reach a hole in s seconds it is vulnerable to being eaten. A hole can save at most one gopher. All the gophers run at the same velocity v. The gopher family needs an escape strategy that minimizes the number of vulnerable gophers.
Input
The input contains several cases. The first line of each case contains four positive integers less than 100: n, m, s, and v. The next n lines give the coordinates of the gophers; the following m lines give the coordinates of the gopher holes. All distances are in metres; all times are in seconds; all velocities are in metres per second.
Output
Output consists of a single line for each case, giving the number of vulnerable gophers.
Sample Input
2 2 5 10 1.0 1.0 2.0 2.0 100.0 100.0 20.0 20.0
Sample Output
1
Source
简单的最大流,当然用二分匹配会更简单,最大流建图只是在二分匹配的基础上加了一个源点和汇点而已。简单的最大流,当然用二分匹配会更简单,最大流建图只是在二分匹配的基础上加了一个源点和汇点而已。
计算出s秒之后,老鼠能够移动的距离,然后判断这m个洞与老鼠的距离是否小于等于这段距离,如果是,则连边,流量为1.计算出s秒之后,老鼠能够移动的距离,然后判断这m个洞与老鼠的距离是否小于等于这段距离,如果是,则连边,流量为1.
然后源点与每只老鼠相连,汇点与每个洞相连。最后用n减去求出的最大流就是答案。
//472K 32MS
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#define inf 9999999
#define M 1007
#define MIN(a,b) a>b?b:a;
using namespace std;
struct E
{
int v,w,next;
}edg[500000];
struct sa
{
double x,y;
}p1[1007],p2[1007];
int dis[2000],gap[2000],head[2000],nodes;
int sourse,sink,nn;
double dist;
bool judge(int i,int j)
{
return sqrt((p2[i].x-p1[j].x)*(p2[i].x-p1[j].x)+(p2[i].y-p1[j].y)*(p2[i].y-p1[j].y))<=dist;
}
void addedge(int u,int v,int w)
{
edg[nodes].v=v;
edg[nodes].w=w;
edg[nodes].next=head[u];
head[u]=nodes++;
edg[nodes].v=u;
edg[nodes].w=0;
edg[nodes].next=head[v];
head[v]=nodes++;
}
int dfs(int src,int aug)
{
if(src==sink)return aug;
int left=aug,mindis=nn;
for(int j=head[src];j!=-1;j=edg[j].next)
{
int v=edg[j].v;
if(edg[j].w)
{
if(dis[v]+1==dis[src])
{
int minn=MIN(left,edg[j].w);
minn=dfs(v,minn);
edg[j].w-=minn;
edg[j^1].w+=minn;
left-=minn;
if(dis[sourse]>=nn)return aug-left;
if(left==0)break;
}
if(dis[v]<mindis)
mindis=dis[v];
}
}
if(left==aug)
{
if(!(--gap[dis[src]]))dis[sourse]=nn;
dis[src]=mindis+1;
gap[dis[src]]++;
}
return aug-left;
}
int sap(int s,int e)
{
int ans=0;
nn=e+1;
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
gap[0]=nn;
sourse=s;
sink=e;
while(dis[sourse]<nn)
ans+=dfs(sourse,inf);
return ans;
}
int main()
{
int n,m;
double ss,v;
while(scanf("%d%d%lf%lf",&n,&m,&ss,&v)!=EOF)
{
int s=0,sum=0,t=n+m+1;
dist=ss*v;
memset(head,-1,sizeof(head));
nodes=0;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&p1[i].x,&p1[i].y);
addedge(0,i,1);
}
for(int i=1;i<=m;i++)
{
addedge(i+n,t,1);
scanf("%lf%lf",&p2[i].x,&p2[i].y);
for(int j=1;j<=n;j++)
if(judge(i,j))
addedge(j,n+i,1);
}
int anss=sap(s,t);
printf("%d\n",n-anss);
}
return 0;
}