POJ2749 Building roads
嘟嘟嘟
最近把21天漏的给不上。
今天重温了一下2-SAT,感觉很简单。就是把所有条件都转化成如果……必然能导出……。然后就这样连边建图,这样一个强连通分量中的所有点必然都是真或者假。从而根据这个点拆点后的两个点是否在一个强连通分量里判断是否有解。
这题人很容易想到拆点:\(i\)表示\(i\)连向\(s_1\),\(i + n\)表示\(i\)连向\(s_2\)。
这道题关键在于距离这个限制。我们肯定能想到二分,但是接下来我就没想出来,因为2-SAT的图的边权是没有意义的。但实际上这个也是可以用2-SAT的思路解决的:比如\(dis(i, s_1) + dis(j, s_1) > mid\),那么就说明,如果\(i\)连了\(s_1\),\(j\)只能连\(s_2\),因此我们连边\((i, j + n)\)。按照这种思路把四种情况都写一遍就行了。
这题和板子还有一个区别就是,板子给的条件形如"如果\(i\)为真,则\(j\)为假",也就是说,每一点的真假已知。但这到题真假不定,所以都得讨论。也就是说,对于每一个讨厌和喜欢关系,都得连4条边。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 5e2 + 5;
const int maxe = 5e6 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, A, B;
struct Node
{
int x, y;
friend In int dis(Node& A, Node& B)
{
return abs(A.x - B.x) + abs(A.y - B.y);
}
}s[2], p[maxn], a[maxn << 1], b[maxn << 1];
int d[maxn << 1], DIS;
struct Edge
{
int nxt, to;
}e[maxe];
int head[maxn << 1], ecnt = -1;
In void addEdge(int x, int y)
{
e[++ecnt] = (Edge){head[x], y};
head[x] = ecnt;
}
In void build(int x)
{
for(int i = 1; i <= A; ++i)
{
int u = a[i].x, v = a[i].y;
addEdge(u, v + n), addEdge(v, u + n);
addEdge(u + n, v), addEdge(v + n, u);
}
for(int i = 1; i <= B; ++i)
{
int u = b[i].x, v = b[i].y;
addEdge(u, v), addEdge(u + n, v + n);
addEdge(v, u), addEdge(v + n, u + n);
}
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
if(i ^ j)
{
if(d[i] + d[j] > x)
addEdge(i, j + n), addEdge(j, i + n);
if(d[i + n] + d[j + n] > x)
addEdge(i + n, j), addEdge(j + n, i);
if(d[i] + d[j + n] + DIS > x)
addEdge(i, j), addEdge(j + n, i + n);
if(d[i + n] + d[j] + DIS > x)
addEdge(i + n, j + n), addEdge(j, i);
}
}
bool in[maxn << 1];
int dfn[maxn << 1], low[maxn << 1], cnt = 0;
int st[maxn << 1], top = 0, col[maxn << 1], ccol = 0;
In void dfs(int now)
{
dfn[now] = low[now] = ++cnt;
st[++top] = now; in[now] = 1;
for(int i = head[now], v; ~i; i = e[i].nxt)
{
if(!dfn[v = e[i].to])
{
dfs(v);
low[now] = min(low[now], low[v]);
}
else if(in[v]) low[now] = min(low[now], dfn[v]);
}
if(low[now] == dfn[now])
{
int x; ++ccol;
do
{
x = st[top--]; in[x] = 0;
col[x] = ccol;
}while(x ^ now);
}
}
In void init()
{
ecnt = -1;
for(int i = 1; i <= (n << 1); ++i)
dfn[i] = low[i] = col[i] = in[i] = 0, head[i] = -1;
cnt = top = ccol = 0;
}
In bool sat(int x)
{
init();
build(x);
for(int i = 1; i <= (n << 1); ++i) if(!dfn[i]) dfs(i);
for(int i = 1; i <= n; ++i)
if(col[i] == col[i + n]) return 0;
return 1;
}
int main()
{
n = read(), A = read(), B = read();
s[0].x = read(), s[0].y = read(), s[1].x = read(), s[1].y = read();
for(int i = 1; i <= n; ++i) p[i].x = read(), p[i].y = read();
for(int i = 1; i <= A; ++i) a[i].x = read(), a[i].y = read();
for(int i = 1; i <= B; ++i) b[i].x = read(), b[i].y = read();
DIS = dis(s[0], s[1]);
for(int i = 1; i <= n; ++i) d[i] = dis(p[i], s[0]), d[i + n] = dis(p[i], s[1]);
int L = 0, R = 6e6;
while(L < R)
{
int mid = (L + R) >> 1;
if(sat(mid)) R = mid;
else L = mid + 1;
}
write(L == 6e6 ? -1 : L), enter;
return 0;
}
POJ2749 Building roads的更多相关文章
- [POJ2749]Building roads(2-SAT)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8153 Accepted: 2772 De ...
- POJ2749 Building roads 【2-sat】
题目 Farmer John's farm has N barns, and there are some cows that live in each barn. The cows like to ...
- poj 3625 Building Roads
题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...
- poj 2749 Building roads (二分+拆点+2-sat)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6229 Accepted: 2093 De ...
- BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )
计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- Building roads
Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads
P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树
1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer J ...
随机推荐
- 华硕笔记本的U盘启动
开机以后有两种方式: 1:按住ESC键,在弹出的见面直接选择USB启动进入. 2:按F2进BLOS进入,在boot里面原则第一个,找到USB作为第一启动项,再按F10保存一下即可.
- element vue Array数组和Map对象的添加与删除
使用场景: 一个后台系统中, 管理员要配置自定义字段后台要生成id和title,其他角色要使用自定义字段的表单, 添加数据, 但是每个要填写的对象的id 和title都是无法固定的,因此页面显示的ti ...
- 如何用ABP框架快速完成项目(7) - 用ABP一个人快速完成项目(3) - 通过微服务模式而不是盖楼式来避免难度升级和奥卡姆剃刀原理
这节文章十分重要!十分重要!十分重要! 很多同学在使用ABP的过程中遇到很多问题, 花费了很多时间和精力都还无法解决, 就是卡在这节文章这里. Talk is cheap, just show ...
- 智能POS承接口碑点餐FAQ
1.一体机正餐后付(正餐6.0.0.8),口碑订单在商家后台无优惠(小票上有),但实收不等于订单金额. 原因:订单回流问题(线上线下数据没有及时回流造成的):造成这个问题的操作: 1.在线下加菜线上同 ...
- Redis常用命令【列表】
一.简介 基于Linked List实现,元素是字符串类型,列表头尾增删快,中间增删慢,增删元素是常态. 元素可以重复出现,最多包含2^32-1个元素. 二.命令 1.说明 1.1 B block 块 ...
- Spark数据倾斜及解决方案
一.场景 1.绝大多数task执行得都非常快,但个别task执行极慢.比如,总共有100个task,97个task都在1s之内执行完了,但是剩余的task却要一两分钟.这种情况很常见. 2.原本能够正 ...
- The log scan number (620023:3702:1) passed to log scan in database 'xxxx' is not valid
昨天一台SQL Server 2008R2的数据库在凌晨5点多抛出下面告警信息: The log scan number (620023:3702:1) passed to log scan in d ...
- js获取地址栏中的数据
window.location.href:设置或获取整个 URL 为字符串window.location.pathname:设置或获取对象指定的文件名或路径window.location.search ...
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
1.第一步,在本地数据库中建一个与服务器同名的数据库 2.第二步,右键源数据库,任务>导出数据,弹出导入导出提示框,点下一步继续 3.远程数据库操作,确认服务器名称(服务器地址).身份验证(输入 ...
- 你的MySQL服务器开启SSL了吗?SSL在https和MySQL中的原理思考
最近,准备升级一组MySQL到5.7版本,在安装完MySQL5.7后,在其data目录下发现多了很多.pem类型的文件,然后通过查阅相关资料,才知这些文件是MySQL5.7使用SSL加密连接的.本篇主 ...