HackerRank "Bike Racer"
Just for study from its editorial~
Lesson learnt: an optimized Hungarian Algorithm: Hopcroft-Karp Algorithm (a batched version of Hungarian)
A very good article on it (in Chinese): https://www.renfei.org/blog/bipartite-matching.html
The basic idea of Hungarian is: find all augment pathes and flip (matched\unmatched toggled) them, recursively.
And Hopcroft-Karp is a batched version of Hungarian: we simply check all edges at each node.
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std; #define MAX 2001 typedef long long LL; int N, M, K;
vector<int> edges[MAX]; // edges of left side
vector<bool> visited(MAX);
vector<int> Left(MAX), Right(MAX);
vector<vector<LL>> dist(MAX, vector<LL>(MAX)); /*
* Hopcroft-Karp Algorithm: optimized(batched) Hungarian Algorithm
* Complexity: E*sqrt(V)
*/ // True: augment path flipped
// False:augment path stay the same
//
bool dfs(int u)
{
if(visited[u]) return false; visited[u] = true; // Flip input u-v pairs
for(auto v : edges[u])
{
if(Right[v] == -) // u-v not matched
{
// then match u-v
Right[v] = u, Left[u] = v;
return true;
}
} // Given all input u-v are matched then match deeper pathes
for(auto v : edges[u])
{
if(dfs(Right[v])) // flipped deeper?
{
// then flip current edge too
Right[v] = u, Left[u] = v;
return true;
}
}
return false;
} int match()
{
// Cleanup work
Left.assign(MAX, -);
Right.assign(MAX, -); int i, ret = ;
bool done = true;
do
{
done = true; // for the new aug. path
visited.assign(MAX, ); for(int i = ; i <= N; i ++)
if(Left[i] == - && dfs(i))
done = false; // augment-able? again.. }while(!done); // Count no. of matched edges
for(int i = ; i <= N; i ++)
ret += Left[i] != -;
return ret;
}
/**********************************************/ bool check(LL val)
{
// Pick reasonable edges
for( int i= ; i<=N ; i++)
for( int j= ; j<=M ; j++)
if(dist[i][j] <= val)
edges[i].push_back(j); // Run Hopcroft-Karp
LL num_match = match(); // Clean for the next check
for(int i= ; i<= N ; i++)
edges[i].clear(); return num_match >= K;
} int main()
{
cin >> N >> M >> K; // Get input array
vector<pair<LL, LL>> P(N + ), Q(M + );
for(int i = ; i <= N; i ++)
cin >> P[i].first >> P[i].second;
for(int i = ; i <= M; i ++)
cin >> Q[i].first >> Q[i].second; // Calculate distances
for(int i= ; i<=N ; i++)
for(int j= ; j<=M ; j++)
{
LL a = P[i].first - Q[j].first;
LL b = P[i].second - Q[j].second;
dist[i][j] = a * a + b * b;
} // Binay Search the min matching result for K
LL low = , high = ;
while(low < high)
{
LL mid = (low + high) >> ;
if(check(mid)) high = mid;
else low = mid + ;
}
cout << low << endl;
return ;
}
HackerRank "Bike Racer"的更多相关文章
- Hackerrank 2020 February 2014 解题报告
Hackerrank 2020 February 2014 解题报告 比赛链接 Sherlock and Watson (20分) 题意:给定一个数组,向右平移K次,然后有Q个询问,问第x位置上是几 ...
- Rust初步(三):使用atom搭配racer进行rust编程
在rust.cc社区中有一个关于rust编辑器的讨论(话说很多人要学一个新语言,都会立即考虑编辑器的问题,包括我在内),主要关注的是,智能提示(这个真的太重要了).大家讨论下来有几个选择 1. ecl ...
- UVALive 6908---Electric Bike(DP或记录型深搜)
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- 日常小测:颜色 && Hackerrank Unique_colors
题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...
- HackerRank "Square Subsequences" !!!
Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...
- HackerRank "Minimum Penalty Path"
It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial ...
- HackerRank "TBS Problem" ~ NPC
It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...
- HackerRank Extra long factorials
传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...
- HackerRank "Lucky Numbers"
Great learning for me:https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hacke ...
随机推荐
- 用Xshell访问 虚拟机里的kali
1.vim /etc/ssh/sshd_config 2.PermitRootLogin without-password复制 3.把一个PermitRootLogin without-passwor ...
- HTML DOM部分---做竖向横向的下拉导航 下拉菜单 图片轮播(圆点、箭头) 选项卡 进度条;
1,竖向下拉导航 鼠标单击打开 再打击关闭 <style> *{ margin:0px auto; padding:0px;} div{ width:100px; height:50px; ...
- Java 23种设计模式全解析
转自:http://blog.csdn.net/longyulu/article/details/9159589
- JavaScript学习记录总结(五)——servlet将json数据写出去
定义teacher和student实体 json.do List<Student> stus=new ArrayList<Student>(); stus.a ...
- mysql数据库主从复制部署笔记
主从复制是mysql中数据库实时同步的一个常用做法了,今天我来给各位介绍一下关于mysql数据库主从复制部署一个过程,希望此例子对各位同学参考参考. 数据库主从复制原理: 数据库的主从复制就是从mas ...
- poj1680 最短路判环
题意:有多个银行可以换钱,每个银行可以将特定的两种钱相互兑换,并且有自己的汇率,现在问是否可以将自己的钱通过银行兑换增加. 其实比较水,主要就是知道最短路问题里的负环可以通过bellman-fold或 ...
- <老友记>学习笔记
这是六个人的故事,从不服输而又有强烈控制欲的monica,未经世事的千金大小姐rachel,正直又专情的ross,幽默风趣的chandle,古怪迷人的phoebe,花心天真的joey——六个好友之间的 ...
- kuangbin_ShortPath G (POJ 1502)
尽管题目很长 写的很玄乎 让我理解了半天 但是事实上就是个模板题啊摔 一发水过不解释 #include <iostream> #include <string> #includ ...
- google和ebay微服务经验
摘自:http://www.infoq.com/cn/articles/ecosystems-of-microservices 多元化(polyglot)微服务是终极游戏 大规模系统和多元化微服务最终 ...
- 【C++11】30分钟了解C++11新特性
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 什么是C++11 C++11是曾经被叫做C+ ...