poj 2749
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6091 | Accepted: 2046 |
Description
Clever John just had another good idea. He first builds two transferring point S1 and S2, and then builds a road connecting S1 and S2 and N roads connecting each barn with S1 or S2, namely every barn will connect with S1 or S2, but not both. So that every pair of barns will be connected by the roads. To make the cows don't spend too much time while dropping around, John wants to minimize the maximum of distances between every pair of barns.
That's not the whole story because there is another troublesome problem. The cows of some barns hate each other, and John can't connect their barns to the same transferring point. The cows of some barns are friends with each other, and John must connect their barns to the same transferring point. What a headache! Now John turns to you for help. Your task is to find a feasible optimal road-building scheme to make the maximum of distances between every pair of barns as short as possible, which means that you must decide which transferring point each barn should connect to.
We have known the coordinates of S1, S2 and the N barns, the pairs of barns in which the cows hate each other, and the pairs of barns in which the cows are friends with each other.
Note that John always builds roads vertically and horizontally, so the length of road between two places is their Manhattan distance. For example, saying two points with coordinates (x1, y1) and (x2, y2), the Manhattan distance between them is |x1 - x2| + |y1 - y2|.
Input
Next line contains 4 integer sx1, sy1, sx2, sy2, which are the coordinates of two different transferring point S1 and S2 respectively.
Each of the following N line contains two integer x and y. They are coordinates of the barns from the first barn to the last one.
Each of the following A lines contains two different integers i and j(1 <= i < j <= N), which represent the i-th and j-th barns in which the cows hate each other.
The same pair of barns never appears more than once.
Each of the following B lines contains two different integers i and j(1 <= i < j <= N), which represent the i-th and j-th barns in which the cows are friends with each other. The same pair of barns never appears more than once.
You should note that all the coordinates are in the range [-1000000, 1000000].
Output
Sample Input
4 1 1
12750 28546 15361 32055
6706 3887
10754 8166
12668 19380
15788 16059
3 4
2 3
Sample Output
53246
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack> using namespace std; const int MAX_N = ;
int N,M,A,B;
bool fri[MAX_N][MAX_N],hate[MAX_N][MAX_N];
int low[MAX_N * ],pre[MAX_N * ],cmp[MAX_N * ];
int dfs_clock,scc_cnt;
int x[MAX_N],y[MAX_N];
stack<int> S;
vector<int> G[ * MAX_N]; void dfs(int u) {
low[u] = pre[u] = ++dfs_clock;
S.push(u);
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(!pre[v]) {
dfs(v);
low[u] = min(low[u],low[v]);
} else if(!cmp[v]) {
low[u] = min(low[u],pre[v]);
}
} if(low[u] == pre[u]) {
++scc_cnt;
for(;;) {
int x = S.top(); S.pop();
cmp[x] = scc_cnt;
if(x == u) break;
}
}
} bool scc() {
dfs_clock = scc_cnt = ;
memset(cmp,,sizeof(cmp));
memset(pre,,sizeof(pre)); for(int i = ; i <= * N + ; ++i) if(!pre[i]) dfs(i); for(int i = ; i <= N + ; ++i) {
if(cmp[i] == cmp[N + i]) return false;
}
return true;
} int dis(int i,int j) {
return abs(x[i] - x[j]) + abs(y[i] - y[j]);
} void build(int x) {
for(int i = ; i <= * N + ; ++i) {
G[i].clear();
} for(int i = ; i <= N + ; ++i) {
for(int j = i + ; j <= N + ; ++j) {
if(fri[i][j]) {
G[i].push_back(j);
G[i + N].push_back(j + N);
G[j].push_back(i);
G[j + N].push_back(i + N);
}
if(hate[i][j]) {
G[i].push_back(j + N);
G[j].push_back(i + N);
G[j + N].push_back(i);
G[i + N].push_back(j);
} if(dis(i,) + dis(j,) > x) {
G[i].push_back(j + N);
G[j].push_back(i + N);
}
if(dis(i,) + dis(j,) > x) {
G[i + N].push_back(j);
G[j + N].push_back(i);
}
if(dis(i,) + dis(j,) + dis(,) > x) {
G[i + N].push_back(j + N);
G[j].push_back(i);
}
if(dis(i,) + dis(j,) + dis(,)> x) {
G[i].push_back(j);
G[j + N].push_back(i + N);
}
}
} }
void solve() {
int l = ,r = 12e6 + ; //printf("r = %d\n",r); while(l < r) {
int mid = (l + r) / ;
build(mid);
if(scc()) r = mid;
else l = mid + ;
}
build(l);
if(scc())
printf("%d\n",l);
else
printf("-1\n");
} int main()
{
//freopen("sw.in","r",stdin);
scanf("%d%d%d",&N,&A,&B);
for(int i = ; i <= N + ; ++i) {
scanf("%d%d",&x[i],&y[i]);
} for(int i = ; i <= A; ++i) {
int a,b;
scanf("%d%d",&a,&b);
hate[a + ][b + ] = ;
} for(int i = ; i <= B; ++i) {
int a,b;
scanf("%d%d",&a,&b);
fri[a + ][b + ] = ;
} solve(); return ;
}
poj 2749的更多相关文章
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- Java实现 POJ 2749 分解因数(计蒜客)
POJ 2749 分解因数(计蒜客) Description 给出一个正整数a,要求分解成若干个正整数的乘积,即a = a1 * a2 * a3 * - * an,并且1 < a1 <= ...
- poj 2749 Building roads (二分+拆点+2-sat)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6229 Accepted: 2093 De ...
- poj 2749 2-SAT问题
思路:首先将hate和friend建边求其次2-SAT问题,判断是否能有解,没解就输出-1,否则用二分枚举最大的长度,将两个barn的距离小于mid的看做是矛盾,然后建边,求2-SAT问题.找出最优解 ...
- [poj] 2749 building roads
原题 2-SAT+二分答案! 最小的最大值,这肯定是二分答案.而我们要2-SATcheck是否在该情况下有可行解. 对于目前的答案limit,首先把爱和恨连边,然后我们n^2枚举每两个点通过判断距离来 ...
- POJ 2749 Building roads 2-sat+二分答案
把爱恨和最大距离视为限制条件,可以知道,最大距离和限制条件多少具有单调性 所以可以二分最大距离,加边+check #include<cstdio> #include<algorith ...
- POJ 2749 2SAT判定+二分
题意:图上n个点,使每个点都与俩个中转点的其中一个相连(二选一,典型2-sat),并使任意两点最大 距离最小(最大最小,2分答案),有些点相互hata,不能选同一个中转点,有些点相互LOVE,必需选相 ...
- TTTTTTTTTTT POJ 2749 修牛棚 2-Sat + 路径限制 变形
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7019 Accepted: 2387 De ...
- [转] POJ图论入门
最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...
随机推荐
- System V共享内存区
要点 shell查看命令:ipcs -m 主要函数 #include <sys/shm.h> //oflag=IPC_CREAT|IPC_EXCL|0644组合 //创建一个内存共享区 i ...
- Nunit单元测试的使用
先建立一个需要测试的项目 安装nunit 通过nuget安装Install-Package Nunit 类前加[TestFixture] 要测试的方法前加[Test] using System; u ...
- 配置DB2的数据库ODBC连接
打开cmd窗口,输入db2cmd启动db2 clp窗口 输入db2 list node directory查看是否有数据库需要连接的节点 如果不存在,则 节点编目:db2 catalog tcpip ...
- jQuery的筛选选择器
基本筛选选择器 很多时候我们不能直接通过基本选择器与层级选择器找到我们想要的元素,为此jQuery提供了一系列的筛选选择器用来更快捷的找到所需的DOM元素.筛选选择器很多都不是CSS的规范,而是jQu ...
- spring与mysql整合数据源的配置
需要解决两点,数据源的配置交给spring完成,事务管理交个spring来管理. <context:property-placeholder location="classpath:c ...
- ORACLE-用户常用数据字典的查询使用方法
一.用户 查看当前用户的缺省表空间 SQL> select username,default_tablespace from user_users; USERNAME DEFAULT_TABLE ...
- android开发遇到SDK无法访问谷歌而安装不了的情况
遇到SDK无法访问谷歌而安装不了的情况 1.修改C:\Windows\System32\drivers\etc的HOSTS文件,添加 #google_android更新203.208.46.146 d ...
- epoll重要
EPOLL事件分发系统可以运转在两种模式下:Edge Triggered (ET).Level Triggered (LT). LT是缺省的工作方式,并且同时支持block和no-blocksocke ...
- PHP自定义函数使用外部变量
一般,php的自定义函数不能直接使用外部变量. 在php自定义函数中使用外部变量前,需要先使用global对外部变量进行声明. <?php $var = "hello World!& ...
- dev 激活没有权限问题
用管理员身份打开 Microsoft Visual Studio 2010-->Visual Studio Tools-->Visual Studio 命令提示(2010) 然后输入一下命 ...