Building roads
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8153   Accepted: 2772

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 build N * (N - 1) / 2 roads, which is so costly that cheapskate John will never do that, though that's the best choice for the cows.

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

The
first line of input consists of 3 integers N, A and B (2 <= N <=
500, 0 <= A <= 1000, 0 <= B <= 1000), which are the number
of barns, the number of pairs of barns in which the cows hate each other
and the number of pairs of barns in which the cows are friends with
each other.

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

You
just need output a line containing a single integer, which represents
the maximum of the distances between every pair of barns, if John
selects the optimal road-building scheme. Note if there is no feasible
solution, just output -1.

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

[Submit]   [Go Back]   [Status]   [Discuss]

考虑二分答案,然后根据题目给出的限制以及这个二分出来的距离限制建图,2-SAT解决。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define mem(a) memset(a,0,sizeof(a))
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define For(i,x) for (int i=h[x],k; i; i=nxt[i])
using namespace std; const int N=,M=N*N*;
int a,b,n,mx,ans,cnt,scc,top,L,R,tim,dis,x1,y1,x2,y2,x,y,dis1[N],dis2[N],ax[N],ay[N],bx[N],by[N];
int to[M],nxt[M],q[N],dfn[N],inq[N],h[N],low[N],bel[N];
void add(int u,int v){ to[++cnt]=v; nxt[cnt]=h[u]; h[u]=cnt; } void init(){ cnt=top=scc=tim=; mem(h); mem(dfn); mem(inq); } int cal(int x1,int y1,int x2,int y2){ return abs(x2-x1)+abs(y2-y1); } void tarjan(int x){
dfn[x]=low[x]=++tim; inq[x]=; q[++top]=x;
For(i,x) if (!dfn[k=to[i]]) tarjan(k),low[x]=min(low[x],low[k]);
else if (inq[k]) low[x]=min(low[x],dfn[k]);
if (dfn[x]==low[x]){
scc++; int t;
do { t=q[top--]; bel[t]=scc; inq[t]=; }while(t!=x);
}
} bool jud(int mid){
init();
rep(i,,n) rep(j,i+,n){
if (dis1[i]+dis1[j]>mid) add(i,n+j),add(j,n+i);
if (dis2[i]+dis2[j]>mid) add(i+n,j),add(j+n,i);
if (dis1[i]+dis2[j]+dis>mid) add(i,j),add(j+n,i+n);
if (dis2[i]+dis1[j]+dis>mid) add(i+n,j+n),add(j,i);
}
rep(i,,a) add(ax[i],ay[i]+n),add(ay[i]+n,ax[i]),add(ay[i],ax[i]+n),add(ax[i]+n,ay[i]);
rep(i,,b) add(bx[i],by[i]),add(by[i],bx[i]),add(bx[i]+n,by[i]+n),add(by[i]+n,bx[i]+n);
rep(i,,*n) if (!dfn[i]) tarjan(i);
rep(i,,n) if (bel[i]==bel[i+n]) return ;
return ;
} int main(){
freopen("poj2749.in","r",stdin);
freopen("poj2749.out","w",stdout);
while (~scanf("%d%d%d",&n,&a,&b)){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dis=cal(x1,y1,x2,y2); mx=;
rep(i,,n){
scanf("%d%d",&x,&y);
dis1[i]=cal(x,y,x1,y1); dis2[i]=cal(x,y,x2,y2);
mx=max(mx,max(dis1[i],dis2[i]));
}
mx=mx*+dis;
rep(i,,a) scanf("%d%d",&ax[i],&ay[i]);
rep(i,,b) scanf("%d%d",&bx[i],&by[i]);
int L=,R=mx; ans=-;
while (L<=R){
int mid=(L+R)>>;
if (jud(mid)) ans=mid,R=mid-; else L=mid+;
}
printf("%d\n",ans);
}
return ;
}

[POJ2749]Building roads(2-SAT)的更多相关文章

  1. POJ2749 Building roads

    嘟嘟嘟 最近把21天漏的给不上. 今天重温了一下2-SAT,感觉很简单.就是把所有条件都转化成如果--必然能导出--.然后就这样连边建图,这样一个强连通分量中的所有点必然都是真或者假.从而根据这个点拆 ...

  2. POJ2749 Building roads 【2-sat】

    题目 Farmer John's farm has N barns, and there are some cows that live in each barn. The cows like to ...

  3. poj 3625 Building Roads

    题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...

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

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

  5. BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )

    计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...

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

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

  7. Building roads

    Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  8. bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...

  9. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer J ...

随机推荐

  1. Spring整合Quartz分布式调度

    前言 为了保证应用的高可用和高并发性,一般都会部署多个节点:对于定时任务,如果每个节点都执行自己的定时任务,一方面耗费了系统资源,另一方面有些任务多次执行,可能引发应用逻辑问题,所以需要一个分布式的调 ...

  2. 【leetcode 简单】第四十题 求众数

    给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 ...

  3. VideoJS 与 Framework7 中 fastclick 冲突问题

    Framework7 由于自动启用  fastclick,会导致在 移动端下使用 video.js,控制条上的 播放和音量按钮 点击的时候会触发两次. 解决办法: 1. 全局禁用 fastclick, ...

  4. mysql安装后开启远程

    操作系统为centos7 64 1.修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1 这一行配置让 mysqld 启动时不对密码进行验证 2. ...

  5. spring boot注解学习记

    @Component Compent等效于xml文件中的Bean标注,Autowired自动初始化Bean是通过查找Component注解实现的,在增加Component后还是Autowired找不到 ...

  6. caffe Python API 之图片预处理

    # 设定图片的shape格式为网络data层格式 transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) ...

  7. Linux创建ftp并设置权限以及忘记ftp帐号(密码)修改

    忘记ftp密码修改方法: 1.登录服务器 cd  /etc/vsftpdcat ftpusers找到对应的ftp用户名 (如果用户名也忘记了 那么 cd /etc 然后cat passwd 查看用户和 ...

  8. csu 1503: 点到圆弧的距离

    1503: 点到圆弧的距离 Time Limit: 1 Sec  Memory Limit: 128 MB  Special JudgeSubmit: 614  Solved: 101[Submit] ...

  9. [前端随笔][JavaScript] 制作一个富文本编辑器

    写在前面 现在网上有很多现成的富文本编辑器,比如百度家的UEditor,kindeditor,niceditor等等,功能特别的多,API也很多,要去熟悉他的规则也很麻烦,所以想自己了解一下原理,做一 ...

  10. IE7、IE8下使用escape、encodeURI传递中文参数乱码的问题及解决方案

    js跳转到指定页面,一旦escape()中文数据,浏览器就会终止和没有反应.上网搜了半天始终不得解.一种说法是,escape中文之后,url中出现了%u,IE7和IE8拒绝执行.目前看来差不多是这样的 ...