zoukankan      html  css  js  c++  java
  • POJ 3683.Priest John's Busiest Day 2-SAT

    Priest John's Busiest Day
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 10144   Accepted: 3472   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

     
     
    题意:有一天n对夫妻同时结婚,但是只有一个神父,需要神父只需要在结婚典礼开始的d分钟或者结束的d分钟,即[s,s+d]或者[t-d,t]。问能不能举办所有的婚礼。
    思路:2-SAT
    2-SAT

      什么是2-SAT呢?就是有一些集合,每个集合中有且仅有两个元素,且不能同时选取两个元素,集合间的元素存在一定的选择关系,求解可行性及可行方案。

    算法

    1、连边

    2、tarjan算法缩点,同一个强联通分量缩成一个点。

    3、判可行性,即同一集合中的两个点是否同属一个强连通块

    4、缩点建新图,连反边

    5、拓扑序,若当前点没有被访问过,则选择该点,不选择其另外的点

    (缩点方式改成kosaraju算法(即按照原图拓扑排序的点顺序拓扑排序转置图,最后的强联通分量并且是按照拓扑排序的顺序)可以省略第5步,最后scnno[x]>sccno[¬x]即x为真)

    连边

    2-SAT算法本身并不难,关键是连边:

    a->b即选a必选b。

    a、b不能同时选:选了a就要选b',选了b就要选a'。

    a、b必须同时选:选了a就要选b,选了b就要选a,选了a'就要选b',选了b'就要选a'。

    a、b必须选一个:选了a就要选b',选了b就要选a',选了a'就要选b,选了b'就要选a。

    ※a必须选:a'->a。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> P;
    const int maxn=2e3+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
    const ll INF=1e18+7;
    priority_queue<P,vector<P>,greater<P> >q;
    struct tt
    {
        int s,t,d;
    } sign[maxn];
    vector<int>G[maxn];
    void addedge(int u,int v)
    {
        G[u].push_back(v);
    }
    int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
    stack<int>s;
    void dfs(int u)
    {
        pre[u]=lowlink[u]=++dfs_clock;
        s.push(u);
        for(int i=0; i<G[u].size(); i++)
        {
            int v=G[u][i];
            if(!pre[v])
            {
                dfs(v);
                lowlink[u]=min(lowlink[u],lowlink[v]);
            }
            else if(!sccno[v])
                lowlink[u]=min(lowlink[u],pre[v]);
        }
        if(lowlink[u]==pre[u])
        {
            scc_cnt++;
            while(true)
            {
                int x=s.top();
                s.pop();
                sccno[x]=scc_cnt;
                if(x==u) break;
            }
        }
    }
    void find_scc(int n)
    {
        dfs_clock=scc_cnt=0;
        memset(sccno,0,sizeof(sccno));
        memset(pre,0,sizeof(pre));
        for(int i=1; i<=n; i++)
            if(!pre[i]) dfs(i);
    }
    tt ans1[maxn],ans2[maxn];
    int cmp(tt x,tt y)
    {
        return x.t<y.t;
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            int st,sm,tt,tm,d;
            scanf("%d:%d %d:%d %d",&st,&sm,&tt,&tm,&d);
            sign[i].s=st*60+sm,sign[i].t=tt*60+tm,sign[i].d=d;
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(i==j) continue;
                if(max(sign[i].s,sign[j].s)<min(sign[i].s+sign[i].d,sign[j].s+sign[j].d))
                {
                    addedge(i,j+n);
                    addedge(j,i+n);
                }
                if(max(sign[i].s,sign[j].t-sign[j].d)<min(sign[i].s+sign[i].d,sign[j].t))
                {
                    addedge(i,j);
                    addedge(j+n,i+n);
                }
                if(max(sign[i].t-sign[i].d,sign[j].s)<min(sign[i].t,sign[j].s+sign[j].d))
                {
                    addedge(i+n,j+n);
                    addedge(j,i);
                }
                if(max(sign[i].t-sign[i].d,sign[j].t-sign[j].d)<min(sign[i].t,sign[j].t))
                {
                    addedge(i+n,j);
                    addedge(j+n,i);
                }
            }
        }
        find_scc(2*n);
        for(int i=1; i<=n; i++)
        {
            if(sccno[i]==sccno[i+n])
            {
                puts("NO");
                return 0;
            }
        }
        puts("YES");
        for(int i=1; i<=n; i++)
        {
            if(sccno[i]>sccno[i+n])
            {
                ans1[i].s=sign[i].s,ans1[i].t=sign[i].s+sign[i].d;
                ans2[i].s=sign[i].t-sign[i].d,ans2[i].t=sign[i].t;
    
            }
            else
            {
                ans1[i].s=sign[i].t-sign[i].d,ans1[i].t=sign[i].t;
                ans2[i].s=sign[i].s,ans2[i].t=sign[i].s+sign[i].d;
            }
        }
        sort(ans1+1,ans1+n+1,cmp);
        for(int i=1; i<n; i++)
        {
            if(ans1[i].t>ans1[i+1].s)
            {
                for(int j=1; j<=n; j++)
                    printf("%02d:%02d %02d:%02d
    ",ans2[j].s/60,ans2[j].s%60,ans2[j].t/60,ans2[j].t%60);
                return 0;
            }
        }
        for(int i=1; i<=n; i++)
            printf("%02d:%02d %02d:%02d
    ",ans1[i].s/60,ans1[i].s%60,ans1[i].t/60,ans1[i].t%60);
        return 0;
    }
    2-SAT
  • 相关阅读:
    openAI的仿真环境Gym Retro的Python API接口(续1)—— 游戏过程记录及回放
    openAI的仿真环境Gym Retro的Python API接口
    如何使用Python环境下的2D经典游戏仿真器(openai推出的)retro库运行游戏"刺猬索尼克" (SonicTheHedgehog-Genesis)
    运行openai的gym代码报错提示import pyglet,安装后依然报错:ImportError: sys.meta_path is None, Python is likely shutting down
    “阿里事件”的结束真的是结束吗
    BMS(电池管理系统)第二课——一文说清各大IC厂商模拟前端(AFE)优缺点
    BMS(电池管理系统)第一课——BMS系统框架简介什么是BMS?
    Linux内核调试技术——kprobe使用与实现
    Qt事件:changeEvent(改变事件)
    ImGui-imgui实例解析之ShowDemoWindowWidgets-Basic
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7287173.html
Copyright © 2011-2022 走看看