POJ 2536 Gopher II 简单最大流


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.

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值