hdu2389二分图之Hopcroft Karp算法
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.
InputThe 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 <= 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.
OutputFor 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
题意:给n个人坐标速度和m个伞坐标进行匹配,转化成二分图匹配
题解:二遍循环判断是否能到达,进行匹配,匈牙利算法会tle,用Hopcroft Karp算法(暂时还不是很理解,先背下来)
参考的博客,图很清楚但是注释有点少http://www.cnblogs.com/penseur/archive/2013/06/16/3138981.html
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; struct edge{
int x,y,v;
}g[N],u[N]; int n,m,dis;
bool used[N],ok[N][N];
int mx[N],my[N];//mx保存右侧匹配点,my保存左侧匹配点
int dx[N],dy[N]; double road(edge a,edge b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool searchP()//寻找增广路径集
{
queue<int>q;
dis=inf;
memset(dy,-,sizeof dy);
memset(dx,-,sizeof dx);
for(int i=;i<=n;i++)
if(mx[i]==-)//将未访问过的左侧点加入队列
{
q.push(i);
dx[i]=;//距离设为0
}
while(!q.empty()){
int u=q.front();
q.pop();
if(dx[u]>dis)break;
for(int i=;i<=m;i++)//取左侧点匹配到右侧
{
if(ok[u][i]&&dy[i]==-)//右侧点联通且未访问
{
dy[i]=dx[u]+;//i对应距离为u对应距离+1
if(my[i]==-)dis=dy[i];//i无匹配点
else
{
dx[my[i]]=dy[i]+;
q.push(my[i]);
}
}
}
}
return dis!=inf;
}
bool dfs(int x)
{
for(int i=;i<=m;i++)
{
if(!used[i]&&ok[x][i]&&dy[i]==dx[x]+)//没有访问过且距上一点为1
{
used[i]=;
if(my[i]!=-&&dy[i]==dis)continue;
if(my[i]==-||dfs(my[i]))
{
my[i]=x;
mx[x]=i;
return ;
}
}
}
return ;
}
int solve()
{
int ans=;
memset(mx,-,sizeof mx);
memset(my,-,sizeof my);
while(searchP()){
memset(used,,sizeof used);
for(int i=;i<=n;i++)
if(mx[i]==-&&dfs(i))
ans++;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int t,time;
cin>>t;
for(int k=;k<=t;k++)
{
cin>>time>>n;
for(int i=;i<=n;i++)cin>>g[i].x>>g[i].y>>g[i].v;
cin>>m;
for(int i=;i<=m;i++)cin>>u[i].x>>u[i].y;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(time*g[i].v>=road(g[i],u[j]))ok[i][j]=;
else ok[i][j]=;
}
}
cout<<"Scenario #"<<k<<":"<<endl<<solve()<<endl<<endl;
}
return ;
}
hdu2389二分图之Hopcroft Karp算法的更多相关文章
- HDU5090--Game with Pearls 二分图匹配 (匈牙利算法)
题意:给N个容器,每个容器里有一定数目的珍珠,现在Jerry开始在管子上面再放一些珍珠,放上的珍珠数必须是K的倍数,可以不放.最后将容器排序,如果可以做到第i个容器上面有i个珍珠,则Jerry胜出,反 ...
- "《算法导论》之‘图’":不带权二分图最大匹配(匈牙利算法)
博文“二分图的最大匹配.完美匹配和匈牙利算法”对二分图相关的几个概念讲的特别形象,特别容易理解.本文介绍部分主要摘自此博文. 还有其他可参考博文: 趣写算法系列之--匈牙利算法 用于二分图匹配的匈牙利 ...
- 二分图最大匹配(匈牙利算法)简介& Example hdu 1150 Machine Schedule
二分图匹配(匈牙利算法) 1.一个二分图中的最大匹配数等于这个图中的最小点覆盖数 König定理是一个二分图中很重要的定理,它的意思是,一个二分图中的最大匹配数等于这个图中的最小点覆盖数.如果你还不知 ...
- 最大流算法之Ford-Fulkerson算法与Edmonds–Karp算法
引子 曾经很多次看过最大流的模板,基础概念什么的也看了很多遍.也曾经用过强者同学的板子,然而却一直不会网络流.虽然曾经尝试过写,然而即使最简单的一种算法也没有写成功过,然后对着强者大神的代码一点一点的 ...
- UVALive 6811 Irrigation Line(二分图最小点覆盖--匈牙利算法)
题意:求最少的线可以覆盖一个由0.1两种数字组成的图中所有的1. eg: 只需要两条线即可. 分析: 1.先为上述例子的行列标号 2.若图中数字为1,则代表该数字所在的行与列有关联. 例如第r1行第c ...
- 二分图最大匹配:匈牙利算法的python实现
二分图匹配是很常见的算法问题,一般用匈牙利算法解决二分图最大匹配问题,但是目前网上绝大多数都是C/C++实现版本,没有python版本,于是就用python实现了一下深度优先的匈牙利算法,本文使用的是 ...
- 51nod 2006 飞行员配对(二分图最大匹配) 裸匈牙利算法 求二分图最大匹配题
题目: 题目已经说了是最大二分匹配题, 查了一下最大二分匹配题有两种解法, 匈牙利算法和网络流. 看了一下觉得匈牙利算法更好理解, 然后我照着小红书模板打了一遍就过了. 匈牙利算法:先试着把没用过的左 ...
- hdu2255 奔小康赚大钱 二分图最佳匹配--KM算法
传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住(如果有老百姓没房子住 ...
- HDU 2444 The Accomodation of Students二分图判定和匈牙利算法
本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...
随机推荐
- 优雅的使用sublime写lua~ sublime lua相关必装插件推荐~~
缘起 lua脚本语言虽好,代码写得飞快,可是写错了调试起来却很困难,lua使用者经常容易犯得一个错误是--写错变量名了,if end 嵌套太多没匹配~,多打了一个逗号, 假设定义了一个变量 local ...
- JAVA基础知识(2)--关键字static的使用
在Java类中声明属性.方法和内部类时,可使用关键字static作为修饰符,static标记的属性和方法可以由整个类进行共享,因此static修饰的属性称为类成员或者称为类方法:static修饰的方法 ...
- yii2.0自带验证码使用
相信大家刚接触yii框架的时候都会觉得它比较麻烦.很不顺手.但我相信,只要你使用过一段时间后就会发觉它超给力,很高大上.里面的小物件很多,让你随心所欲的使用. 自带验证码实现也挺简单的,代码如下 1. ...
- flex中过滤掉字符串的空格
1.先引入import mx.utils.StringUtil;这个包, 在使用StringUtil.trim(); 如:if(StringUtil.trim(this.d1.3.text) ==&q ...
- Java日期格式化方法
首先获取当前系统时间的方法有两种:第一种可以用currentTimeMillis()方法获取,它其实产生的是一个当前的毫秒数,这个毫秒是自1970年1月1日0时起至现在的毫秒数,类型是long 型,可 ...
- Visual Studio 2017创建.net standard类库编译出错原因
正式版上个月已经Release了,从那时到现在经常会收到更新提示,估计问题还不少吧!其中最吸引我的当然是.net standard与.net core. 刚好最近接触.net standard项目,新 ...
- JavaScript基础学习(七)—BOM
BOM(Browser Object Model): 浏览器对象模型.提供了独立于内容而与浏览器窗口交互的对象,BOM主要用于管理窗口和窗口之间的通讯. 一.Navigator对象 ...
- Android开发学习-view
题记:抱着对Android开发的浓厚兴趣,加入了Study jams的线上学习小组,开启了自己的Android学习之旅.一.学习前准备:1.自己动手搭建了"Android Studio&qu ...
- 变态版大鱼吃小鱼-基于pixi.js 2D游戏引擎
之前用CSS3画了一条
- JavaScript中数组类型的属性和方法
除了Object,Array类型应该是ECMAScript中最常用的类型了. ECMAScript的数组虽然也是数据的有序列表,但还是与其他语言中的数组有很大的区别.比如ECMAScript数组每一项 ...