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,必需选相 ...
随机推荐
- 【文文殿下】【洛谷】分治NTT模板
题解 可以计算每一项对后面几项的贡献,然后考虑后面每一项,发现这是一个卷积,直接暴力NTT就行了,发现它是一个有后效性的,我们选择使用CDQ分治. Tips:不能像通常CDQ分治一样直接 每次递归两边 ...
- D14——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D14 20180919内容纲要: 1.html认识 2.常用标签 3.京东html 4.小结 5.练习(简易淘宝html) 1.html初识(HyperText ...
- 在MySQL Workbench查看表,表结构,索引,函数,存储过程,触发器,重连
表 表结构 索引 触发器 存储过程 函数 重新连接 出现Error Code: 2006 MySQL server has gone away时
- 初识SQL语句
SQL(Structured Query Language ) 即结构化查询语言 SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,SQL语言由IBM开发.SQL语言分为3种类型: ...
- win10关机之后自动重启(系统更新之后出现这个问题)
最近更新了一把win10之后出现无法关机,关机之后直接又开机,无限循环状态.最近几天没空处理一直是强关笔记本下班的. 今天打了一把命令: shutdown /s /t 0 发现关机正常,本来打算整个脚 ...
- 微信端支付宝支付,iframe改造,解决微信中无法使用支付宝付款和弹出“长按地址在浏览器中打开”
微信对支付宝的链接屏蔽了, https://mapi.alipay.com/gateway.do?_input_charset=utf-8¬ify_url=http%3A%2F%2Fzh ...
- eclipse下搭建shell脚本编辑器--安装开发shell的eclipse插件shelled
具体请看: 亲测有效: http://www.cnblogs.com/shellshell/p/6122811.html
- vue中请求本地的json数据
为什么要请求本地的数据?模拟后台的请求数据,验证页面的逻辑是否存在问题,抛开后台提前开发等. 常用的说来有:jq的方式 约等于 axios的方式,vuex状态管理的方式 个人认为最好用的就是jq的方式 ...
- C# 多线程八之并行Linq(ParallelEnumerable)
1.简介 关于并行Linq,Ms官方叫做并行语言集成(PLINQ)查询,其实本质就是Linq的多线程版本,常规的Linq是单线程的,也就是同步的过程处理完所有的查询.如果你的Linq查询足够简单,而且 ...
- Vue + Element UI 实现权限管理系统 前端篇(九):接口格式定义
接口请求格式定义 前台显示需要后台数据,我们这里先把前后端交互接口定义好,没有后台的时候,也方便用mock模拟. 接口定义遵循几个规范: 1. 接口按功能模块划分. 系统登录:登录相关接口 用户管理: ...