POJ 2749--Building roads(2-SAT)
题意: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)的更多相关文章
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- poj 2749 Building roads (二分+拆点+2-sat)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6229 Accepted: 2093 De ...
- poj 3625 Building Roads(最小生成树,二维坐标,基础)
题目 //最小生成树,只是变成二维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> ...
- 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 ...
- POJ 1947 Rebuilding Roads (树形DP)
题意:给一棵树,在树中删除一些边,使得有一个连通块刚好为p个节点,问最少需要删除多少条边? 思路: 因为任一条边都可能需要被删除,独立出来的具有p个节点的连通块可能在任意一处地方.先从根开始DFS,然 ...
- HDU1815 Building roads(二分+2-SAT)
Problem Description Farmer John's farm has N barns, and there are some cows that live in each barn. ...
- POJ 1947 Rebuilding Roads(树形DP)
题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j] ...
- [poj] 2749 building roads
原题 2-SAT+二分答案! 最小的最大值,这肯定是二分答案.而我们要2-SATcheck是否在该情况下有可行解. 对于目前的答案limit,首先把爱和恨连边,然后我们n^2枚举每两个点通过判断距离来 ...
- POJ 2749 Building roads 2-sat+二分答案
把爱恨和最大距离视为限制条件,可以知道,最大距离和限制条件多少具有单调性 所以可以二分最大距离,加边+check #include<cstdio> #include<algorith ...
- Java实现 POJ 2749 分解因数(计蒜客)
POJ 2749 分解因数(计蒜客) Description 给出一个正整数a,要求分解成若干个正整数的乘积,即a = a1 * a2 * a3 * - * an,并且1 < a1 <= ...
随机推荐
- Interface Serializable
public interface Serializable Serializability of a class is enabled by the class implementing the ja ...
- WebViewJavascriptBridge的基本原理
前言 WebViewJavascriptBridge是支持到iOS6之前的版本的,用于支持native的iOS与javascript交互.如果需要支持到iOS6之前的app,使用它是很不错的.本篇讲讲 ...
- [codility]Min-abs-sum
https://codility.com/demo/take-sample-test/delta2011/ 0-1背包问题的应用.我自己一开始没想出来.“首先对数组做处理,负数转换成对应正数,零去掉, ...
- QT插件开发方式(作者有RemOjbects文档翻译(48)篇)
创建一个QT的库项目,删除自动生成的.h和.cpp文件,添加一个接口定义.h文件和一个接口实现类(一个.h一个.cpp).代码如下: 1.接口文件源码 #ifndef PLUGININTERFACE_ ...
- POJ1328——Radar Installation
Radar Installation Description Assume the coasting is an infinite straight line. Land is in one side ...
- Foreman--Puppet类导入
一.Foreman环境: foreman建好后,系统默认创建了3个环境:production,development,common, 1. production: 在puppet.conf里已经定义其 ...
- (转)linux 技巧:使用 screen 管理你的远程会话
转自:http://www.ibm.com/developerworks/cn/linux/l-cn-screen/index.html 你是不是经常需要 SSH 或者 telent 远程登录到 Li ...
- 如何完全卸载VS2010
1.首先用360卸载,当卸载完成后,提示有残余的话,就强力清除 2,接着,下载IobitUninstaller工具 3.按照下面进行卸载 1.Microsoft .NET Framework 4 框架 ...
- 【HDOJ】2890 Longest Repeated subsequence
后缀数组的应用.和男人八题那个后缀数组差不多. /* 2890 */ #include <iostream> #include <sstream> #include <s ...
- POI 中Cell的backgroundcolor和foregroundcolor
刚开始以为要获得cell的背景色是使用 getFillBackgroundColor()这个函数(这里返回的是调色板的索引,要获得RGB需要先获得系统的Pallete,然后在获得 RGB).结果出来 ...