zoukankan      html  css  js  c++  java
  • poj 3686 Priest John's Busiest Day

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

    2-sat 问题判定,输出一组可行解

    http://www.cnblogs.com/TheRoadToTheGold/p/8436948.html

    注:

    本代码在判断两个时间段部分有误,数据弱A了

    #include<cstdio>
    #include<vector>
    
    using namespace std;
    
    #define N 1001
    
    struct TIME
    {
        int h1,m1;
        int h2,m2;
        int tim;
    }e[N];
    
    int n;
    int tot;
    
    int front[N<<1],to[N*N*8],nxt[N*N*8];
    
    int dfn[N<<1],low[N<<1];
    int st[N<<1],top;
    bool vis[N<<1];
    
    int cnt;
    int id[N<<1];
    vector<int>V[N<<1];
    
    int FRONT[N<<1],TO[N*N*8],NXT[N*N*8],TOT;
    int in[N<<1];
    
    bool use[N<<1],cut[N<<1];
    
    struct ANS
    {
        int h1,m1;
        int h2,m2;
    }ans[N];
    
    void add(int u,int v)
    {
        to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
    }
    
    void ADD(int u,int v)
    {
        TO[++TOT]=v; NXT[TOT]=FRONT[u]; FRONT[u]=TOT;
        in[v]++;
    }
    
    bool judge(int h1s,int m1s,int h1t,int m1t,int h2s,int m2s,int h2t,int m2t)
    {
        int s1=(h1s-1)*60+m1s;
        int t1=(h1t-1)*60+m1t;
        int s2=(h2s-1)*60+m2s;
        int t2=(h2t-1)*60+m2t;
        if(s1<=s2)
        {
            if(s2>=t1) return false;
            return true; 
        }
        else
        {
            if(t2<=s1) return false;
            return true;
        }
    }
    
    void tarjan(int u)
    {
        dfn[u]=low[u]=++tot;
        st[++top]=u;
        vis[u]=true;
        for(int i=front[u];i;i=nxt[i])
        {
            if(!dfn[to[i]])
            {
                tarjan(to[i]);
                low[u]=min(low[u],low[to[i]]);
            }
            else if(vis[to[i]]) low[u]=min(low[u],dfn[to[i]]);
        }
        if(dfn[u]==low[u])
        {
            cnt++;
            while(st[top]!=u)
            {
                id[st[top]]=cnt;
                vis[st[top]]=false;
                V[cnt].push_back(st[top--]);
            }
            id[u]=cnt;
            vis[u]=false;
            V[cnt].push_back(u);
            top--;
        }
    }
    
    void rebuild()
    {
        for(int k=1;k<=n;++k)
        {
            int i=k<<1;
            for(int j=front[i];j;j=nxt[j])
                if(id[i]!=id[to[j]]) ADD(id[to[j]],id[i]);
            i=k<<1|1;
            for(int j=front[i];j;j=nxt[j])
                if(id[i]!=id[to[j]]) ADD(id[to[j]],id[i]);
        }
    }
    
    void out(int h,int m)
    {
        if(m>0 && m<60) printf("%02d:%02d",h,m);
        else if(m<0)
        {
            if(!(m%60))
            {
                h+=m/60;
                printf("%02d:00",h);
            }
            else
            {
                h+=m/60;
                h--;
                m%=60;
                if(m<0) m+=60;
                printf("%02d:%02d",h,m);
            }
        }
        else
        {
            h+=m/60;
            m%=60;
            printf("%02d:%02d",h,m);
        }
    }
    
    void topsort()
    {
        for(int i=1;i<=cnt;++i)
            if(!in[i]) st[++top]=i;
        int u,v;
        while(top)
        {
            u=st[top--];
            if(cut[u]) continue;
            use[u]=true;
            v=id[V[u][0]^1];
            cut[v]=true;
            for(int i=FRONT[u];i;i=NXT[i])
            {
                in[TO[i]]--;
                if(!in[TO[i]]) st[++top]=TO[i];
            }
            //for(int i=FRONT[v];i;i=NXT[i]) cut[TO[i]]=true;
        }
        for(int i=1;i<=n;++i)
            if(use[id[i<<1]]) 
            {
                ans[i].h1=e[i].h1;
                ans[i].m1=e[i].m1;
                ans[i].h2=e[i].h1;
                ans[i].m2=e[i].m1+e[i].tim;
            }
            else 
            {
                ans[i].h1=e[i].h2;
                ans[i].m1=e[i].m2-e[i].tim;
                ans[i].h2=e[i].h2;
                ans[i].m2=e[i].m2;
            }
        puts("YES");
        for(int i=1;i<=n;++i)
        {
            out(ans[i].h1,ans[i].m1);
            putchar(' ');
            out(ans[i].h2,ans[i].m2);
            putchar('
    ');
        }
    }
    
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;++i) 
        {
            scanf("%d:%d",&e[i].h1,&e[i].m1);
            scanf("%d:%d",&e[i].h2,&e[i].m2);
            scanf("%d",&e[i].tim);
        }
        for(int i=1;i<n;++i)
            for(int j=i+1;j<=n;++j)
                if(i!=j)
                { 
                    if(judge(e[i].h1,e[i].m1,e[i].h1,e[i].m1+e[i].tim,e[j].h1,e[j].m1,e[j].h1,e[j].m1+e[j].tim))
                           add(i<<1,j<<1|1),add(j<<1,i<<1|1);
                    if(judge(e[i].h1,e[i].m1,e[i].h1,e[i].m1+e[i].tim,e[j].h2,e[j].m2-e[j].tim,e[j].h2,e[j].m2)) 
                        add(i<<1,j<<1),add(j<<1|1,i<<1|1);
                    if(judge(e[i].h2,e[i].m2-e[i].tim,e[i].h2,e[i].m2,e[j].h1,e[j].m1,e[j].h1,e[j].m1+e[j].tim))
                        add(i<<1|1,j<<1|1),add(j<<1,i<<1);
                    if(judge(e[i].h2,e[i].m2-e[i].tim,e[i].h2,e[i].m2,e[j].h2,e[j].m2-e[j].tim,e[j].h2,e[j].m2)) 
                        add(i<<1|1,j<<1),add(j<<1|1,i<<1);
                }
        tot=0;
        for(int i=1;i<=n;++i)
        {
            if(!dfn[i<<1]) tarjan(i<<1);
            if(!dfn[i<<1|1]) tarjan(i<<1|1);
        }
        for(int i=1;i<=n;++i)
            if(id[i<<1]==id[i<<1|1]) 
            {
                puts("NO");
                return 0;
            }
        rebuild();
        topsort();
    }
    Priest John's Busiest Day
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 11007   Accepted: 3759   Special Judge

    Description

    John is the only priest in his town. September 1st is the John's busiest day in a year because there is an old legend in the town that the couple who get married on that day will be forever blessed by the God of Love. This year N couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from time Si to time Ti. According to the traditions in the town, there must be a special ceremony on which the couple stand before the priest and accept blessings. The i-th couple need Di minutes to finish this ceremony. Moreover, this ceremony must be either at the beginning or the ending of the wedding (i.e. it must be either from Si to Si + Di, or from Ti - Di to Ti). Could you tell John how to arrange his schedule so that he can present at every special ceremonies of the weddings.

    Note that John can not be present at two weddings simultaneously.

    Input

    The first line contains a integer N ( 1 ≤ N ≤ 1000). 
    The next N lines contain the SiTi and DiSi and Ti are in the format of hh:mm.

    Output

    The first line of output contains "YES" or "NO" indicating whether John can be present at every special ceremony. If it is "YES", output another N lines describing the staring time and finishing time of all the ceremonies.

    Sample Input

    2
    08:00 09:00 30
    08:15 09:00 20
    
    

    Sample Output

    YES
    08:00 08:30
    08:40 09:00
    

    Source

  • 相关阅读:
    java数组
    java 常用类
    java 集合(一)
    mysql相关操作(一)
    记录java BigDecimal
    hxg-yw
    一个困惑我好久的问题
    关于重载和重写的一些小知识
    几个常用的HTTP状态码
    死锁产生的原因和条件简述
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8438346.html
Copyright © 2011-2022 走看看