HDU1815(二分+2-SAT)
Building roads
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1569 Accepted Submission(s): 490
Problem Description
Clever John just had another good idea. He first builds two transferring point S1 and S2, and then builds a road connecting S1 and S2 and N roads connecting each barn with S1 or S2, namely every barn will connect with S1 or S2, but not both. So that every pair of barns will be connected by the roads. To make the cows don't spend too much time while dropping around, John wants to minimize the maximum of distances between every pair of barns.
That's not the whole story because there is another troublesome problem. The cows of some barns hate each other, and John can't connect their barns to the same transferring point. The cows of some barns are friends with each other, and John must connect their barns to the same transferring point. What a headache! Now John turns to you for help. Your task is to find a feasible optimal road-building scheme to make the maximum of distances between every pair of barns as short as possible, which means that you must decide which transferring point each barn should connect to.
We have known the coordinates of S1, S2 and the N barns, the pairs of barns in which the cows hate each other, and the pairs of barns in which the cows are friends with each other.
Note that John always builds roads vertically and horizontally, so the length of road between two places is their Manhattan distance. For example, saying two points with coordinates (x1, y1) and (x2, y2), the Manhattan distance between them is |x1 - x2| + |y1 - y2|.
Input
Next line contains 4 integer sx1, sy1, sx2, sy2, which are the coordinates of two different transferring point S1 and S2 respectively.
Each of the following N line contains two integer x and y. They are coordinates of the barns from the first barn to the last one.
Each of the following A lines contains two different integers i and j(1 <= i < j <= N), which represent the i-th and j-th barns in which the cows hate each other.
The same pair of barns never appears more than once.
Each of the following B lines contains two different integers i and j(1 <= i < j <= N), which represent the i-th and j-th barns in which the cows are friends with each other. The same pair of barns never appears more than once.
You should note that all the coordinates are in the range [-1000000, 1000000].
Output
Sample Input
12750 28546 15361 32055
6706 3887
10754 8166
12668 19380
15788 16059
3 4
2 3
Sample Output
Source
思路:二分枚举最大值limit,然后重新构图,用2-SAT判定可行性。用Xi表示第i个牛棚连到S1,~Xi表示连到S2,检查每一个约束条件,构图:
1.hate关系的i,j Xi->~Xj ~Xi->Xj Xj->~Xi ~Xj->Xi
2.friend关系的i,j Xi->Xj ~Xi->~Xj Xj->Xi ~Xj->~Xi
接下来的也要检查,因为引入参数,就是多了约束条件了
这四种情况就是i,j到达对方的所有情况了
3.dist(i,S1)+dist(S1,j)>limit Xi->~Xj Xj->Xi
4.dist(i,S2)+dist(S2,j)>limit ~Xi->Xj ~Xj->Xi
5.dist(i,S1)+dist(S1,S2)+dist(S2,j)>limit Xi->Xj ~Xj->~Xi
5.dist(i,S2)+dist(S2,S1)+dist(S1,j)>limit ~Xi->~Xj Xj->Xi
然后求强连通分量判断Xi与~Xi是否在同一个连通分量中,是的话就有矛盾。
//2017-08-28
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath> using namespace std; const int N = ;
const int M = N*N*;
int head[N], rhead[N], tot, rtot;
struct Edge{
int to, next;
}edge[M], redge[M]; void init(){
tot = ;
rtot = ;
memset(head, -, sizeof(head));
memset(rhead, -, sizeof(rhead));
} void add_edge(int u, int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++; redge[rtot].to = u;
redge[rtot].next = rhead[v];
rhead[v] = rtot++;
} vector<int> vs;//后序遍历顺序的顶点列表
bool vis[N];
int cmp[N];//所属强连通分量的拓扑序 //input: u 顶点
//output: vs 后序遍历顺序的顶点列表
void dfs(int u){
vis[u] = true;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
if(!vis[v])
dfs(v);
}
vs.push_back(u);
} //input: u 顶点编号; k 拓扑序号
//output: cmp[] 强连通分量拓扑序
void rdfs(int u, int k){
vis[u] = true;
cmp[u] = k;
for(int i = rhead[u]; i != -; i = redge[i].next){
int v = redge[i].to;
if(!vis[v])
rdfs(v, k);
}
} //Strongly Connected Component 强连通分量
//input: n 顶点个数
//output: k 强连通分量数;
int scc(int n){
memset(vis, , sizeof(vis));
vs.clear();
for(int u = ; u < n; u++)
if(!vis[u])
dfs(u);
int k = ;
memset(vis, , sizeof(vis));
for(int i = vs.size()-; i >= ; i--)
if(!vis[vs[i]])
rdfs(vs[i], k++);
return k;
} int n, A, B, dis_s1[N], dis_s2[N], dis_s1_s2;
struct Point{
int x, y;
}point[N], s1, s2, hate[N], friends[N]; //input: 两个点
//output: 两点间距离
double distance(Point a, Point b){
return abs(a.x-b.x) + abs(a.y-b.y);
} bool check(int limit){
init();
// i 表示 i 连 s1, NOT i 表示 i 连 s2
for(int i = ; i < n; i++){
bool fg = true;
if(distance(point[i], s1) > limit){
add_edge(i, i+n);
fg = false;
}
if(distance(point[i], s2) > limit){
if(!fg)return false;
add_edge(i+n, i);
}
for(int j = i+; j < n; j++){
if(dis_s1[i] + dis_s1[j] > limit){
add_edge(i, j+n);// i -> s1, j -> s2
add_edge(j, i+n);// j -> s1, i -> s2
}
if(dis_s2[i] + dis_s2[j] > limit){
add_edge(i+n, j);// i -> s2, j -> s1
add_edge(j+n, i);// j -> s2, i -> s1
}
if(dis_s1[i] + dis_s1_s2 + dis_s2[j] > limit){
add_edge(i, j);// i -> s1, j -> s1
add_edge(j+n, i+n);// j -> s2, i -> s2
}
if(dis_s2[i] + dis_s1_s2 + dis_s1[j] > limit){
add_edge(i+n, j+n);// i -> s2, j -> s2
add_edge(j, i);// j -> s1, i -> s1
}
}
}
for(int i = ; i < A; i++){
int u = hate[i].x, v = hate[i].y;
add_edge(u, v+n);
add_edge(v+n, u);
add_edge(v, u+n);
add_edge(u+n, v);
}
for(int i = ; i < B; i++){
int u = friends[i].x, v = friends[i].y;
add_edge(u, v);
add_edge(v, u);
add_edge(u+n, v+n);
add_edge(v+n, u+n);
}
scc(*n);
for(int i = ; i < n; i++){
if(cmp[i] == cmp[i+n])
return false;
}
return true;
} int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputE.txt", "r", stdin);
while(cin>>n>>A>>B){
cin>>s1.x>>s1.y>>s2.x>>s2.y;
dis_s1_s2 = distance(s1, s2);
for(int i = ; i < n; i++){
cin>>point[i].x>>point[i].y;
dis_s1[i] = distance(point[i], s1);
dis_s2[i] = distance(point[i], s2);
}
for(int i = ; i < A; i++){
cin>>hate[i].x>>hate[i].y;
hate[i].x--;
hate[i].y--;
}
for(int i = ; i < B; i++){
cin>>friends[i].x>>friends[i].y;
friends[i].x--;
friends[i].y--;
}
//r 开小了HDU会TLE,ORZ。。。
int l = , r = , mid, ans = -;
while(l <= r){
mid = (l+r)/;
if(check(mid)){
ans = mid;
r = mid-;
}else l = mid+;
}
cout<<ans<<endl;
}
return ;
}
HDU1815(二分+2-SAT)的更多相关文章
- HDU1815 2-sat+二分
Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- hdu1815 2sat + 二分 + 建图
题意: 给你两个总部,s1 ,s2,和n个点,任意两点之间都是通过这个总部相连的,其中有一些点不能连在同一个总部上,有一些点可以连接在同一个总部上,总部和总部之间可以直接连接,就是假如a, ...
- HDU1815 Building roads(二分+2-SAT)
Problem Description Farmer John's farm has N barns, and there are some cows that live in each barn. ...
- 证明与计算(3): 二分决策图(Binary Decision Diagram, BDD)
0x01 布尔代数(Boolean algebra) 大名鼎鼎鼎的stephen wolfram在2015年的时候写了一篇介绍George Boole的文章:George Boole: A 200-Y ...
- Map Labeler POJ - 2296(2 - sat 具体关系建边)
题意: 给出n个点 让求这n个点所能建成的正方形的最大边长,要求不覆盖,且这n个点在正方形上或下边的中点位置 解析: 当然是二分,但建图就有点还行..比较难想..行吧...我太垃圾... 2 - s ...
- LA 3211 飞机调度(2—SAT)
https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...
- UVALive - 3211 (2-SAT + 二分)
layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true m ...
- hdu3715 2-sat+二分
Go Deeper 题意:确定一个0/1数组(size:n)使得满足最多的条件数.条件在数组a,b,c给出. 吐槽:哎,一水提,还搞了很久!关键是抽象出题目模型(如上的一句话).以后做二sat:有哪些 ...
- POJ 2749 2SAT判定+二分
题意:图上n个点,使每个点都与俩个中转点的其中一个相连(二选一,典型2-sat),并使任意两点最大 距离最小(最大最小,2分答案),有些点相互hata,不能选同一个中转点,有些点相互LOVE,必需选相 ...
随机推荐
- Linux下查看文档指令
1,cat直接查看 cat /etc/profile 2,more,less分页查看 less /etc/profile more /etc/profile less -N /etc/profile ...
- MySQL 并行复制(MTS) 从库更新的记录不存在实际却存在
目录 背景 版本 分析 测试 背景 开了并行复制的半同步从库SQL 线程报1032错误,异步复制从库没有报错,偶尔会出现这种 版本 mysql 5.7.16 redhat 6.8 mysql> ...
- Elasticsearch集群搭建及使用Java客户端对数据存储和查询
本次博文发两块,前部分是怎样搭建一个Elastic集群,后半部分是基于Java对数据进行写入和聚合统计. 一.Elastic集群搭建 1. 环境准备. 该集群环境基于VMware虚拟机.CentOS ...
- 在mac上安装xcode时 弹出需要关闭itunes的警告 解决办法
1 首先打开终端(在工具栏中:前往-->使用工具-->终端) 2 输入 ps -ef | grep iTunes 回车 501 300 207 0 11:58上午 ?? ...
- 06-02 Java值传递、数据加密
值传递: /* 思考题1:看程序写结果,然后分析为什么是这个样子的.并画图讲解.最后总结Java中参数传递规律. Java中的参数传递问题: 基本类型:形式参数的改变对实际参数没有影响.基本类型传递的 ...
- odoo配置界面设置字段默认值
转自国外牛人博客:http://ludwiktrammer.github.io/odoo/custom-settings-odoo.html Defining custom settings in O ...
- 在MVC3中修改KindEditor实现上传图片到指定文件夹
KindEditor编辑器默认上传的图片文件夹,是根据系统时间自动生成的,图片是自动上传到这些文件夹里面,无法选择.如果要上传图片到指定文件夹,像相册一样管理图片,则需要扩展KindEditor编辑器 ...
- chrome中Timeline的使用(译)
一.概括 Timeline面板包括以下四个部分: 控制面板.开始记录.停止记录.配置捕获信息: 概况.页面性能的整体概况: flame chart.直观展示cpu堆的情况.你能够看到三条虚线,蓝色的代 ...
- VMWare Workstation 11的安装
不多说,直接上干货! 说白了 VMWare Workstation是收费的! VMware Player 和 VirtualBox是免费的! 虚拟机软件可让你在一个操作系统上直接运行的多个不同的虚 ...
- PHP设计模式:观察者模式
示例代码详见https://github.com/52fhy/design_patterns 观察者模式 观察者模式(Observer)是对象的行为模式,又叫发布-订阅(Publish/Subscri ...