Poj2749 Building Roads

原创 2016年06月01日 21:24:59

有N个点,每个点向S1或S2连边。有一些恩怨情仇限制某些对点不能同时连向同一点,而某些对点必须同时连向同一点。每个点到S1和S2都有距离,我们要求可行方案中最大的两点间通过所连的S1(S2)到达彼此的距离最小是多少。

除了恩怨情仇外的限制再加上二分出的距离的限制来做2-SAT判定就好了。

#include<cmath>
#include<stack>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;

struct edge {
    int x,y,next;
    edge(){}
    edge(int _x,int _y,int _nt):x(_x),y(_y),next(_nt){}
} e[ 1000005 ];
int head[2000],tot = 0;
inline void addedge(int x,int y) {
    e[++tot] = edge(x,y,head[x]); head[x] = tot;
}
bool ins[2000];
int inc[2000],dfn[2000],low[2000],SCC,T;
stack<int>sta;
void tarjan(int x){
    dfn[x] = low[x] = ++T;
    sta.push(x); ins[x] = 1;
    for (int i = head[x]; i; i = e[i].next) {
        int y = e[i].y;
        if( ! dfn[y] ) {
            tarjan(y);
            low[x] = min(low[x], low[y]);
        }else if( ins[y] && dfn[y] < low[x]) low[x] = dfn[y];
    }
    if( low[x] == dfn[x] ){
        SCC++;
        while(1) {
            int y = sta.top(); sta.pop(); ins[y] = 0;
            inc[y] = SCC;
            if (x == y ) break;
        }
    }
}
struct point {
    int x,y;
    point(){}
    point(int _x,int _y):x(_x),y(_y){}
} a[2005],s[3];
int dis(point a,point b) {
    return abs(a.x-b.x) + abs(a.y-b.y);
}
int N,A,B;
int e1[2005][2],e2[2005][2];
int d1[2005],d2[2005],d12;
bool judge(int d) {
    memset(head,0,sizeof head);
    tot = 0;

    for (int i = 1; i <= A; i++) {
        addedge(e1[i][0],   e1[i][1]+N);
        addedge(e1[i][0]+N, e1[i][1]);
        addedge(e1[i][1],   e1[i][0]+N);
        addedge(e1[i][1]+N, e1[i][0]);
    }
    for (int i = 1; i <= B; i++) {
        addedge(e2[i][0],   e2[i][1]);
        addedge(e2[i][0]+N, e2[i][1]+N);
        addedge(e2[i][1],   e2[i][0]);
        addedge(e2[i][1]+N, e2[i][0]+N);
    }
    for (int i = 1; i <= N; i++) for (int j = i+1; j <= N; j++) {
        if ( d1[i] + d1[j] > d ) {
            addedge(i,j+N);         
            addedge(j,i+N);
        }
        if ( d2[i] + d2[j] > d ) {
            addedge(i+N,j);
            addedge(j+N,i);
        } 
        if ( d1[i] + d12 + d2[j] > d ) {
            addedge(i,j); addedge(j+N,i+N);
        }
        if ( d2[i] + d12 + d1[j] > d ) {
            addedge(i+N,j+N); addedge(j,i);
        } 
    }
    SCC = 0; T = 0;
    memset(ins,0,sizeof ins);
    memset(dfn,0,sizeof dfn);
    for (int i = 1; i <= N<<1; i++) if( ! dfn[i] ) tarjan(i);
    for (int i = 1; i <= N; i++)
        if( inc[i] == inc[i+N] ) return 0;
    return 1;
}
int main() {
    scanf("%d%d%d",&N,&A,&B);
    scanf("%d%d%d%d",&s[1].x,&s[1].y,&s[2].x,&s[2].y);
    int L = 0x3f3f3f3f,R = -1 ,mid,ans;
    d12 = dis(s[1],s[2]);
    for (int i = 1; i <= N; i++) {
        scanf("%d%d",&a[i].x,&a[i].y);
        d1[i] = dis(a[i], s[1]);
        d2[i] = dis(a[i], s[2]);
        L = min(L,min(d1[i],d2[i]));
        R = max(R,max(d1[i],d2[i]));
    }
    L <<= 1, (R<<=1)+= d12, ans = 0x3f3f3f3f;
    for (int i = 1; i <= A; i++) scanf("%d%d",&e1[i][0],&e1[i][1]);
    for (int i = 1; i <= B; i++) scanf("%d%d",&e2[i][0],&e2[i][1]);
    while( L <= R) {
        int mid = (L + R) >> 1;
        if( judge(mid) ) ans = mid,R=mid-1;
        else L = mid + 1;
    }
    if( ans == 0x3f3f3f3f ) ans = -1;
    printf("%d\n",ans);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Poj 2749 & Hdu 1815 Building roads (2-SAT + 二分 建图)

以下分析摘自:http://www.cnblogs.com/wally/p/3251968.html 题意:给出n个牛棚、两个特殊点S1,S2的坐标。S1、S2直连。牛棚只能连S1或S2,还有,某些...

POJ2749——Building roads

Description Farmer John's farm has N barns, and there are some cows that live in each barn. The cow...

POJ 2749|Building roads|2-SAT|二分答案

#include #include #include #include using namespace std; #define rep(i,j,k) for(i=j;i

2-SAT——6.0(poj2749 Building roads)

poj2749 Building roads 题意是说有 N 个牛栏,现在通过一条通道(s1,s2)把他们连起来,他们之间有一些约束关系,一些牛栏不能连在同一个点,一些牛栏必须连在同一个点,现在问有...

poj 2749 Building roads #二分+2-sat

/** poj 2749 Building roads #二分+2-sat 坐标系中有n个点和两个点s1,s2,每个点连到s1或s2上,连且只连一个, 求使得任意两点的曼哈顿距离中最大值 最...

poj 2749 Building roads 2-SAT

同样我们二分距离,我们可以这样考虑,对每个仓库a用a表示到第一个结合点连边,非a到第二个转接点连边,接下来我们对于相互恨的和相互喜欢的可以直接加边,对于约束条件,我们暴力每两个仓库,判断这两个仓库分别...

Poj 2749 Building roads【二分+2-Sat----------Tarjan强连通】

Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissio...

poj2749 Building roads

Description Farmer John's farm has N barns, and there are some cows that live in each barn. The c...

POJ_2749 Building roads 2-Sat

http://poj.org/problem?id=2749 题意: 有N个牛棚,2个中转站,每个牛棚只能选择一个中转站并连接到该中转站,求任意 两个在同一个中转站中的牛棚之间的最大距离的最小值...

POJ--2749[Building roads] 2-SAT第五题,2—SAT+二分判定

题目描述:有n个农场,每个农场有坐标x,y。有两个集合点s1和s2(也有坐标),每个农场必须连接其中的一个(有且仅有一个)。然后有A个条件,每个条件a,b表示a农场不能和b农场连接在一个集合点。然后再...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Poj2749 Building Roads
举报原因:
原因补充:

(最多只允许输入30个字)