HDU 2389 ——Rain on your Parade——————【Hopcroft-Karp求最大匹配、sqrt(n)*e复杂度】
Time Limit:3000MS Memory Limit:165535KB 64bit IO Format:%I64d & %I64u
Description
But nothing ever is perfect. One of your guests works in weather forecasting. He suddenly yells, “I know that breeze! It means its going to rain heavily in just a few minutes!” Your guests all wear their best dresses and really would not like to get wet, hence they stand terrified when hearing the bad news.
You have prepared a few umbrellas which can protect a few of your guests. The umbrellas are small, and since your guests are all slightly snobbish, no guest will share an umbrella with other guests. The umbrellas are spread across your (gigantic) garden, just like your guests. To complicate matters even more, some of your guests can’t run as fast as the others.
Can you help your guests so that as many as possible find an umbrella before it starts to pour?
Given the positions and speeds of all your guests, the positions of the umbrellas, and the time until it starts to rain, find out how many of your guests can at most reach an umbrella. Two guests do not want to share an umbrella, however.
Input
Each test case starts with a line containing the time t in minutes until it will start to rain (1 <=t <= 5). The next line contains the number of guests m (1 <= m <= 3000), followed by m lines containing x- and y-coordinates as well as the speed si in units per minute (1 <= s i<= 3000) of the guest as integers, separated by spaces. After the guests, a single line contains n (1 <= n <= 3000), the number of umbrellas, followed by n lines containing the integer coordinates of each umbrella, separated by a space.
The absolute value of all coordinates is less than 10000.
Output
Sample Input
Sample Output
2
2
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 3100;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
struct Guest{
double x,y,s;
}guests[maxn];
struct Umbrella{
double x,y;
}umbrellas[maxn];
double distan(Guest a,Umbrella b){
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx*dx+dy*dy);
}
vector<int>G[maxn];
int Mx[maxn], My[maxn], dx[maxn], dy[maxn], used[maxn], dis;
bool SearchP(int _n){ //传参x部的顶点个数,处理出来dx与dy数组
queue<int>Q;
memset(dx,-1,sizeof(dx));
memset(dy,-1,sizeof(dy));
int dis = INF;
for(int i = 1; i <= _n; i++){
if(Mx[i] == -1){ //将x部的未盖点入队
dx[i] = 0;
Q.push(i);
}
}
int v;
while(!Q.empty()){
int u = Q.front(); Q.pop();
if(dx[u] > dis) break; //没有更小的dis
for(int i = 0; i < G[u].size(); i++){
v = G[u][i];
if(dy[v] == -1){ //如果y部的v没有访问过
dy[v] = dx[u] + 1; //更新dy
if(My[v] == -1){ //如果y部的v是未盖点,找到了最短增广路长度
dis = dy[v];
}else{
dx[My[v]] = dy[v] + 1; //更新v的x部匹配点
Q.push(My[v]); //将v的匹配点入队
}
}
}
}
return dis != INF; //找到了最短增广路
}
int dfs(int u){
int v;
for(int i = 0; i < G[u].size(); i++){
v = G[u][i];
if(!used[v] && dy[v] == dx[u] + 1){ //v未访问过且距离相差为1
used[v] = 1;
if(My[v] != -1 && dy[v] == dis){ //如果v不是未盖点且已经等于最短增广路距原点距离,说明有更短的可以去增广
continue;
}
if(My[v] == -1 || dfs(My[v])){ //如果v是y部未盖点或者原来跟v匹配的x部节点能另外找到一个匹配
Mx[u] = v; //匹配u、v
My[v] = u;
return true;
}
}
}
return false;
}
int MaxMatch(int ln,int rn){ //传参左、右部顶点个数,返回最大匹配个数
int ret = 0;
memset(Mx,-1,sizeof(Mx)); //x部初始化未盖点
memset(My,-1,sizeof(My)); //y部初始化未盖点
while(SearchP(ln)){
memset(used,0,sizeof(used)); //初始化未访问
for(int i = 1; i <= ln; i++){
if(Mx[i] == -1 && dfs(i)){ //如果x部为未盖点且找到了增广路
ret++;
}
}
}
return ret;
}
int main(){
int T, cas = 0, n, m;
double t;
scanf("%d",&T);
while(T--){
scanf("%lf",&t);
scanf("%d",&m);
for(int i = 0; i <= m; i++){
G[i].clear();
}
for(int i = 1; i <= m; i++){
scanf("%lf%lf%lf",&guests[i].x,&guests[i].y,&guests[i].s);
}
scanf("%d",&n);
for(int i = 1; i <= n; i++){
scanf("%lf%lf",&umbrellas[i].x,&umbrellas[i].y);
}
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
double dd = distan(guests[i],umbrellas[j]);
if(guests[i].s * t >= dd){
G[i].push_back(j);
}
}
}
int res = MaxMatch(m,n);
printf("Scenario #%d:\n%d\n\n",++cas,res);
}
return 0;
}
HDU 2389 ——Rain on your Parade——————【Hopcroft-Karp求最大匹配、sqrt(n)*e复杂度】的更多相关文章
- HDU 2389 Rain on your Parade / HUST 1164 4 Rain on your Parade(二分图的最大匹配)
HDU 2389 Rain on your Parade / HUST 1164 4 Rain on your Parade(二分图的最大匹配) Description You're giving a ...
- 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/Ot ...
- HDU 2389 Rain on your Parade
大意:在一个二维坐标系上有nx个人和ny把伞,每个人都有自己的移动速度,问有多少人可以再 time 时间内移动到不同的雨伞处(不允许两个人共用一把伞). 输入数据: 第一行是一个T代表T组测试数据 ...
- HDU 2389 Rain on your Parade 最大匹配(模板题)【HK算法】
<题目链接> 题目大意:有m个宾客,n把雨伞,预计时间t后将会下大雨,告诉你每个宾客的位置和速度,每把雨伞的位置,问你最多几个宾客能够拿到伞. 解题分析: 本题就是要我们求人与伞之间的最大 ...
- Hdu 3289 Rain on your Parade (二分图匹配 Hopcroft-Karp)
题目链接: Hdu 3289 Rain on your Parade 题目描述: 有n个客人,m把雨伞,在t秒之后将会下雨,给出每个客人的坐标和每秒行走的距离,以及雨伞的位置,问t秒后最多有几个客人可 ...
- HDOJ 2389 Rain on your Parade
HK.... Rain on your Parade Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 655350/165535 K ...
- Hdu2389 Rain on your Parade (HK二分图最大匹配)
Rain on your Parade Problem Description You’re giving a party in the garden of your villa by the sea ...
- 【HDOJ】2389 Rain on your Parade
读题显然是二分图匹配,看成guest与umbrella的匹配.匈牙利果断TLE了,其实时间卡的相当紧.HK过的,750ms. /* 2389 */ #include <iostream> ...
- Rain on your Parade---hdu2389(HK求最大匹配)
题目链接 题意:有n个客人,m把雨伞,在t秒之后将会下雨,给出每个客人的坐标和每秒行走的距离,以及雨伞的位置,问t秒后最多有几个客人可以拿到雨伞? 就是求最大匹配的 Hopcroft-Karp复杂度 ...
随机推荐
- Apache虚拟主机-解惑篇
有很多平时喜欢钻研的童鞋会发现,为什么有时候自己访问某XXse网站时,总是更新IP地址,内容却与以前一样.这个时候就要了解虚拟主机的概念了.了解这个概念,能够帮助运维同学,更内涵的隐藏自己的主 ...
- (Python OpenGL)【5】平移 PyOpenGL
(Python OpenGL) 原文:http://ogldev.atspace.co.uk/www/tutorial06/tutorial06.html (英文) 下面是我翻译过来的: 背景 在本 ...
- C++并发低级接口:std::thread和std::promise
std::thread和std::promise 相比std::async,std::thread就原始多了.thread一定会创建新线程(而不是像async那样创建的时候可能不会,后面才创建新线程( ...
- linux物理内存管理
1.为什么需要连续的物理内存: Linux内核管理物理内存是通过分页机制实现的,它将整个内存划分成无数个4k(在i386体系结构中)大小的页,从而分配和回收内存的基本单位便是内存页了.利用分页管理有助 ...
- appium键盘处理
最近对appium感兴趣,就从网上找了些资料,搭建了环境,下载了appium测试代码和测试apk,这方面的东西晚上再写 appium最新版(v1.4.0.0)已经没有sendKeyEvent了,所以现 ...
- pytorch实现depthwise convolution
深度分离卷积是Xception这个模型中提出来的(不太确定,但肯定是它让这个概念为大众周知),具体来说分为两步,depthwise conv和pointwise conv,前者对输入特征图的每个通道进 ...
- js 遍历tree的一个例子
小例子蕴含着到力量,我希望自己有改变自己生活的能力,自己有主宰自己命运的能力... 备注:这个例子有问题. <!DOCTYPE html> <html lang="en&q ...
- maven 配置jetty 插件
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin ...
- Codeforces Round #347 (Div. 2) A
Description Greatest common divisor GCD(a, b) of two positive integers a and b is equal to the bigge ...
- 求js数组的最大值和最小值
数组 ,,,,,,,,,]; 方法1 - 字符串拼接法 利用toString或join把数组转换为字符串,再和Math的max和min方法分别进行拼接,最后执行eval方法 var max = eva ...