Go教程

Gopher II

本文主要是介绍Gopher II,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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;
}
这篇关于Gopher II的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!