• 原题如下:

    Priest John's Busiest Day
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 12162   Accepted: 4138   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 SiDi, 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
  • 题解:定义变量xi用于表示对于结婚仪式i在开始还是结束时进行特别仪式:xi为真↔在开始时进行特别仪式
    这样,对于结婚仪式i和j,如果Si~Si+Di和Sj~Sj+Dj冲突,就有¬xi∨¬xj为真。对于开始和结束、结束和开始、结束和结束三种情况,也可以得到类似的条件。于是,要保证所有特别仪式的时间不冲突,只要考虑将所有的子句用∧连接起来所得到的布尔公式就好了。对于输入样例,可以得到布尔公式(¬x1∨¬x2)∧(x1∨¬x2)∧(x1∨x2),当x1为真而x2为假时,其值为真。这样原问题就转为了2-SAT问题。
    注:判断两个区间[s1, e1]、[s2, e2]是否相交:若max(s1, s2)<min(e1, e2)为真,则两区间相交。
  • 代码:
     #include <cstdio>
    #include <algorithm>
    #include <vector>
    #include <stack>
    #include <cstring> using namespace std; const int MAX_N=;
    int N, V;
    int S[MAX_N], T[MAX_N], D[MAX_N];
    vector<int> G[MAX_N*];
    stack<int> s;
    int dfn[MAX_N*], low[MAX_N*];
    int index;
    int cmp[MAX_N*];
    bool instack[MAX_N*];
    int componentnumber; void add_edge(int x, int y)
    {
    G[x].push_back(y);
    } void tarjan(int i)
    {
    dfn[i]=low[i]=index++;
    instack[i]=true;
    s.push(i);
    int j;
    for (int e=; e<G[i].size(); e++)
    {
    j=G[i][e];
    if (dfn[j]==-)
    {
    tarjan(j);
    low[i]=min(low[i], low[j]);
    }
    else
    if (instack[j]) low[i]=min(low[i], dfn[j]);
    }
    if (dfn[i]==low[i])
    {
    componentnumber++;
    do
    {
    j=s.top();
    s.pop();
    instack[j]=false;
    cmp[j]=componentnumber;
    }
    while (j!=i);
    }
    } int main()
    {
    memset(dfn, -, sizeof(dfn));
    scanf("%d", &N);
    for (int i=; i<N; i++)
    {
    int a, b, c, d;
    scanf("%d:%d %d:%d %d", &a, &b, &c, &d, &D[i]);
    S[i]=a*+b;
    T[i]=c*+d;
    }
    V=N*;
    for (int i=; i<N; i++)
    {
    for (int j=; j<i; j++)
    {
    if (min(S[i]+D[i], S[j]+D[j])>max(S[i], S[j]))
    {
    add_edge(i, N+j);
    add_edge(j, N+i);
    }
    if (min(S[i]+D[i], T[j])>max(S[i], T[j]-D[j]))
    {
    add_edge(i, j);
    add_edge(N+j, N+i);
    }
    if (min(T[i], S[j]+D[j])>max(T[i]-D[i], S[j]))
    {
    add_edge(N+i, N+j);
    add_edge(j, i);
    }
    if (min(T[i], T[j])>max(T[i]-D[i], T[j]-D[j]))
    {
    add_edge(N+i, j);
    add_edge(N+j, i);
    }
    }
    }
    for (int i=; i<V; i++)
    {
    if (dfn[i]==-) tarjan(i);
    }
    for (int i=; i<N; i++)
    {
    if (cmp[i]==cmp[N+i])
    {
    printf("NO\n");
    return ;
    }
    }
    printf("YES\n");
    for (int i=; i<N; i++)
    {
    if (cmp[i]<=cmp[N+i])
    {
    printf("%02d:%02d %02d:%02d\n", S[i]/, S[i]%, (S[i]+D[i])/, (S[i]+D[i])%);
    }
    else
    {
    printf("%02d:%02d %02d:%02d\n", (T[i]-D[i])/, (T[i]-D[i])%, T[i]/, T[i]%);
    }
    }
    }

Priest John's Busiest Day(POJ 3683)的更多相关文章

  1. POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题)

    POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题) Descripti ...

  2. POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10010   Accep ...

  3. POJ 3683 Priest John's Busiest Day (2-SAT)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6900   Accept ...

  4. poj 3686 Priest John's Busiest Day

    http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...

  5. 【POJ3683】Priest John's Busiest Day

    题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...

  6. 图论(2-sat):Priest John's Busiest Day

    Priest John's Busiest Day   Description John is the only priest in his town. September 1st is the Jo ...

  7. POJ 3683 Priest John's Busiest Day(2-SAT 并输出解)

    Description John is the only priest in his town. September 1st is the John's busiest day in a year b ...

  8. POJ3683 Priest John's Busiest Day(2-SAT)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11049   Accepted: 3767   Special Judge ...

  9. HDU 2491 Priest John's Busiest Day(贪心)(2008 Asia Regional Beijing)

    Description John is the only priest in his town. October 26th is the John's busiest day in a year be ...

随机推荐

  1. JavaScript 手写 setTimeout 及 同步调用和异步调用

    demo let timeout = (sec, num) => { const now = new Date().getTime() // 获取进入方法时的时间 let flag = true ...

  2. Centos系统安装Python3.7

    服务器安装Python3.7,实测可用 原博客地址 首先要先安装依赖包: yum install zlib-devel bzip2-devel openssl-devel ncurses-devel ...

  3. 用Python爬取股票数据,绘制K线和均线并用机器学习预测股价(来自我出的书)

    最近我出了一本书,<基于股票大数据分析的Python入门实战 视频教学版>,京东链接:https://item.jd.com/69241653952.html,在其中用股票范例讲述Pyth ...

  4. Git使用之submodule

    入职第一周,就因为clone项目而产生了很大的障碍,花了差不多三四个小时才定位问题并解决,记录一下. 一.问题 当我们在使用Git克隆项目的时候,无法克隆下来一个文件夹.记该文件夹为A,A在远程仓库是 ...

  5. 【python接口自动化】- 使用requests库发送http请求

    前言:什么是Requests ?Requests 是⽤Python语⾔编写,基于urllib,采⽤Apache2 Licensed开源协议的 HTTP 库.它⽐ urllib 更加⽅便,可以节约我们⼤ ...

  6. 如何在 asp.net core 的中间件中返回具体的页面

    前言 在 asp.net core 中,存在着中间件这一概念,在中间件中,我们可以比过滤器更早的介入到 http 请求管道,从而实现对每一次的 http 请求.响应做切面处理,从而实现一些特殊的功能 ...

  7. idea Maven项目 包下载不下来或者已经下载了就是飘红

    0.先在settings.xml加上阿里的镜像在刷新试试 <mirror> <id>aliyunmaven</id> <mirrorOf>*</m ...

  8. python 02 if while

    1. if的格式 >>> 1<3 True 真>>> 1>3False 假 if   条件:                     条件 + : (t ...

  9. idea配置opencv

    参考:https://blog.csdn.net/sinat_38102206/article/details/81156589 配置运行时参数.通过菜单“Run->Edit Configura ...

  10. META.表

    META.表