GYM - 101490 J Programming Tutors (匈牙利+二分)
题意:有N个学生和N个老师,每个人都有自己的坐标X,Y,给每个学生匹配一个老师,要求N个匹配中的距离最大值最小。其中距离的定义为:|X − X’| + |Y − Y ‘|.
分析:一道典型的最大值最小化的题目,可以二分逼近的方法找到最小值。二分中的check操作就用匈牙利匹配来判断。在匹配的过程中加入对边长的判断,或者直接根据边长限制建立新图。最后得到的上界就是答案。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e2+;
struct Node{
LL x,y;
}s[maxn],t[maxn];
int N;
struct Edge{
LL val;
int to,next;
}edges[maxn<<];
int head[maxn],tot;
int linker[maxn];
bool used[maxn]; void init()
{
tot=;
memset(head,-,sizeof(head));
} void AddEdge(int u,int v, LL val)
{
edges[tot].val = val;
edges[tot].to = v;
edges[tot].next = head[u];
head[u] = tot++;
} bool dfs(int u,LL limit){
int v,st,ed;
for(int i=head[u];~i;i = edges[i].next){
v = edges[i].to;
if(!used[v] && edges[i].val <=limit){
used[v]=true;
if(linker[v]==-||dfs(linker[v],limit)){
linker[v]=u;
return true;
}
}
}
return false;
} bool hungary(LL limit){
int u;
int res=;
memset(linker,-,sizeof(linker));
for(u=;u<=N;u++){
memset(used,,sizeof(used));
if(dfs(u,limit)) res++;
}
return res==N;
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T;
LL X,Y;
while(scanf("%d",&N)==){
init();
for(int i=;i<=N;++i)
scanf("%lld%lld",&s[i].x,&s[i].y);
for(int i=;i<=N;++i)
scanf("%lld%lld",&t[i].x,&t[i].y);
LL mx=-;
for(int i=;i<=N;++i){
for(int j=;j<=N;++j){
LL dist = abs(s[i].x-t[j].x)+abs(s[i].y-t[j].y);
AddEdge(i,j+N,dist);
AddEdge(j+N,i,dist);
mx = max(mx,dist);
}
}
LL L=,R=mx,mid;
while(R-L>){
mid = L+(R-L)/;
if(hungary(mid)) R = mid;
else L = mid;
}
printf("%lld\n",R);
}
return ;
}
GYM - 101490 J Programming Tutors (匈牙利+二分)的更多相关文章
- codeforces gym 100947 J. Killing everything dp+二分
J. Killing everything time limit per test 4 seconds memory limit per test 64 megabytes input standar ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- 【codeforces.com/gym/100240 J】
http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- Gym 100801 J. Journey to the “The World’s Start” DP+单调队列优化+二分
http://codeforces.com/gym/100801 题目大意:有从左到右有n个车站,有n-1种车票,第i种车票一次最多可以坐 i 站(1<=i<=n) 每种票有固定的价钱 ...
- kattis Programming Tutors 给游客与导游匹配(二分+二分图)
题目来源:https://vjudge.net/problem/Kattis-programmingtutors 题意: 有n个游客,n个导游,给出他们的坐标,问你怎么匹配可以使他们最大距离最小 题解 ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
随机推荐
- 【NLP】新词发现
http://www.csdn.net/article/2013-05-08/2815186 http://blog.csdn.net/yuyu2223/article/details/7725705 ...
- 浅谈AutoResetEvent的用法
using System;using System.Threading; namespace AutoResetEvent_Examples{ class MyMainClass { ...
- CSS3制作旋转导航
慕课网学习CSS3时,遇到个习题,觉得有必要总结学习下:CSS3制作旋转导航 慕课网习题地址:http://www.imooc.com/code/1883 示例及源码地址:http://codepen ...
- 回车(CR)换行(LF)的来历及区别
转自https://blog.csdn.net/lw370481/article/details/8229344 一.回车”(Carriage Return)和“换行”(Line Feed)起源 回车 ...
- android mock location
原理:用 setTestProviderLocation 设置模拟gps的位置 http://androidcookbook.com/Recipe.seam?recipeId=1229 http:// ...
- c#修改cpu主频
并不是真正能修改硬件,只是一个数据,能骗过部分程序检测,如英雄联盟必须达到3.0的主频才能使用录像功能,通过修改可以达到要求. 下面是代码: public enum RegValueKind { // ...
- Windows中压缩版的MySQL的安装、配置
本次笔记是根据mysql-8.0.13-winx64版本编写: 1.将下载的压缩包解压到自己想放的目录 2.右键计算机 -> 属性 -> 高级系统设置 -> 环境变量 -> 系 ...
- squee_spoon and his Cube VI---郑大校赛(求最长子串)
市面上最常见的魔方,是三阶魔方,英文名为Rubik's Cube,以魔方的发明者鲁比克教授的名字命名.另外,二阶魔方叫Pocket Cube,它只有2*2*2个角块,通常也就比较小:四阶魔方叫Reve ...
- DRF的版本、认证、权限
DRF的版本 版本控制是做什么用的, 我们为什么要用 首先我们要知道我们的版本是干嘛用的呢~~大家都知道我们开发项目是有多个版本的~~ 当我们项目越来越更新~版本就越来越多~~我们不可能新的版本出了~ ...
- 使用selenium实现简单网络爬虫抓取MM图片
撸主听说有个网站叫他趣,里面有个社区,其中有一项叫他趣girl,撸主点进去看了下,还真不错啊,图文并茂,宅男们自己去看看就知道啦~ 接下来当然就是爬取这些妹子的图片啦,不仅仅是图片,撸主发现里面的对话 ...