• 原题如下:

    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. C#LeetCode刷题之#441-排列硬币(Arranging Coins)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3995 访问. 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状 ...

  2. JVM垃圾回收(GC)

    JVM垃圾回收(GC) 1. 判断对象是否可以被回收 引用计数法:每个对象有一个引用计数属性,新增一个引用时计数加1,引用释放时计数减1,计数为0时可以回收.此方法简单,但无法解决对象相互循环引用的问 ...

  3. 极简 Node.js 入门 - 2.2 事件

    极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...

  4. 【SP2916】Can you answer these queries V - 线段树

    题面 You are given a sequence \(a_1,a_2,...,a_n\). (\(|A[i]| \leq 10000 , 1 \leq N \leq 10000\)). A qu ...

  5. Ubuntu 18.04 安装配置 java jdk

    1.下载 下载 jdk 到 Downloands 文件夹下 cd 进入 /usr/local, 创建 jdk 文件夹, 然后 cd 进这个文件夹 cd /usr/local sudo mkdir jd ...

  6. myBatis源码解析-配置文件解析(6)

    前言 本来打算此次写一篇关于SqlSession的解析,但发现SqlSession涉及的知识太多.所以先结合mybatis配置文件(我们项目中常写的如mybatisConfig.xml),来分析下my ...

  7. 共享&img (给作者自己

    --------------------------------------------------------- ------------------------------------------ ...

  8. 什么?Java9这些史诗级更新你都不知道?Java9特性一文打尽!

    「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...

  9. psutil 简单使用!

    psutil.cpu_percent() cpu 百分比 mem = psutil.virtual_memory()mem.total,mem.used mem.free psutil.cpu_cou ...

  10. CentOS7(Linux)源码安装Redis

    介绍 项目中经常需要用到Redis做缓存数据库,可是还有小伙伴不会在Linux上安装Redis,毕竟我们开发的项目都是要在服务器上运行的,今天就来讲讲如何在CentOS7环境使用源码进行安装Redis ...