TTTTTTTTTTT POJ 2749 修牛棚 2-Sat + 路径限制 变形
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 7019 | Accepted: 2387 |
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 题意:题意:给出n个牛棚、两个特殊点S1,S2的坐标。S1、S2直连。牛棚只能连S1或S2还有,某些牛棚只能连在同一个S,某些牛棚不能连在同一个S
求使最长的牛棚间距离最小 距离是曼哈顿距离
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=100+1000; struct Point{
int x,y;
void read()
{
scanf("%d %d",&x,&y);
}
}p[510];
vector<int> g[2*maxn],G[2*maxn];
stack<int> S;
Point s1,s2;
int n,a,b,u,v,scc_cnt,dfs_clock,pre[2*maxn],sccno[2*maxn],lowlink[2*maxn]; int dis(Point a,Point b)
{
return abs(a.x-b.x)+abs(a.y-b.y);
} void add_edgeG(int u,int v)
{
G[u].push_back(v);
} void add_edge(int u,int v)
{
g[u].push_back(v);
} void build(int mid)
{
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
int u=i<<1,v=j<<1;
if(dis(p[i],s1)+dis(s1,p[j])>mid)
{
add_edge(u,v^1);
add_edge(v,u^1);
}
if(dis(p[i],s2)+dis(s2,p[j])>mid)
{
add_edge(u^1,v);
add_edge(v^1,u);
}
if(dis(p[i],s1)+dis(s1,s2)+dis(s2,p[j])>mid)
{
add_edge(u,v);
add_edge(v^1,u^1);
}
if(dis(p[i],s2)+dis(s2,s1)+dis(s1,p[j])>mid)
{
add_edge(u^1,v^1);
add_edge(v,u);
}
}
} void tarjan(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
S.push(u);
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(!pre[v])
{
tarjan(v);
lowlink[u]=min(lowlink[v],lowlink[u]);
}
else if(!sccno[v])
lowlink[u]=min(lowlink[u],pre[v]);
} if(pre[u]==lowlink[u])
{
scc_cnt++;
for(;;)
{
int w=S.top();S.pop();
sccno[w]=scc_cnt;
if(w==u) break;
}
}
} bool find_scc(int mid)
{
for(int i=2;i<=2*n+1;i++) g[i].clear();
for(int u=2;u<=2*n+1;u++)
for(int i=0;i<G[u].size();i++)
g[u].push_back(G[u][i]);
build(mid); MM(lowlink,0);
MM(sccno,0);
MM(pre,0);
scc_cnt=dfs_clock=0;
for(int i=2;i<=2*n+1;i++)
if(!pre[i])
tarjan(i); for(int i=2;i<=2*n;i+=2)
{
if(sccno[i]==sccno[i^1])
return false;
}
return true;
} int main()
{
while(~scanf("%d %d %d",&n,&a,&b))
{
for(int i=2;i<=2*n+1;i++) G[i].clear();
s1.read();
s2.read();
for(int i=1;i<=n;i++) p[i].read(); for(int i=0;i<a;i++)
{
scanf("%d %d",&u,&v);
u<<=1;
v<<=1;
add_edgeG(u,v^1);
add_edgeG(v,u^1);
add_edgeG(u^1,v);
add_edgeG(v^1,u);
} for(int i=0;i<b;i++)
{
scanf("%d %d",&u,&v);
u<<=1;
v<<=1;
add_edgeG(u,v);
add_edgeG(v,u);
add_edgeG(u^1,v^1);
add_edgeG(v^1,u^1);
} int l=-1,r=5*1e6+100;
while(r-l>1)
{
int mid=(l+r)>>1;
if(find_scc(mid))
r=mid;
else l=mid;
}
if(r>5*1e6) printf("-1\n");
else printf("%d\n",r);
}
return 0;
}
分析:与模板题最大的不同就是要求在满足有连接方案存在的情况下,图中存在的最长简单路径尽可能短,处理方式如下:
1.因为是最长路径尽可能短,所以二分最大路径。
2.二分出一个mid值后,将每两个节点之间的四种组合的路径长度与mid比较,从而根据要让所有路径长度
都不能超过mid这一假设条件进行再次连边;
3.因为要多次连边,所以需要设置两个邻接表,一个记录根据喜欢与讨厌的关系保存边,另一个在前一个的基础上加上路径限制
TTTTTTTTTTT POJ 2749 修牛棚 2-Sat + 路径限制 变形的更多相关文章
- POJ 2594 Treasure Exploration(最小路径覆盖变形)
POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要 ...
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- Java实现 POJ 2749 分解因数(计蒜客)
POJ 2749 分解因数(计蒜客) Description 给出一个正整数a,要求分解成若干个正整数的乘积,即a = a1 * a2 * a3 * - * an,并且1 < a1 <= ...
- poj 2749
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6091 Accepted: 2046 De ...
- poj 3020 Antenna Placement(最小路径覆盖 + 构图)
http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- poj 2060 Taxi Cab Scheme (最小路径覆盖)
http://poj.org/problem?id=2060 Taxi Cab Scheme Time Limit: 1000MS Memory Limit: 30000K Total Submi ...
- POJ 3216 Repairing Company(最小路径覆盖)
POJ 3216 Repairing Company id=3216">题目链接 题意:有m项任务,每项任务的起始时间,持续时间,和它所在的block已知,且往返每对相邻block之间 ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
随机推荐
- Java小知识-----Map 按Key排序和按Value排序
Map排序的方式有很多种,这里记录下自己总结的两种比较常用的方式:按键排序(sort by key), 按值排序(sort by value). 1.按键排序 jdk内置的java.util包下的Tr ...
- sql sever 两数据表差异比较EXCEPT、INTERSECT
1.概念: EXCEPT主要是用来获取两个结果集的差:两个结果用EXCEPT链接,返回第一个结果集不在第二个结果集中的数据. INTERSECT主要是用来获取两个结果集的交集:两个结果用INTERSE ...
- windows10 AppStore安装 应用商店重新安装
点击左下角的搜索按钮,如下图所示 输入powershell,在结果中找到widows powershell应用,如下图所示 右键单击widows powershell应用,选择以管理员运行,如 ...
- 模板引擎StringTemplate和模板StringTemplateGroup的应用
博主很懒什么都没有留下,只留下了一个转载链接!!! http://www.cnblogs.com/Jerry-Chou/archive/2012/12/12/2814693.html
- 使用Python基于OpenCV的图像油画特效
算法步骤: 1.获取图像的灰度图片 2.设计一个小方框(4x4/8x8 /10x10等),统计每个小方框的像素值 3.将0-255的灰度值划分成几个等级,并把第二步处理的结果映射到所设置的各个等级中, ...
- python flask解决上传下载的问题
记录瞬间 最近为了解决一些新的需求,简单介入了flask对文件的上传和下载的方法,并分别使用python和curl模拟发送 代码: #! /usr/bin/env python3 # coding:u ...
- redis 学习(13)-- BitMap
BitMap 什么是 BitMap BitMap,即位图,其实也就是 byte 数组,用二进制表示,只有 0 和 1 两个数字. 如图所示: 重要 API 命令 含义 getbit key offse ...
- SQLServer 存储过程详解
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...
- Maven之私服配置
一.配置从私服下载 从私服下载主要是将 central 库的下载地址从https://repo1.maven.org/maven2/修改为私服地址,比如http://localhost:8081/re ...
- Django框架——基础之模板系统(template文件夹)
---恢复内容开始--- 1. 常用语法 需要记住两组特殊符号:{{ }} 和 {% %}. 在运用到变量的时候使用{{ }},如果是跟逻辑相关的话就使用{% %}. 在Django模板(t ...
还有,某些牛棚只能连在同一个S,某些牛棚不能连在同一个S