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

HDU 1815, POJ 2749 Building roads

题目链接HDU

题目链接POJ

题意:

有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来。 为了使得任意牛棚两个都可以有道路联通,现在要让每个牛棚都连接一条路到S1或者S2。

有a对牛棚互相有仇恨,所以不能让他们的路连接到同一个中转站。还有b对牛棚互相喜欢,所以他们的路必须连到同一个中专站。

道路的长度是两点的曼哈顿距离。

问最小的任意两牛棚间的距离中的最大值是多少?

思路:二分距离,考虑每两个牛棚之间4种连边方式,然后根据二分的长度建立表达式,然后跑2-sat判断即可

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

const int MAXNODE = 505;

struct TwoSet {
	int n;
	vector<int> g[MAXNODE * 2];
	bool mark[MAXNODE * 2];
	int S[MAXNODE * 2], sn;

	void init(int tot) {
		n = tot * 2;
		for (int i = 0; i < n; i += 2) {
			g[i].clear();
			g[i^1].clear();
		}
		memset(mark, false, sizeof(mark));
	}

	void add_Edge(int u, int uval, int v, int vval) {
		u = u * 2 + uval;
		v = v * 2 + vval;
		g[u^1].push_back(v);
		g[v^1].push_back(u);
	}

	void delete_Edge(int u, int uval, int v, int vval) {
		u = u * 2 + uval;
		v = v * 2 + vval;
		g[u^1].pop_back();
		g[v^1].pop_back();
	}

	bool dfs(int u) {
		if (mark[u^1]) return false;
		if (mark[u]) return true;
		mark[u] = true;
		S[sn++] = u;
		for (int i = 0; i < g[u].size(); i++) {
			int v = g[u][i];
			if (!dfs(v)) return false;
		}
		return true;
	}

	bool solve() {
		for (int i = 0; i < n; i += 2) {
			if (!mark[i] && !mark[i + 1]) {
				sn = 0;
				if (!dfs(i)){
					for (int j = 0; j < sn; j++)
						mark[S[j]] = false;
					sn = 0;
					if (!dfs(i + 1)) return false;
				}
			}
		}
		return true;
	}
} gao;

const int N = 505;
int n, a, b;

struct Point {
	int x, y;
	void read() {
		scanf("%d%d", &x, &y);
	}
} s1, s2, p[N], A[N * 2], B[N * 2];

int dis(Point a, Point b) {
	int dx = a.x - b.x;
	int dy = a.y - b.y;
	return abs(dx) + abs(dy);
}

int g[N][N][4];

bool judge(int d) {
	gao.init(n);
	for (int i = 0; i < a; i++) {
		gao.add_Edge(A[i].x - 1, 0, A[i].y - 1, 0);
		gao.add_Edge(A[i].x - 1, 1, A[i].y - 1, 1);
	}
	for (int i = 0; i < b; i++) {
		gao.add_Edge(B[i].x - 1, 0, B[i].x - 1, 1);
		gao.add_Edge(B[i].x - 1, 0, B[i].y - 1, 1);
		gao.add_Edge(B[i].y - 1, 0, B[i].x -1 , 1);
		gao.add_Edge(B[i].y - 1, 0, B[i].y - 1, 1);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < i; j++) {
			if (g[i][j][3] > d)
				gao.add_Edge(i, 0, j, 1);
			if (g[i][j][2] > d)
				gao.add_Edge(i, 1, j, 0);
			if (g[i][j][1] > d)
				gao.add_Edge(i, 0, j, 0);
			if (g[i][j][0] > d)
				gao.add_Edge(i, 1, j, 1);
		}
	}
	return gao.solve();
}

int main() {
	while (~scanf("%d%d%d", &n, &a, &b)) {
		s1.read(); s2.read();
		for (int i = 0; i < n; i++) {
			p[i].read();
			for (int j = 0; j < i; j++) {
				g[i][j][0] = dis(p[i], s1) + dis(p[j], s1);
				g[i][j][1] = dis(p[i], s2) + dis(p[j], s2);
				g[i][j][2] = dis(p[i], s1) + dis(p[j], s2) + dis(s1, s2);
				g[i][j][3] = dis(p[i], s2) + dis(p[j], s1) + dis(s1, s2);
			}
		}
		for (int i = 0; i < a; i++) A[i].read();
		for (int i = 0; i < b; i++) B[i].read();
		int l = 0, r = 7777777;
		if (!judge(r)) printf("-1\n");
		else {
			while (l < r) {
				int mid = (l + r) / 2;
				if (judge(mid)) r = mid;
				else l = mid + 1;
			}
			printf("%d\n", l);
		}
	}
	return 0;
}
时间: 2024-12-10 09:31:41

HDU 1815, POJ 2749 Building roads(2-sat)的相关文章

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

Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 Description Farmer John's farm has N barns, and there are some cows that live in each barn. The cows like to drop around, so John wants to build some ro

poj 2749 Building roads(2-sat)

Description Farmer John's farm has N barns, and there are some cows that live in each barn. The cows like to drop around, so John wants to build some roads to connect these barns. If he builds roads for every pair of different barns, then he must bui

poj 2749 Building roads 2-sat

题意: 给n个村庄的坐标和两个特殊点s1,s2的坐标,现在要将每个村庄连到s1或s2上,使n个村庄间的最大距离最小. 分析: 二分任意两村庄间的最大距离,用2-sat判断该最大距离是否可行,二分的时候可以顺便记录答案,不用等最后区间为空时再输出l或l-1. 代码: //poj 2749 //sep9 #include <iostream> #include <vector> using namespace std; const int maxN=1024; const int ma

[poj] 2749 building roads

原题 2-SAT+二分答案! 最小的最大值,这肯定是二分答案.而我们要2-SATcheck是否在该情况下有可行解. 对于目前的答案limit,首先把爱和恨连边,然后我们n^2枚举每两个点通过判断距离来实现连边,然后跑2-SAT判断是否有可行解 O(n^2logn) 想起来和听起来都很难写,事实上还好吧- #include<cstdio> #include<algorithm> #include<stack> #include<cstring> #define

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

把爱恨和最大距离视为限制条件,可以知道,最大距离和限制条件多少具有单调性 所以可以二分最大距离,加边+check 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 #include<stack> 6 #define N 5010 7 #define INF 4000000 8 using namespace std; 9 int di

POJ 1947 Rebuilding Roads (树形DP)

题意:给一棵树,在树中删除一些边,使得有一个连通块刚好为p个节点,问最少需要删除多少条边? 思路: 因为任一条边都可能需要被删除,独立出来的具有p个节点的连通块可能在任意一处地方.先从根开始DFS,然后进行树DP,dp[t][i]表示在以t为根的子树中删除i个点需要删除多少条边.dp[t][n-p]有可能是答案了,但是这种仅考虑到从树上脱落掉部分子树,那么留下的连通块通常是与1号点(树根)相连的,那如果所需要的连通块是在某棵子树中呢?将所有可能的子树取出来,若该子树节点数>=p,那么就可以在该子

POJ 1947 Rebuilding Roads(树形DP)

题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j]-1,son是u的儿子节点.初始是将所有的儿子都断开,然后-1代表的是这个儿子我需要了,不断了. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream>

HDU 1102 &amp;&amp; POJ 2421 Constructing Roads (经典MST~Prim)

链接:http://poj.org/problem?id=2421  或   http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We

HDU 1816, POJ 2723 Get Luffy Out(2-sat)

HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 题意:N串钥匙.每串2把,仅仅能选一把.然后有n个大门,每一个门有两个锁,开了一个就能通过,问选一些钥匙,最多能通过多少个门 思路:二分通过个数.然后对于钥匙建边至少一个不选,门建边至少一个选,然后2-sat搞一下就可以. 一開始是按每串钥匙为1个结点,但是后面发现数据有可能一把钥匙,出如今不同串(真是不合