Rain on your Parade

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)
Total Submission(s): 3310    Accepted Submission(s): 1066

Problem Description
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 imagined. It could be the perfect end of a beautiful day.
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
The input starts with a line containing a single integer, the number of test cases.
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 <= si <= 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
For each test case, write a line containing “Scenario #i:”, where i is the number of the test case starting at 1. Then, write a single line that contains the number of guests that can at most reach an umbrella before it starts to rain. Terminate every test case with a blank line.
 
Sample Input
2
1
2
1 0 3
3 0 3
2
4 0
6 0
1
2
1 1 2
3 3 2
2
2 2
4 4
 
Sample Output
Scenario #1:
2
 
Scenario #2:
2
 
Source
 
这道题用匈牙利算法会超时,匈牙利算法复杂度O(V*E)
Hopcroft-Carp算法复杂度O(sqrt(V)*E)
/*
ID: LinKArftc
PROG: 2389.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; struct Node {
double x, y, speed;
Node() {}
Node(double _x, double _y) : x(_x), y(_y) {}
Node(double _x, double _y, double _s) : x(_x), y(_y), speed(_s) {}
} men[maxn], un[maxn]; vector<int>G[maxn];
int uN, vN;
int Mx[maxn],My[maxn];
int dx[maxn],dy[maxn];
int dis;
bool used[maxn];
bool SearchP()
{
queue<int>Q;
dis = INF;
memset(dx,-,sizeof(dx));
memset(dy,-,sizeof(dy));
for(int i = ; i <= uN; i++)
if(Mx[i] == -)
{
Q.push(i);
dx[i] = ;
}
while(!Q.empty())
{
int u = Q.front();
Q.pop();
if(dx[u] > dis)break;
int sz = G[u].size();
for(int i = ;i < sz;i++)
{
int v = G[u][i];
if(dy[v] == -)
{
dy[v] = dx[u] + ;
if(My[v] == -)dis = dy[v];
else
{
dx[My[v]] = dy[v] + ;
Q.push(My[v]);
}
}
}
}
return dis != INF;
}
bool DFS(int u)
{
int sz = G[u].size();
for(int i = ;i < sz;i++)
{
int v = G[u][i];
if(!used[v] && dy[v] == dx[u] + )
{
used[v] = true;
if(My[v] != - && dy[v] == dis)continue;
if(My[v] == - || DFS(My[v]))
{
My[v] = u;
Mx[u] = v;
return true;
}
}
}
return false;
}
int MaxMatch()
{
int res = ;
memset(Mx,-,sizeof(Mx));
memset(My,-,sizeof(My));
while(SearchP())
{
memset(used,false,sizeof(used));
for(int i = ;i <= uN;i++)
if(Mx[i] == - && DFS(i))
res++;
}
return res;
} int main() {
//input;
int T, t, _t = ;
scanf("%d", &T);
while (T --) {
scanf("%d", &t);
scanf("%d", &uN);
for (int i = ; i <= uN; i ++) scanf("%lf %lf %lf", &men[i].x, &men[i].y, &men[i].speed);
scanf("%d", &vN);
for (int i = ; i <= vN; i ++) scanf("%lf %lf", &un[i].x, &un[i].y);
for (int i = ; i <= uN; i ++) {
G[i].clear();
for (int j = ; j <= vN; j ++) {
if (sqrt(fabs(men[i].x - un[j].x) * fabs(men[i].x - un[j].x) + fabs(men[i].y - un[j].y) * fabs(men[i].y - un[j].y)) - men[i].speed * t < eps) G[i].push_back(j);
}
}
printf("Scenario #%d:\n%d\n\n", _t ++, MaxMatch());
} return ;
}

HDU2389(二分图匹配Hopcroft-Carp算法)的更多相关文章

  1. hdu2389二分图之Hopcroft Karp算法

    You're giving a party in the garden of your villa by the sea. The party is a huge success, and every ...

  2. HDU5090--Game with Pearls 二分图匹配 (匈牙利算法)

    题意:给N个容器,每个容器里有一定数目的珍珠,现在Jerry开始在管子上面再放一些珍珠,放上的珍珠数必须是K的倍数,可以不放.最后将容器排序,如果可以做到第i个容器上面有i个珍珠,则Jerry胜出,反 ...

  3. [ACM] HDU 3395 Special Fish (最大重量二分图匹配,KM算法)

    Special Fish Problem Description There is a kind of special fish in the East Lake where is closed to ...

  4. CF1139E Maximize Mex(二分图匹配,匈牙利算法)

    好题.不过之前做过的[SCOI2010]连续攻击游戏跟这题一个套路,我怎么没想到…… 题目链接:CF原网 洛谷 题目大意:在一个学校有 $n$ 个学生和 $m$ 个社团,每个学生有一个非负整数能力值 ...

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

    题意:给一些人和一些伞的坐标,然后每个人都有一定的速度,还有多少时间就会下雨,问最多能有多少人可以拿到伞. 分析:题意很明确,可以用每个人和伞判断一下是否能够达到,如果能就建立一个联系.不过这道题的数 ...

  6. 【模板】解决二分图匹配的强力算法——Hopcroft-Karp算法

    详细解释 参见:http://blog.csdn.net/wall_f/article/details/8248373 简要过程 HK算法可以当成是匈牙利算法的优化版,和dinic算法的思想比较类似. ...

  7. hdu2063 二分图匹配,匈牙利算法

    #include <stdio.h> #include <string.h> int n1,n2,m,ans; ]; //记录V2中的点匹配的点的编号 ]; //记录V2中的每 ...

  8. 二分图匹配(KM算法)n^4 分类: ACM TYPE 2014-10-04 11:36 88人阅读 评论(0) 收藏

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

  9. 二分图匹配(KM算法)n^3 分类: ACM TYPE 2014-10-01 21:46 98人阅读 评论(0) 收藏

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

  10. Codevs 1222 信与信封问题 二分图匹配,匈牙利算法

    题目: http://codevs.cn/problem/1222/ 1222 信与信封问题   时间限制: 1 s   空间限制: 128000 KB   题目等级 : 钻石 Diamond 题解 ...

随机推荐

  1. python学习总结----简单数据结构

    mini-web服务器 - 能够完成简单的请求处理 - 使用http协议 - 目的:加深对网络编程的认识.为后面阶段学习web做铺垫 简单数据结构 - 排列组合 import itertools # ...

  2. 08-Mysql数据库----完整性约束

    总结:      1,not null 不能插入空,不设置可空       2,unique  单列唯一 create table department(name char(10) unique); ...

  3. Leetcode 678.有效的括号字符串

    有效的括号字符串 给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何右括号 ) 必须 ...

  4. Jquery append()总结(一) 转载

    转载自:http://dushanggaolou.iteye.com/blog/1173457 append(content)  /** * 向每个匹配的元素内部追加内容. * 这个操作与对指定的元素 ...

  5. 【Python】python中的装饰器——@

    对装饰器本来就一知半解的,今天终于弄清楚了,Python中的装饰器是对装饰者模式的很好运用,简化到骨子里了. python中为什么需要装饰器,看这里:http://www.cnblogs.com/hu ...

  6. 软工实践Beta冲刺(3/7)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...

  7. 剑指offer:跳台阶

    目录 题目 解题思路 具体代码 题目 题目链接 剑指offer:跳台阶 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). ...

  8. lintcode-94-二叉树中的最大路径和

    94-二叉树中的最大路径和 给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和) 样例 给出一棵二叉树: 返回 6 标签 动态规划 ...

  9. C++版本的C标准库头文件的特点

    C++标准库中除了定义C++语言特有的功能外,也兼容了C语言的标准库.C语言的头文件形如name.h,C++则将这些文件命名为cname.也就是去掉了.h后缀,而在文件名name之前添加了字母c,这里 ...

  10. Android调用Java WebSevice篇之二

    1.创建Activity. package com.web; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapO ...