2-SAT入门 poj2749 Building roads

poj2749 Building roads

http://poj.org/problem?id=2749

二分答案,以后构造布尔表达式。

  相互讨厌是!((a and b)or(!a and !b) 化简得 (!a or !b)and(a or b)

  相互喜欢是!(a and !b)or(!a and b) 化简得 (!a or b)and(a or !b)

  然后枚举点对讨论一个点对和S1,S2相连会不会超过lim来添加限制。

    一共有四种情况都差不多,比如都连到S1会超过lim,就添加!(!a and !b) 化简得 (a or b)

构造表达式的时候可以从三个层次考虑,一个是直接写!()描述不允许出现的情况,然后化简。也可以考虑要排除什么情况,从什么变量取值使得子句为假出发,直接构造得到化简后的表达式。也可以从图上考虑,不通过dd_clause将子句化成蕴含的形式,直接考虑,谁向谁连边。比如相互喜欢就是四条边<a真,b真> <b假,a假><a假,b假><b真,a真>。

我觉得第一种比较好。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;

typedef pair<int,int> Point;
//点的标号从0开始到2n-1
const int maxn = 500 + 10 ;
const int maxm = 1000 + 10 ;
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 0;
        if(mark[x  ]) return 1;
        mark[x] = 1;
        S[c++]  = x;
        for (int i = 0; i < G[x].size(); ++i)
            if(!dfs(G[x][i])) return 0;
        return 1;
    }

    void init(int n){
        this->n = n;
        for (int i = 0; i < n*2; ++i)
            G[i].clear();
        memset(mark,0,sizeof(mark));
    }

    // x = xval or y = yval
    void add_clause(int x,int xval,int y,int yval){
        x = x * 2 + xval;
        y = y * 2 + yval;
        G[x^1].push_back(y);
        G[y^1].push_back(x);
    }

    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]] = 0;
                    if(!dfs(i+1))
                        return 0;
                }
            }
        return 1;
    }
};

TwoSAT TSAT;
Point S[3],P[maxn];
pair<int,int> hate[maxm],like[maxm];

int N,A,B,SS;

int dis(Point P,Point Q){return abs(P.first-Q.first)+abs(P.second-Q.second);}

bool ok(int lim){
    TSAT.init(N);
    for (int i = 0; i < A; ++i){
        TSAT.add_clause(hate[i].first,0,hate[i].second,0);
        TSAT.add_clause(hate[i].first,1,hate[i].second,1);
    }
    for (int i = 0; i < B; ++i){
        TSAT.add_clause(like[i].first,0,like[i].second,1);
        TSAT.add_clause(like[i].first,1,like[i].second,0);
    }
    for (int i = 0; i < N; ++i)
        for (int j = i+1; j < N; ++j){
            int i0 = dis(P[i],S[0]),
                i1 = dis(P[i],S[1]),
                j0 = dis(P[j],S[0]),
                j1 = dis(P[j],S[1]);
            if(i0+j0>lim)
                TSAT.add_clause(i,1,j,1);
            if(i1+j1>lim)
                TSAT.add_clause(i,0,j,0);
            if(i0+SS+j1>lim)
                TSAT.add_clause(i,1,j,0);
            if(i1+SS+j0>lim)
                TSAT.add_clause(i,0,j,1);
        }
    return TSAT.solve();
}

int main()
{

    Point S1,S2;
    while(cin>>N>>A>>B){
        cin>>S[0].first>>S[0].second>>S[1].first>>S[1].second;
        SS=dis(S[0],S[1]);
        for (int i = 0; i < N; ++i)
            cin>>P[i].first>>P[i].second;

        for (int i = 0; i < A; ++i){
            cin>>hate[i].first>>hate[i].second;
            hate[i].first--;
            hate[i].second--;
        }
        for (int i = 0; i < B; ++i){
            cin>>like[i].first>>like[i].second;
            like[i].first--;
            like[i].second--;
        }

        if(!ok(4000000)){
            cout<<-1<<endl;
            continue;
        }

        int l=0,r=4000000,mid=(l+r)>>1;
        for(;l<r-1;mid=(l+r)>>1)
            if(ok(mid))
                r=mid;
            else
                l=mid;

        cout<<r<<endl;
    }
    return 0;
}

2-SAT入门 poj2749 Building roads

时间: 2024-11-08 21:49:23

2-SAT入门 poj2749 Building roads的相关文章

[POJ2749]Building roads(2-SAT)

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 ro

POJ2749 Building roads

嘟嘟嘟 最近把21天漏的给不上. 今天重温了一下2-SAT,感觉很简单.就是把所有条件都转化成如果--必然能导出--.然后就这样连边建图,这样一个强连通分量中的所有点必然都是真或者假.从而根据这个点拆点后的两个点是否在一个强连通分量里判断是否有解. 这题人很容易想到拆点:\(i\)表示\(i\)连向\(s_1\),\(i + n\)表示\(i\)连向\(s_2\). 这道题关键在于距离这个限制.我们肯定能想到二分,但是接下来我就没想出来,因为2-SAT的图的边权是没有意义的.但实际上这个也是可以

hdu 1815 Building roads

Building roads Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 822    Accepted Submission(s): 239 Problem Description Farmer John's farm has N barns, and there are some cows that live in each b

[BZOJ1626][Usaco2007 Dec]Building Roads 修建道路

1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1730  Solved: 727 [Submit][Status][Discuss] Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场之间原本就有道路相连.

poj 3625 Building Roads

题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already conn

洛谷——P2872 [USACO07DEC]道路建设Building Roads

P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms

Building roads

Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 34 Accepted Submission(s): 13   Problem Description Farmer John's farm has N barns, and there are some cows that live in each barn.

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

HDU 1815, POJ 2749 Building roads 题目链接HDU 题目链接POJ 题意: 有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来. 为了使得任意牛棚两个都可以有道路联通,现在要让每个牛棚都连接一条路到S1或者S2. 有a对牛棚互相有仇恨,所以不能让他们的路连接到同一个中转站.还有b对牛棚互相喜欢,所以他们的路必须连到同一个中专站. 道路的长度是两点的曼哈顿距离. 问最小的任意两牛棚间的距离中的最大值是多少? 思路:二分距离,考虑每两个牛棚之间4种连

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

Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 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 ro