Algorithm: POJ 2749 & hdu 1815 Building Roads (2-sat + two points, good title)

Source: Internet
Author: User
Tags abs bool int size min

"The main effect of the topic"

There are n barns, there are two transfer stations S1 and S2, S1 and S2 are connected by a path. In order to make the two of any cow can have access to the road, it is now necessary to have each of the Barns connect a road to S1 or S2.

There is a grudge against each other in the barn, so they cannot connect their way to the same transit point. and b to the barn like each other, so their road must be linked to the same secondary station.

The length of the road is two points of Manhattan distance.

What is the maximum value of the distance between the smallest of any two sheds?

Ideas

Two days ago, when I saw this problem, I had no idea, and I looked at it again today.

Each barn has the choice of S1 or S2, and there are contradictions, is 2-sat no doubt.

First of all, the distance required by the two points is a must, the biggest problem is how to build the map?

We have enumerated the maximum distance between any of the sheds, indicating that if there are two of them, they choose to connect in a way that makes their distance greater than the maximum distance, and they certainly cannot choose this connection, which is a paradox, which can be added by the edge.

Then there is the mutual hatred and mutual love of the building side, this is easy to think of.

After writing in POJ submitted, found WA, but very sure that their algorithm is no problem, it is possible to think that the right edge of the two points is too small, and then changed to 400W, and then AC.

So again to Hdu to go, the result unexpectedly tle ... After a long time, found that the right side of the border opened small, so constantly increase, and finally to 800W AC. Right border small will tle, this let me think impassability, ask for advice

In addition, each point to S1 and S2 distance in advance, not every time the map is counted, this can save a lot of time

Code

#include <iostream> #include <queue> #include <cstdio> #include <cstring> #include <CMATH&G  
T  
    
using namespace Std;  
const int MAXN = 1005;  
const int VN = MAXN*2;  
    
const int EN = 1200000;  
int n, hatenum, Likenum;  
    
int D[VN], slen;  
struct node{int x, y;  
    
}BARN[MAXN], HATE[VN], LIKE[VN], S1, S2;  
struct edge{int V, next;  
    
};  
    struct graph{int size, HEAD[VN];  
    Edge E[en];   
        void Init () {size=0;   
    Memset (Head,-1, sizeof (head));  
        } void Addedge (int u, int v) {e[size].v = v;  
        E[size].next = Head[u];  
    Head[u] = size++;  
    
}}g;  
        Class two_sat{Public:bool Check (const graph& g, const int n) {SCC (g, 2*n);  
        for (int i=0; i<n; ++i) if (belong[i] = = Belong[i+n]) return false;  
    return true; } private:void Tarjan (const graph& g, const INT u) {int V;  
        Low[u] = dfn[u] = ++idx;  
        sta[top++] = u;  
    
        Instack[u] = true;  
            for (int e=g.head[u]; E!=-1 e=g.e[e].next) {v = g.e[e].v;  
                if (dfn[v] = =-1) {Tarjan (g, v);  
            Low[u] = min (Low[u], low[v]);  
            }else if (Instack[v]) {Low[u] = min (Low[u], dfn[v]);  
            } if (dfn[u] = = Low[u]) {++bcnt;  
                do{v = sta[--top];  
                INSTACK[V] = false;  
            BELONG[V] = bcnt;  
        }while (U!= v);  
        } void SCC (const graph& g, const int n) {bcnt = idx = top = 0;  
        memset (DFN,-1, sizeof (DFN));  
        memset (instack, 0, sizeof (instack));  
    for (int i=0; i<n; ++i) if (dfn[i] = = 1) Tarjan (g, I);  
    } private:int idx, top, bcnt; int DFN[VN], LOW[VN], BELONG[VN], STA[VN];  
BOOL INSTACK[VN];  
    
}sat;  
inline int dist (const node& A, const node& b) {return abs (a.x-b.x) +abs (A.Y-B.Y);  
       
    } void Buildgraph (int maxlen) {g.init ();  
            for (int i=0; i<n; ++i) for (int j=i+1; j<n; ++j) if (i!=j) {int l1=d[i], l2=d[i+n];  
            int r1=d[j], r2=d[j+n];  
                if (L1 + r1 > MaxLen) {g.addedge (I, j+n);  
            G.addedge (J, I+n);  
                } if (L1 + r2 + slen > MaxLen) {G.addedge (i, j);  
            G.addedge (J+n, i+n);  
                } if (L2 + r1 + Slen > MaxLen) {g.addedge (i+n, j+n);  
            G.addedge (J, I);  
                } if (L2 + r2 > MaxLen) {G.addedge (i+n, J);  
            G.addedge (J+n, i); for (int i=0; i<hatenum; ++i) {int a=hate[i].x, B=hate[i].y;  
        G.addedge (A, b+n);  
        G.addedge (a+n, b);  
        G.addedge (b, a+n);  
    G.addedge (B+n, a);  
        for (int i=0; i<likenum; ++i) {int a=like[i].x, b=like[i].y;  
        G.addedge (A, b);  
        G.addedge (A+n, b+n);  
        G.addedge (b, a);  
    G.addedge (B+n, a+n);  
            
        int main () {while (~scanf ("%d%d%d", &n, &hatenum, &likenum)) {  
        scanf ("%d%d%d%d", &s1.x,&s1.y,&s2.x,&s2.y);  
        
        Slen = dist (s1, S2);  
            for (int i=0; i<n; ++i) {scanf ("%d%d", &barn[i].x, &BARN[I].Y);  
            D[i] = dist (barn[i], S1);  
        D[i+n] = dist (barn[i], S2);      
            for (int i=0; i<hatenum; ++i) {scanf ("%d%d", &hate[i].x, &HATE[I].Y);  
            --hate[i].x;  
        --HATE[I].Y;  
  for (int i=0; i<likenum; ++i) {          scanf ("%d%d", &like[i].x, &LIKE[I].Y);  
            --like[i].x;  
        --LIKE[I].Y;  
        int l=0, r=8000000, Mid, ans=-1;  
            while (L <= r) {mid = (l+r) >>1;  
            Buildgraph (mid);    
                if (Sat.check (g, N)) {ans = mid;  
            R = mid-1;  
        else L = mid+1;  
    
    printf ("%d\n", ans);  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.