POJ 2749 || HDU 1815 Building roads 2-sat
2016-10-09 20:15
183 查看
题目:
http://poj.org/problem?id=2749题意:
有两个中转站并给出坐标,有n个牛舍并给出n个坐标,要把n个牛舍连接到两个中转站上使任意两个牛舍想通。每个牛舍里有一头牛,然后有A对牛之间是敌对关系,着意味着他们不能连接在同一个中转站上,有B对牛之间是朋友关系,意味着他们必须连在同一个中转站上,问满足上述条件的基础上,任意两个牛舍的最远距离最小是多少思路:
最小化最大距离,二分枚举距离,然后2-sat判定是否可行。对于敌对关系的牛,意味着两只牛不能连接到同一个中转站,也不能同时不连接到同一个中转站(此时说明两只牛同时连接到另外一个中转站),即连边(i,~j)(j,~i)(~i,j)(~j,i)。对于朋友关系的牛,必须在同一个中转站,即连边(i,j)(j,i)(~i,~j)(~j,~i)。然后因为要满足任意两个牛舍之间的距离小于枚举值,所以对任意两个牛舍,如果距离大于枚举值,那么不能共存,即(i,~j)(j,~i)。另外注意枚举距离时的右界,虽然点坐标的范围是[-1000000, 1000000],最大距离貌似是根号2乘以1000000,但是我们计算过程最大距离是这种情况:i-s1,s1-s2,s2-j,此时最大距离应该是乘以3的。。。#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> using namespace std; const int N = 1010, INF = 0x3f3f3f3f; const double eps = 1e-8; struct edge { int to, next; } g[N*N*2]; int cnt, head ; int dfn , low , scc , st , top, num, idx; int x , y , a , b , c , d , dis[N*2]; bool vis ; int n, m, k, len; void add_edge(int v, int u) { g[cnt].to = u, g[cnt].next = head[v], head[v] = cnt++; } void init() { memset(head, -1, sizeof head); memset(dfn, -1, sizeof dfn); memset(vis, 0, sizeof vis); top = num = idx = cnt = 0; } void tarjan(int v) { dfn[v] = low[v] = ++idx; vis[v] = true, st[top++] = v; int u; for(int i = head[v]; i != -1; i = g[i].next) { u = g[i].to; if(dfn[u] == -1) { tarjan(u); low[v] = min(low[v], low[u]); } else if(vis[u]) low[v] = min(low[v], dfn[u]); } if(dfn[v] == low[v]) { num++; do { u = st[--top], vis[u] = false, scc[u] = num; } while(u != v); } } bool solve(int mid) { init(); for(int i = 1; i <= m; i++) { add_edge(a[i], b[i]+n), add_edge(b[i], a[i]+n); add_edge(a[i]+n, b[i]), add_edge(b[i]+n, a[i]); } for(int i = 1; i <= k; i++) { add_edge(c[i], d[i]), add_edge(d[i], c[i]); add_edge(c[i]+n, d[i]+n), add_edge(d[i]+n, c[i]+n); } for(int i = 1; i <= n; i++) for(int j = i + 1; j <= n; j++) { if(dis[i] + dis[j] > mid) add_edge(i, j+n), add_edge(j, i+n); if(dis[i+n] + dis[j+n] > mid) add_edge(i+n, j), add_edge(j+n, i); if(dis[i+n] + dis[j] + len > mid) add_edge(i+n, j+n), add_edge(j, i); if(dis[i] + dis[j+n] + len > mid) add_edge(i, j), add_edge(j+n, i+n); } for(int i = 1; i <= 2*n; i++) if(dfn[i] == -1) tarjan(i); for(int i = 1; i <= n; i++) if(scc[i] == scc[i+n]) return false; return true; } int main() { while(~ scanf("%d%d%d", &n, &m, &k)) { int sx1, sy1, sx2, sy2; scanf("%d%d%d%d", &sx1, &sy1, &sx2, &sy2); for(int i = 1; i <= n; i++) scanf("%d%d", &x[i], &y[i]); for(int i = 1; i <= m; i++) scanf("%d%d", &a[i], &b[i]); for(int i = 1; i <= k; i++) scanf("%d%d", &c[i], &d[i]); for(int i = 1; i <= n; i++) dis[i] = abs(x[i] - sx1) + abs(y[i] - sy1), dis[i+n] = abs(x[i] - sx2) + abs(y[i] - sy2); len = abs(sx1 - sx2) + abs(sy1 - sy2); int l = 0, r = 8000000, res = -1; while(l <= r) { int mid = (l + r) >> 1; if(solve(mid)) r = mid - 1, res = mid; else l = mid + 1; } printf("%d\n", res); } return 0; }
相关文章推荐
- POJ 2749 && HDU 1815 Building roads(2-SAT+二分)
- HDU 1815, POJ 2749 Building roads(2-sat)
- Poj 2749 & Hdu 1815 Building roads (2-SAT + 二分 建图)
- HDU 1815, POJ 2749 Building roads(2-sat)
- poj 2749 & hdu 1815 Building roads(2-SAT + 二分,好题)
- POJ_2749 Building roads 2-Sat
- POJ 2749 Building roads(2-SAT)
- HDU1815 Building roads(二分+2-SAT)
- poj 2749 Building roads 2-sat
- POJ 2749 Building roads (2-SAT)
- Poj 2749 Building roads【二分+2-Sat----------Tarjan强连通】
- poj 2749 Building roads 2-SAT
- poj 2749 Building roads(2-sat)
- HDU - 1815 Building roads (2-SAT)
- poj 2749 Building roads(2-sat+二分)
- poj 2749 Building roads 【2-sat + 二分】【建图较复杂】【好题】
- hdu 1815 Building roads(二分+2-sat判定)
- POJ 2749--Building roads(2-SAT)
- POJ 2749 Building roads 2-sat+二分答案
- |poj 2749|2-SAT|二分|Building roads