ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Gopher II

2022-01-19 23:30:33  阅读:176  来源: 互联网

标签:老鼠 int Gopher coordinates gopher II gophers each


Gopher II(二分图匹配-匈牙利算法)

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.
Samples

Samples
Input
2 2 5 10
1.0 1.0
2.0 2.0
100.0 100.0
20.0 20.0
Output
1

题意:
n个老鼠,m个洞,每个老鼠到洞的速度为v,老鼠到洞的时间不能超过S,要不然会被吃掉,求最少被吃了几个老鼠
思路:
求这个二分图的最大匹配。到洞不超过s的老鼠和洞之间有边

int n,m,c[1000][1000],pre[maxn];
bool vis[maxn];
double s,v;
struct node
{
    double x,y;
}G[200],H[200];
double dis(node p,node q)
{
    return  sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));
}
int dfs(int x)
{
    for(int i=1;i<=m;i++){
        if(!vis[i]&&c[x][i]){//扫描每个dong
        //如果有bian并且还没有标记过(这里标记的意思是这次查找曾试图改变过该dong的归属问题,但是没有成功,所以就不用瞎费工夫了)
            vis[i]=1;
            if(pre[i]==-1||dfs(pre[i])){
				//名花无主或者能腾出个位置来,这里使用递归
                pre[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    
    int i,j,a,b,t;
    while(~scanf("%d%d%lf%lf",&n,&m,&s,&v)){
        memset(pre,-1,sizeof(pre));
        memset(c,0,sizeof(c));
        for(i=1;i<=n;i++){
            scanf("%lf%lf",&G[i].x,&G[i].y);
        }
        for(i=1;i<=m;i++){
            scanf("%lf%lf",&H[i].x,&H[i].y);
        }
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                double d=dis(G[i],H[j]);
                d/=v;
                if(d<=s){
                   c[i][j]=1;
                }
            }
        }
        int ans=0;
        for(i=1;i<=n;i++){//匈牙利算法
            memset(vis,0,sizeof(vis));
            ans+=dfs(i);
        }
        printf("%d\n",n-ans);
    }
    return 0;
}

标签:老鼠,int,Gopher,coordinates,gopher,II,gophers,each
来源: https://blog.csdn.net/qq_51761458/article/details/122591978

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有