题意:有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. Amazon Web Services (目录)

    一.官方声明 AWS云全球服务基础设施区域列表 AWS产品定价国外区 AWS产品定价中国区 (注意!需要登陆账户才能查看) AWS产品费用预算 AWS区域和终端节点 二.计算 Amazon学习:如何启 ...

  2. Angular2 Observable 可观察对象

    可观察对象支持在应用中的发布者和订阅者之间传递消息.在需要进行事件处理,异步编程和处理多值的时候,可观察对象相对其他技术有显著的优点. 可观察对象是声明式的 —— 也就是说,虽然你定义了一个用于发布值 ...

  3. 论坛模块_版块管理_增删改查&实现上下移动

    论坛模块_版块管理1_增删改查 设计实体Forum.java public class Forum { private Long id; private String name; private St ...

  4. nginx基本配置与参数说明以及Nginx中的upstream轮询机制介绍

    转自:http://blog.csdn.net/happydream_c/article/details/54943802 一.nginx简介 Nginx (发音为[engine x])专为性能优化而 ...

  5. Servlet------>jsp输出JavaBean

    JavaBean是遵循特殊写法的java类 它通常具有如下特点: 1.这个java类必须具有一个无参的构造函数 2.属性必须私有化 3.私有化必须通过public类暴露给其他程序,而且方法的命名必须遵 ...

  6. 模拟Push和Pop动作

    //利用CATransition来作模拟 //模拟Push - (void)pushView:(UIView *)pushView inView:(UIView *)inView { CATransi ...

  7. A Secure Cookie Protocol 安全cookie协议 配置服务器Cookie

    Title http://www.cse.msu.edu/~alexliu/publications/Cookie/cookie.pdf AbstractCookies are the primary ...

  8. mysql 标点符号

    w攻防一体化.

  9. echarts学习心得1---模块化单文件引入和标签式单文件引入

    一.模块化单文件引入 1. 为ECharts准备一个具备大小(宽高)的Dom(当然可以是动态生成的) <div id="main" style="height:40 ...

  10. 第六周小组作业 软件测试与评估:百词斩VS扇贝单词

    被测产品说明: A:百词斩 B:扇贝单词 一.基本任务 1.测试进度表 | 项目 | 内容说明 | 预估耗时(分钟) | 实际耗时 (分钟) | | -------------- | -------- ...