题意:有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 (匈牙利+二分)的更多相关文章

  1. codeforces gym 100947 J. Killing everything dp+二分

    J. Killing everything time limit per test 4 seconds memory limit per test 64 megabytes input standar ...

  2. 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 ...

  3. 【codeforces.com/gym/100240 J】

    http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...

  4. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  5. Gym 100801 J. Journey to the “The World’s Start” DP+单调队列优化+二分

    http://codeforces.com/gym/100801 题目大意:有从左到右有n个车站,有n-1种车票,第i种车票一次最多可以坐 i 站(1<=i<=n)   每种票有固定的价钱 ...

  6. kattis Programming Tutors 给游客与导游匹配(二分+二分图)

    题目来源:https://vjudge.net/problem/Kattis-programmingtutors 题意: 有n个游客,n个导游,给出他们的坐标,问你怎么匹配可以使他们最大距离最小 题解 ...

  7. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  8. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  9. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

随机推荐

  1. 【NLP】新词发现

    http://www.csdn.net/article/2013-05-08/2815186 http://blog.csdn.net/yuyu2223/article/details/7725705 ...

  2. Maven项目POM.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  3. 使用 Estimator 构建卷积神经网络

    来源于:https://tensorflow.google.cn/tutorials/estimators/cnn 强烈建议前往学习 tf.layers 模块提供一个可用于轻松构建神经网络的高级 AP ...

  4. java动态代理中的invoke方法是如何被自动调用的(转)

    一.动态代理与静态代理的区别. (1)Proxy类的代码被固定下来,不会因为业务的逐渐庞大而庞大: (2)可以实现AOP编程,这是静态代理无法实现的: (3)解耦,如果用在web业务下,可以实现数据层 ...

  5. Math.max得到数组中最大值

    Math.max(param1,param2) 因为参数不支持数组. 所以可以根据apply的特点来解决, var max = Math.max.apply(null,array),这样就可以轻易的得 ...

  6. 通过AnimationSet 同步或一部播放多个动画 Android 属性动画(Property Animation) 完全解析 (下)

    AnimationSet提供了一个把多个动画组合成一个组合的机制,并可设置组中动画的时序关系,如同时播放,顺序播放等. 以下例子同时应用5个动画: 播放anim1: 同时播放anim2,anim3,a ...

  7. deviceready has not fired after 5 seconds

    deviceready has not fired after 5 seconds 建议用手机连上电脑,用真机进行调试:

  8. cx_Oracle在sublime text里运行遇到 ImportError错误解决办法

    如果你装完cx_Oracle之后,命令行运行没错,但是在sublime text里运行, 就遇到这个错误: ImportError: dlopen(/Library/Python/2.7/site-p ...

  9. 技术宅之flappy bird 二逼鸟

    师雪坤和刘阳 风靡一时的虐心小游戏<Flappy Bird>,以玩法简单.难度超高著称,不过,最近这款让全世界玩家几欲怒摔手机的游戏,被两位中国技术宅设计的"玩鸟机器人" ...

  10. 160608、mysql距离函数st_distance

    随着近几年各类移动终端的迅速普及,在手机移动定位app中,附近的人,附近的地点功能十分常见,基于地理位置的服务(LBS)和相关应用也越来越多,而支撑这些应用的最基础技术之一,就是基于地理位置信息的处理 ...