HDU1815 2-sat+二分

Building roads

Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30 Accepted Submission(s): 12
 

Problem 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

POJ Monthly - 2006.01.22 - zhucheng

题意:

有n个仓库,两个中转站s1,s2,要求每个农场要么和S1场地连接要么和S2场地连接,且每个农场之间的连接距离的最大值最小 ,有a对仓库不能连同一个中转站,b对仓库必须连同一个中转站。

代码:

/*
二分枚举最大距离L,判断一下每个农场可连接的场地(以下的连边表示,a表示和S1连接,!a表示和S2连接)
(前提是dis[a][s1/s2]<=L,dis[b][s1/s2]<=L......................)
如果dis[a][S1] + dis[b][S1] > L,那么表明a和b不能同时和S1连接,连边a -> !b, b->!a
如果dis[a][S2] + dis[b][S2] > L,那么表明a和b不能同时和S2连接,连边!a -> b, !b->a
如果dis[a][S1] + dis[b][S2] + dis[S1][S2] > L,那么表明a农场连接S1时,b农场不能连接S2。b农场连接S2时,a农场不能连接S1,连边 a->b, !b->!a
如果dis[a][S2] + dis[b][S1] + dis[S1][S2] > L,那么表明a农场连接S2时,b农场不能连接S1。b农场连接S1时,a农场不能连接S2,连边 !a->!b, b->a

接下来还要处理A中不可连接限制和B种连接限制.
注意:二分范围如果小了会wa的。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=505;
int dis[505][505],x[505],y[505],likx[1003],liky[1003],hatx[1003],haty[1003];
/********************** 2-sat模板 **********************/
struct Twosat{
    int n;
    vector<int> g[maxn*2];
    bool mark[maxn*2];
    int s[maxn*2],c;
    bool dfs(int x){
        if(mark[x^1]) return false;
        if(mark[x]) return true;
        mark[x]=true;
        s[c++]=x;
        for(int i=0;i<(int)g[x].size();i++)
            if(!dfs(g[x][i])) return false;
        return true;
    }
    void init(int n){
        this->n=n;
        for(int i=0;i<n*2;i++) g[i].clear();
        memset(mark,0,sizeof(mark));
    }
    void add_clause(int x,int xval,int y,int yval){//这个函数随题意变化
        x=x*2+xval;
        y=y*2+yval;
        g[x].push_back(y);
    }
    bool solve(){
        for(int i=0;i<n*2;i+=2)
            if(!mark[i]&&!mark[i+1]){
                c=0;
                if(!dfs(i)){
                    while(c>0) mark[s[--c]]=false;
                    if(!dfs(i+1)) return false;
                }
            }
        return true;
    }
};
/*********************** 2-sat模板 ************************/
int main(){
    int n,A,B,a,b;
    Twosat solver;
    while(~scanf("%d%d%d",&n,&A,&B)){
        scanf("%d%d%d%d",&x[n],&y[n],&x[n+1],&y[n+1]);
        for(int i=0;i<n;i++){
            scanf("%d%d",&x[i],&y[i]);
            dis[i][n]=dis[n][i]=(fabs(x[i]-x[n])+fabs(y[i]-y[n]));
            dis[i][n+1]=dis[n+1][i]=(fabs(x[i]-x[n+1])+fabs(y[i]-y[n+1]));
        }

        dis[n][n+1]=dis[n+1][n]=(fabs(x[n]-x[n+1])+fabs(y[n]-y[n+1]));
        for(int i=0;i<A;i++){
            scanf("%d%d",&a,&b);
            a--;b--;
            hatx[i]=a;haty[i]=b;
        }
        for(int i=0;i<B;i++){
            scanf("%d%d",&a,&b);
            a--;b--;
            likx[i]=a;liky[i]=b;
        }
        int L=0,R=8000006,M,ans=-1;
        while(L<=R){
            M=(L+R)/2;
            solver.init(n);
            for(int i=0;i<A;i++){
                solver.add_clause(hatx[i],0,haty[i],1);
                solver.add_clause(hatx[i],1,haty[i],0);
                solver.add_clause(haty[i],0,hatx[i],1);
                solver.add_clause(haty[i],1,hatx[i],0);
            }
            for(int i=0;i<B;i++){
                solver.add_clause(likx[i],0,liky[i],0);
                solver.add_clause(likx[i],1,liky[i],1);
                solver.add_clause(liky[i],0,likx[i],0);
                solver.add_clause(liky[i],1,likx[i],1);
            }
            for(int i=0;i<n;i++){
                //if(dis[i][n]>M) solver.add_clause(i,0,i,1);
                //if(dis[i][n+1]>M) solver.add_clause(i,1,i,0);
                for(int j=i+1;j<n;j++){
                    if(dis[i][n]<=M&&dis[j][n]<=M&&dis[i][n]+dis[j][n]>M){
                        solver.add_clause(i,0,j,1);
                        solver.add_clause(j,0,i,1);
                    }
                    if(dis[i][n+1]<=M&&dis[j][n+1]<=M&&dis[i][n+1]+dis[j][n+1]>M){
                        solver.add_clause(i,1,j,0);
                        solver.add_clause(j,1,i,0);
                    }
                    if(dis[i][n]<=M&&dis[j][n+1]<=M&&dis[i][n]+dis[j][n+1]+dis[n][n+1]>M){
                        solver.add_clause(i,0,j,0);
                        solver.add_clause(j,1,j,1);
                    }
                    if(dis[i][n+1]<=M&&dis[j][n]<=M&&dis[i][n+1]+dis[j][n]+dis[n][n+1]>M){
                        solver.add_clause(i,1,j,1);
                        solver.add_clause(j,0,i,0);
                    }
                }
            }
            if(solver.solve()) {R=M-1;ans=M;}
            else L=M+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-29 19:10:00

HDU1815 2-sat+二分的相关文章

LA 3211 飞机调度(2—SAT)

https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间为E,晚着陆时间为L,不得在其他时间着陆.你的任务是为这些飞机安排着陆方式,使得整个着陆计划尽量安全.换句话说,如果把所有飞机的实际着陆时间按照从早到晚的顺序排列,相邻两个着陆时间间隔的最小值. 思路: 二分查找最大值P,每次都用2—SAT判断是否可行. 1 #include<iostream>

hdu-----(1179)Ollivanders: Makers of Fine Wands since 382 BC.(二分匹配)

Ollivanders: Makers of Fine Wands since 382 BC. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 935    Accepted Submission(s): 523 Problem Description In Diagon Alley ,there is only one Wand-se

hdu3715 2-sat+二分

Go Deeper 题意:确定一个0/1数组(size:n)使得满足最多的条件数.条件在数组a,b,c给出. 吐槽:哎,一水提,还搞了很久!关键是抽象出题目模型(如上的一句话).以后做二sat:有哪些是点,哪些是条件,分清!,然后注意细节.这次居然因为里面一个小错误: 判断有无解的时候,i与i+1是否在一个SCC中的时候,i居然没有每次+2!而是++!傻X了...囧!还一直以为自己二分写错... #include<iostream> #include<cstdio> #includ

ZOJ 3717 Balloon (二分+2-sat)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3717 2-sat版题 对半径R进行二分,将二分得到的R用2-sat判,如果2R<dis(i,j),则建边add_and_zero(i,j),然后看是否有解 1 // #pragma comment(linker, "/STACK:102400000,102400000") 2 #include <cstdio> 3 #include <i

2—SAT问题

现有一个由N个布尔值组成的序列A,给出一些限制关系,比如A[x] AND A[y]=0.A[x] OR A[y] OR A[z]=1.A[x] XOR A[y]=0等,要确定A[0..N-1]的值,使得其满足所有限制关系.这个称为SAT问题,特别的,若每种限制关系中最多只对两个元素进行限制,则称为2-SAT问题. 对于x.y有11种关系,将其拆点2x(假),2x+1(真).对于x为假或者y为假这样的条件,我们连两条有向边2x+1->2y.2y+1->2x,注意这里是有向边以及边的起点和终点的关

hdu1179Ollivanders: Makers of Fine Wands since 382 BC. (二分最大匹配)

Problem Description In Diagon Alley ,there is only one Wand-seller,peeling gold letters over the door read Ollivanders: Makers of Fine Wands since 382 BC.A single wand lay on a faded purple cushion in the dusty window. A tinkling bell rang somewhere

UVALive - 3211 (2-SAT + 二分)

layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true mathjax: true tags: - 2-SAT - 图论 - 训练指南 Now or later UVALive - 3211 题意 n架飞机,每架可选择两个着落时间.安排一个着陆时间表,使得着陆间隔的最小值最大 题解 二分查找最大值P,每次都用2-SAT判断是否可行. #include<bits

2.1 二分分类

本周学习神经网络编程的基础知识 构建神经网络,有些技巧是非常重要 神经网络的计算过程中,通常有一个正向的过程(正向传播步骤),接着会有一个反向步骤(反向传播步骤), 为什么神经网络的计算可以分为前向传播和反向传播两个分开的过程?本周课程通过使用logistic回归来阐述,以便于能够更好的理解, logistic回归是一个用于二分分类的算法 比如有一个二分分类问题的例子, 假如有一张图像作为输入是这样的,你想输出识别此图的标签,如果是猫,输出1,如果不是,则输出0 使用y来表示输出的结果标签, 来

HDU3715(二分+2-SAT)

Go Deeper Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3184    Accepted Submission(s): 1035 Problem Description Here is a procedure's pseudocode: go(int dep, int n, int m)beginoutput the valu