Algorithm: POJ 3683 priest John ' s busiest day (2-sat + topology output scheme)

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

"The main effect of the topic"

There is a small town where there are n couples who want to have a wedding ceremony, and every couple will ask the town priest to hold a ritual, but there is only one priest in town, and the priest can only do the ritual for a couple at a time.

Known as the starting T1 and the end time of each couple's wedding ceremony, they are held in the T2, which can only be held at the first d time of the wedding ceremony or in D before the end. Ask the priest if it is reasonable to arrange for the ceremony to be held by every couple.

Ideas

For each couple, they either do the ritual at the beginning or do the ritual at the end, so it's the 2-SAT model.

This problem is to output any scheme, studied for several hours ...

Code

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <qu  
    
eue> #define WHITE-1 #define RED 1 #define BLUE 0 using namespace std;  
    
typedef long long Int64;  
const int MAXN = 1010;  
const int VN = MAXN*2;  
const int EN = 1000010;  
    
int n;  
    struct couple{int D;  
int T1, T2;  
    
}ARR[MAXN];  
struct edge{int u, V, next;  
    
    
};  
void time_out (int t, int d) {printf ("%02d:%02d%02d:%02d\n", T/60, T%60, (T+d)/60, (t+d)%60);  
    } struct graph{int size;  
    int HEAD[VN];  
        
    Edge E[en];  
        void init () {size = 0;  
    Memset (Head,-1, sizeof (head));  
        } void Addedge (int u, int v) {e[size].u = u;  
        E[SIZE].V = v;  
        E[size].next = Head[u];  
    Head[u] = size++; }}g, G2; G is the original, G2 for the new figure 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;  
        } void Toposort (const graph&g, graph& g1, const int n) {g1.init ();  
        memset (indeg, 0, sizeof (INDEG));  
            for (int i=0; i<n; ++i) {Opp[belong[i]] = belong[i+n];  
        Opp[belong[i+n]] = belong[i];  
            for (int e=0; e<g.size; ++e) {int u = belong[g.e[e].u];  
            int v = belong[g.e[e].v];  
            if (U = v) continue;  
            ++indeg[u];  
        G1.addedge (V, u);  
        } queue<int>que;  
        memset (color, white, sizeof (color));  
    
        for (int i=1; i<=bcnt; ++i) if (!indeg[i)) Que.push (i);  
        int* head = G1.head; edge* E = G1.  
    
        E   
       while (!que.empty ()) {int u = que.front ();     Que.pop ();  
            if (Color[u]!= white) continue;  
            Color[u] = RED;  
    
            Color[opp[u]] = BLUE;  
                for (int e=head[u]; E!=-1 e=e[e].next) {int v = E[E].V;   
                if (--indeg[v] = = 0) {Que.push (v);  
                for (int i=0; i<n; ++i) {if (color[belong[i]]==red) {  
            Time_out (Arr[i].t1, ARR[I].D);  
            }else{time_out (ARR[I].T2-ARR[I].D, ARR[I].D);  
        }} private:void Tarjan (const graph& g, const int u) {int V;  
        Dfn[u] = low[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) {idx = top = bcnt = 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];  
    int LOW[VN];  
    int BELONG[VN];  
    int STA[VN];  
    
    BOOL INSTACK[VN];  
    Find solution int INDEG[VN];  
    int COLOR[VN];  
    
int OPP[VN];  
    
    
}sat; BOOL Iscross (int a1, int b1, int a2, int b2) {return! ( A2>=B1 | |   
A1>=B2);  
  int main () {int A, B;  while (~SCANF ("%d", &n)) {g.init ();  
            for (int i=0; i<n; ++i) {scanf ("%d:%d", &a, &b);  
            Arr[i].t1 = a*60+b;  
            scanf ("%d:%d", &a, &b);  
            Arr[i].t2 = a*60+b;  
        scanf ("%d", &ARR[I].D);  for (int i=0; i<n; ++i) {for (int j=i+1; j<n; ++j) {Const couple&  
                A=arr[i];  
                Const couple& B=ARR[J];  
                    if (Iscross (A.t1, A.T1+A.D, B.t1, B.T1+B.D)) {G.addedge (I, j+n);  
                G.addedge (J, I+n);  
                    } if (Iscross (A.t1, A.T1+A.D, B.T2-B.D, b.t2)) {G.addedge (i, j);  
                G.addedge (J, I+n);  
                    if (Iscross (A.T2-A.D, A.t2, B.t1, B.T1+B.D)) {G.addedge (i+n, j+n);  
                G.addedge (J, I); } if (Iscross (A.T2-A.D, A.t2, B.T2-B.D, b.t2)) {G.addedge (i+n, J);  
                G.addedge (J+n, i);  
        }} if (!sat.check (g, N)) {puts ("NO");  
            }else{puts ("YES");  
        Sat.toposort (G,g2,n);  
} 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.