POJ2536_Gopher II(二分图最大匹配)

解题报告

http://blog.csdn.net/juncoder/article/details/38156509

题目传送门

题意:

n只地鼠,m个洞,老鹰的到达地面的时间s,地鼠的移动速度v,求多少只地鼠会被老鹰吃了。

思路:

地鼠和洞看成两集合,建立二分图。只有当地鼠到洞的时间少于老鹰到地面的时间才连边。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
int n,m,s,v,mmap[500][500],vis[500],pre[500];
struct point
{
    double x,y;
}G[200],H[200];
double dis(point p1,point p2)
{
    return  sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
int dfs(int x)
{
    for(int i=n+1;i<=n+m;i++){
        if(!vis[i]&&mmap[x][i]){
            vis[i]=1;
            if(pre[i]==-1||dfs(pre[i])){
                pre[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    //std::ios::sync_with_stdio(false);
    int i,j,a,b,t;
    while(~scanf("%d%d%d%d",&n,&m,&s,&v)){
        memset(pre,-1,sizeof(pre));
        memset(mmap,0,sizeof(mmap));
        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]);
                if(d/v<=(double)s){
                    mmap[i][n+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

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6438   Accepted: 2640

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
时间: 2024-08-22 23:36:48

POJ2536_Gopher II(二分图最大匹配)的相关文章

HDU 3081 Marriage Match II 二分图最大匹配

Marriage Match II Problem Description Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the game of playing house we used to play when we are kids. What a happy time as so many friends pl

POJ - 2536 Gopher II 二分图 最大匹配

题目大意:有n只老鼠,m个洞,一个洞只能藏一只老鼠. 有一群鹰来了,老鼠们要赶紧躲到洞里才不会被抓走. 现在给出每只老鼠的坐标,每个洞的坐标,老鼠的速度,和鹰捉到老鼠的时间,问鹰最少能抓到几只老鼠 解题思路:求出每只老鼠和每个洞之间的距离,然后除于老鼠的速度,看在鹰捉到老鼠的时间内能否跑到该洞中. 然后将老鼠和洞分成两个点集,进行二分图的最大匹配,然后n-最大匹配就是鹰至少能抓到的老鼠的数量了 #include<cstdio> #include<cstring> #include&

HDU - 3081 Marriage Match II(二分图最大匹配 + 并查集)

题目大意:有n个男生n个女生,现在要求将所有的男女配对,所有的男女都配对的话,算该轮游戏结束了.接着另一轮游戏开始,还是男女配对,但是配对的男女不能是之前配对过的. 问这游戏能玩多少轮 男女能配对的条件如下 1.男女未曾吵架过. 2.如果两个女的是朋友,那么另一个女的男朋友可以和该女的配对 解题思路:并查集解决朋友之间的关系,找到能配对的所有情况 接着二分图匹配,找到一个完美匹配算玩了一轮游戏,接着将这完美匹配的边删掉,继续进行匹配 如果无法完美匹配了,表示游戏结束了 #include <cst

POJ 2536 之 Gopher II(二分图最大匹配)

Gopher II Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6675   Accepted: 2732 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) coor

POJ 2536 Gopher II(二分图最大匹配)

题意: N只地鼠M个洞,每只地鼠.每个洞都有一个坐标. 每只地鼠速度一样,对于每只地鼠而言,如果它跑到某一个洞的所花的时间小于等于S,它才不会被老鹰吃掉. 规定每个洞最多只能藏一只地鼠. 问最少有多少只地鼠会命丧鹰口. 思路: 直接建图.二分图最大匹配. 代码: char st[105]; char Range[25][5]; int n; int num[10]; int cx[25],cy[205]; bool bmask[205]; vector<int> graph[25]; int

匈牙利算法dfs模板 [二分图][二分图最大匹配]

最近学了二分图最大匹配,bfs模板却死活打不出来?我可能学了假的bfs 于是用到了dfs模板 寻找二分图最大匹配的算法是匈牙利算法 匈牙利算法的主要程序是寻找增广路 寻找增光路是过程是:从一个未经配对的点出发,历经未配边.匹配边.未配边.匹配边.未配边....最终到达一个未配点的过程,只要把路径中的未配边和匹配边的“身份”对调,匹配就加一了.这就是一个寻找增广路的过程,通过不断寻找增广路,可以找到最大的匹配. 1 #include<cstdio> 2 #include<cstring&g

图论——LCA、强联通分量、桥、割顶、二分图最大匹配、网络流

A: 交通运输线 时间限制: 5 Sec  内存限制: 128 MB 题目描述 战后有很多城市被严重破坏,我们需要重建城市.然而,有些建设材料只能在某些地方产生.因此,我们必须通过城市交通,来运送这些材料的城市.由于大部分道路已经在战争期间完全遭到破坏,可能有两个城市之间没有道路.当然在运输线中,更不可能存在圈. 现在,你的任务来了.给你战后的道路情况,我们想知道,两个城市之间是否存在道路,如果存在,输出这两个城市之间的最短路径长度. 输入 第一行一个整数Case(Case<=10)表示测试数据

POJ 2226二分图最大匹配

匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是二部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法. #include<stdio.h> #include<string.h> #include<stdlib.h> int n1,n2; char map[1005][1005]; //数组开大点 int mapx[1005][1005],mapy[1005]

【Codevs1922】骑士共存问题(最小割,二分图最大匹配)

题意: 在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入. 对于给定的n*n个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可以放置多少个骑士,使得它们彼此互不攻击. n<=200,m<=n^2 思路:经典的二分图最大匹配问题,采用黑白点染色的思想. 如果按照相邻点黑白不同染色,可以发现每次跳到的点必定与现在所在点不同色,二分图最大匹配即可. 这里用最小割来解决,因为不能允许任何黑白点之间的任何一条边有流量,符合最小割的思想. 1