题意:John有n个牛棚,每个牛棚都住着一些牛,这些牛喜欢串门(drop around, 学到了。。。),所以John想要建几条路把他们连接起来。他选择的方法是建两个相连中转站,然后每个牛棚连接其中一个中转站就好啦。现在的问题是有一些牛相互憎恨,所以不能连同一个中转站,而又有一些牛相互喜欢,必须连同一个中转站(再次感叹,人不如牛。。),现在要你来建边,要求,任意两个牛棚的距离的最大距离最短。两点距离是指哈密顿距离。比如u, v连的是同一个中转站s1,距离就是dis(u,s1)+dis(v,s1) 如果连不同的中转站就是dis(u,s1)+dis(v,s2)+dis(u,v),题意真的好不清楚啊

输入就是每个牛棚的坐标的中转站的坐标,已经牛之间的憎恨和喜欢关系。

输出最小距离。不能输出-1。

题解:二分。。。然后符合要求的边建图,2-sat求解。建图时喜欢和讨厌都要建四条边,仔细读题。。。仔细建边。。。

//我真的想吐槽我以前用的输入挂啊,我特么从哪搞来的辣鸡读入。。。用一次错一次。。。。

这套题做的我心真累。。。没有特别难的。。。但是每一道都wa的想死。。。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <bitset>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define pk(x) printf("%d\n", x)
using namespace std;
#define PI acos(-1.0)
#define EPS 1E-6
#define clr(x,c) memset(x,c,sizeof(x))
typedef long long ll;
const int N = ;
const int M = ;
inline int Scan()
{
char ch = getchar();
int data = ;
while (ch < '' || ch > '') ch = getchar();
do {
data = data* + ch-'';
ch = getchar();
} while (ch >= '' && ch <= '');
return data;
}
struct Edge {
int from, to, next;
} edge[M];
int head[N];
int cntE;
void addedge(int u, int v) {
edge[cntE].from = u; edge[cntE].to = v; edge[cntE].next = head[u]; head[u] = cntE++;
} int dfn[N], low[N], idx;
int stk[N], top;
int in[N];
int kind[N], cnt; void tarjan(int u)
{
dfn[u] = low[u] = ++idx;
in[u] = true;
stk[++top] = u;
for (int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
else if (in[v]) low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
++cnt;
while () {
int v = stk[top--]; kind[v] = cnt; in[v] = false;
if (v == u) break;
}
}
} void init() {
cntE = ;
memset(head, -, sizeof head);
memset(dfn, , sizeof dfn);
memset(in, false, sizeof in);
idx = top = cnt = ;
} int ax[N], ay[N];
int bx[N], by[N];
int dis1[N], dis2[N];
int n, a, b;
int dis; int cal(int x1, int y1, int x2, int y2) {
return abs(x1-x2) + abs(y1-y2);
} bool ok(int x) {
init();
for (int i = ; i <= n; ++i) {
for (int j = i+; j <= n; ++j) {
if (dis1[i] + dis1[j] > x) addedge(i, n+j), addedge(j, n+i);
if (dis2[i] + dis2[j] > x) addedge(i+n, j), addedge(j+n, i);
if (dis1[i] + dis2[j] + dis > x) addedge(i, j), addedge(j+n, i+n);
if (dis2[i] + dis1[j] + dis > x) addedge(i+n, j+n), addedge(j, i);
}
}
for (int i = ; i < a; ++i) {
addedge(ax[i], ay[i] + n), addedge(ay[i] + n, ax[i]);
addedge(ay[i], ax[i] + n), addedge(ax[i] + n, ay[i]);
}
for (int i = ; i < b; ++i) {
addedge(bx[i], by[i]), addedge(by[i], bx[i]);
addedge(bx[i] + n, by[i] + n), addedge(by[i] + n, bx[i] + n);
} for (int i = ; i <= *n; ++i) if (!dfn[i]) tarjan(i); for (int i = ; i <= n; i++) if (kind[i] == kind[i + n]) return false;
return true;
} int main() {
int x1, y1, x2, y2;
int x, y;
while (~scanf("%d%d%d", &n, &a, &b)) {
x1 = Scan(); y1 = Scan(); x2 = Scan(); y2 = Scan();
dis = cal(x1, y1, x2, y2);
int maxn = ;
for (int i = ; i <= n; ++i) {
x = Scan(); y = Scan();
dis1[i] = cal(x, y, x1, y1);
dis2[i] = cal(x, y, x2, y2);
maxn = max(maxn, max(dis1[i], dis2[i]));
}
maxn = maxn * + dis;
for (int i = ; i < a; ++i) ax[i] = Scan(), ay[i] = Scan();
for (int i = ; i < b; ++i) bx[i] = Scan(), by[i] = Scan(); int l = , r = maxn;
int ans = -;
while (l <= r) {
int mid = (l+r) >> ;
if (ok(mid)) ans = mid, r = mid - ;
else l = mid + ;
}
printf("%d\n", ans);
}
return ;
}

POJ 2749--Building roads(2-SAT)的更多相关文章

  1. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...

  2. poj 2749 Building roads (二分+拆点+2-sat)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 De ...

  3. poj 3625 Building Roads(最小生成树,二维坐标,基础)

    题目 //最小生成树,只是变成二维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> ...

  4. POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

  5. POJ 1947 Rebuilding Roads (树形DP)

    题意:给一棵树,在树中删除一些边,使得有一个连通块刚好为p个节点,问最少需要删除多少条边? 思路: 因为任一条边都可能需要被删除,独立出来的具有p个节点的连通块可能在任意一处地方.先从根开始DFS,然 ...

  6. HDU1815 Building roads(二分+2-SAT)

    Problem Description Farmer John's farm has N barns, and there are some cows that live in each barn. ...

  7. POJ 1947 Rebuilding Roads(树形DP)

    题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j] ...

  8. [poj] 2749 building roads

    原题 2-SAT+二分答案! 最小的最大值,这肯定是二分答案.而我们要2-SATcheck是否在该情况下有可行解. 对于目前的答案limit,首先把爱和恨连边,然后我们n^2枚举每两个点通过判断距离来 ...

  9. POJ 2749 Building roads 2-sat+二分答案

    把爱恨和最大距离视为限制条件,可以知道,最大距离和限制条件多少具有单调性 所以可以二分最大距离,加边+check #include<cstdio> #include<algorith ...

  10. Java实现 POJ 2749 分解因数(计蒜客)

    POJ 2749 分解因数(计蒜客) Description 给出一个正整数a,要求分解成若干个正整数的乘积,即a = a1 * a2 * a3 * - * an,并且1 < a1 <= ...

随机推荐

  1. Pie Charts

    Default pie chart   The default pie chart with no options set. Source Code $.plot('#placeholder', da ...

  2. 如何定位Release 版本中程序崩溃的位置 ---利用map文件 拦截windows崩溃函数

    1       案例描述 作为Windows程序员,平时最担心见到的事情可能就是程序发生了崩溃(异常),这时Windows会提示该程序执行了非法操作,即将关闭.请与您的供应商联系.呵呵,这句微软的“名 ...

  3. 在WIN32 DLL中使用MFC

    最近用WIN32 DLL,为了方便要用到MFC的一些库,又不想转工程,就网上找了很多方法,发现没有详细的介绍,有的也行不通,现在成功在WIN32 DLL中使用了MFC,记录一下以防以后用到忘记 一.修 ...

  4. POJ2526+简单几何

    题意:给定的这些点是否有一个对称中心. PS:我写得有点啰嗦.. 就是把小的x和大的x进行匹配. #include<stdio.h> #include<algorithm> # ...

  5. poj 2100 Graveyard Design

    直接枚举就行了 #include<iostream> #include<stdio.h> #include<algorithm> #include<ioman ...

  6. java 读取文件中文乱码问题

    很少写java io的代码,今天整了一个发现 本地调试好好的,放到jmeter里就打印乱码.一番折腾,终于搞定~直接上代码: List<Order> orderList = new Arr ...

  7. mac 用 brew

    mac 下用   brew 安装插件, 有重复的不会再次安装,比xmap模式好

  8. Android应用启动画面

    原文地址: [Android]应用启动画面 - 空客的日志 - 网易博客 http://blog.163.com/da7_1@126/blog/static/104072678201291921028 ...

  9. JS中访问对象的属性

    方式一: 对象名.属性名;   方式二: 对象名["属性名"];   ★注意:方式二中,属性名以字符串的形式出现在方括号中,这意味着通过方式二访问属性的话,可以实现“动态访问对象的 ...

  10. mac tree命令

    mac下默认是没有 tree命令的,不过我们可以使用find命令模拟出tree命令的效果,如显示当前目录的 tree 的命令: $ find . -print | sed -e 's;[^/]*/;| ...