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二分图判定和匈牙利算法
本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...
随机推荐
- 关于web测试收集
页面部分 页面清单是否完整(是否已经将所需要的页面全部都列出来了) 页面是否显示(在不同分辨率下页面是否存在,在不同浏览器版本中页面是是否显示) 页面在窗口中的显示是否正确.美观(在调整浏览器窗口大小 ...
- mac 下安装securecrt
下载文件链接中附带的文件. 1.先找到secureCRT的包内容,进入MACOS文件夹.替换crack中的secureCRT文件. 2.断网.进入软件,显示你的验证码过期.点continue.选择手动 ...
- ZJOI2017 Day3 滚粗记
私のZJOI Day3 2017-3-21 07:52:53 今天,考了人生当中的第一次省选(虽然只是普及组三等奖但仍然有幸能体会一下).据胡老师说,这就是来体验一下被大神虐--真的是这样,听课听不懂 ...
- ASP.NET Core MVC 源码学习:MVC 启动流程详解
前言 在 上一篇 文章中,我们学习了 ASP.NET Core MVC 的路由模块,那么在本篇文章中,主要是对 ASP.NET Core MVC 启动流程的一个学习. ASP.NET Core 是新一 ...
- iOS性能之其他
本篇文章是个引用,因为这些技术我都只是研究过,但是并没有在项目中使用,也没有深入研究,所以只能当做一个笔记了 网络请求 现在大多数的网络请求都是使用的json格式(相信没有APP再使用XML格式了吧) ...
- TextRank:关键词提取算法中的PageRank
很久以前,我用过TFIDF做过行业关键词提取.TFIDF仅仅从词的统计信息出发,而没有充分考虑词之间的语义信息.现在本文将介绍一种考虑了相邻词的语义关系.基于图排序的关键词提取算法TextRank [ ...
- 将spring cloud打包docker镜像
简介:用spring cloud构建微服务后,微服务肯定要docker镜像化然后用rancher等可视化工具管理,这就是走完了一套基本的流程,现在简单介绍下 环境:两台centos7.x的服务器,一台 ...
- php中奖算法逻辑
最近公司有两个活动, 一个是砸蛋活动, 另一个是转盘活动. 后台这边需要做接口进行对接,当用户在前台点击进行抽奖的时候,发送AJAX请求给后台,后台进行业务处理包括记录用户中奖信息,然后返回json格 ...
- eclipse如何安装插件
eclipse安装插件以springsource-tool-suite为例 打开eclipse,找到help/About Eclipse/ 然后点击右下角图标 找到EclipsePlatform对应的 ...
- 【Flex】读取本地JSON,然后JSON数据转成XML数据
package utils { import flash.xml.XMLDocument; import flash.xml.XMLNode; import mx.rpc.xml.SimpleXMLE ...