HDU2389 Rain on your Parade(HK模版)

题意:

在一个二维坐标系上有n个人和m把伞,每个人都有自己的移动速度,

问有多少人可以在s min内移动到不同的雨伞处(不允许两个人共用一把伞)。

思路:

由于nm太大(3000),匈牙利会超时,就用HK算法

整理了HK的模版

/* ***********************************************
//Author        :devil
//Created Time  :2016/5/10 23:13:51
//************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
const int N=3010;
const int inf=0x3f3f3f3f;
vector<int>eg[N];
int ax[N],ay[N],as[N],bx[N],by[N],n,m;
int mx[N],my[N];
int dx[N],dy[N],dis;
bool vis[N];
bool bfs()
{
    queue<int>q;
    dis=inf;
    memset(dx,-1,sizeof(dx));
    memset(dy,-1,sizeof(dy));
    for(int i=0;i<n;i++)
        if(mx[i]==-1)
        {
            q.push(i);
            dx[i]=0;
        }
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        if(dx[u]>dis) break;
        for(int i=0;i<eg[u].size();i++)
        {
            int v=eg[u][i];
            if(dy[v]==-1)
            {
                dy[v]=dx[u]+1;
                if(my[v]==-1) dis=dy[v];
                else
                {
                    dx[my[v]]=dy[v]+1;
                    q.push(my[v]);
                }
            }
        }
    }
    return dis!=inf;
}
bool dfs(int u)
{
    for(int i=0;i<eg[u].size();i++)
    {
        int v=eg[u][i];
        if(!vis[v]&&dy[v]==dx[u]+1)
        {
            vis[v]=1;
            if(my[v]!=-1&&dy[v]==dis) continue;
            if(my[v]==-1||dfs(my[v]))
            {
                my[v]=u;
                mx[u]=v;
                return 1;
            }
        }
    }
    return 0;
}
int MaxMatch()
{
    int ans=0;
    memset(mx,-1,sizeof(mx));
    memset(my,-1,sizeof(my));
    while(bfs())
    {
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
            if(mx[i]==-1&&dfs(i))
                ans++;
    }
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t,s,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<=3000;i++)
            eg[i].clear();
        scanf("%d%d",&s,&n);
        for(int i=0;i<n;i++)
            scanf("%d%d%d",&ax[i],&ay[i],&as[i]);
        scanf("%d",&m);
        for(int i=0;i<m;i++)
            scanf("%d%d",&bx[i],&by[i]);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if((ax[i]-bx[j])*(ax[i]-bx[j])+(ay[i]-by[j])*(ay[i]-by[j])<=as[i]*as[i]*s*s)
                    eg[i].push_back(j);
        printf("Scenario #%d:\n",++cas);
        printf("%d\n\n",MaxMatch());
    }
    return 0;
}
时间: 2024-07-30 13:36:38

HDU2389 Rain on your Parade(HK模版)的相关文章

hdu-2389.rain on your parade(二分匹配HK算法)

Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)Total Submission(s): 6752    Accepted Submission(s): 2117 Problem Description You’re giving a party in the garden of your villa by the sea. The p

HDU 2389 Rain on your Parade

http://acm.hdu.edu.cn/showproblem.php?pid=2389 题意:给暴风雨到来的时刻,m个人的坐标和单位速度,和n个救生衣的坐标.每个救生衣只能匹配一个人,求最多有多少人可以得到救生衣. 题解:典型二分图最大匹配题型.因为点比较多,使用Hopcroft-Karp算法.讲解:http://blog.csdn.net/wall_f/article/details/8248373 http://www.cnblogs.com/-sunshine/archive/201

Hdu 3289 Rain on your Parade (二分图匹配 Hopcroft-Karp)

题目链接: Hdu 3289 Rain on your Parade 题目描述: 有n个客人,m把雨伞,在t秒之后将会下雨,给出每个客人的坐标和每秒行走的距离,以及雨伞的位置,问t秒后最多有几个客人可以拿到雨伞? 解题思路: 数据范围太大,匈牙利算法O(n*m)果断华丽丽的TLE,请教了一下度娘,发现还有一种神算法—— Hopcroft-Karp,然后就get√新技能,一路小跑过了,有一点不明白的是hdu上竟然有人0ms过,这又是什么神姿势(吓哭!!!!!),额.........,扯远了.  H

HDU 2389 Rain on your Parade (二分图匹配(Hopcroft-Carp的算法模板))

Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others) Total Submission(s): 3033    Accepted Submission(s): 952 Problem Description You're giving a party in the garden of your villa by the sea. The p

HDU 2389 ——Rain on your Parade——————【Hopcroft-Karp求最大匹配、sqrt(n)*e复杂度】

Rain on your Parade Time Limit:3000MS     Memory Limit:165535KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2389 Description You’re giving a party in the garden of your villa by the sea. The party is a huge success, and everyone is h

hust 1164 4 Rain on your Parade

题目描述 You're giving a party in the garden of your villa by the sea. The party is a huge success, and everyone is here. It's a warm, sunny evening, and a soothing wind sends fresh, salty air from the sea. The evening is progressing just as you had imag

hdu 2389 Rain on your Parade(二分图HK算法)

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> using namespace std; const int inf=0x3f3f3f3f; const int maxn=3003; const int maxm=maxn*maxn; int xlink[maxn],ylink[maxn]; int dx[maxn

F - Rain on your Parade - hdu 2389(二分图匹配,Hk算法)

题意:给一些人和一些伞的坐标,然后每个人都有一定的速度,还有多少时间就会下雨,问最多能有多少人可以拿到伞. 分析:题意很明确,可以用每个人和伞判断一下是否能够达到,如果能就建立一个联系.不过这道题的数据还是挺大的,第一次使用的匈牙利算法果断的TLE了,然后就百度了一下发现有一个 Hopcroft-Karp算法 不过这个算法网上描述的很少,而且都说的比较含糊不清,不过幸好搜到一个比较不错的课件,看了一上午总算有些明白怎么回事,以前是寻找一个增广路,这个是寻找所有的增广路,并且使用BFS进行分层,看

【HDOJ】2389 Rain on your Parade

读题显然是二分图匹配,看成guest与umbrella的匹配.匈牙利果断TLE了,其实时间卡的相当紧.HK过的,750ms. 1 /* 2389 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #