POJ3683 Priest John's Busiest Day

来源:互联网 发布:种子 知乎 编辑:程序博客网 时间:2024/05/22 05:02

Priest John’s Busiest Day

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10647 Accepted: 3626 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 Si, Ti and Di. Si 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

POJ Founder Monthly Contest – 2008.08.31, Dagger and Facer


题目的意思是给出n对时间和一个持续时间,表示某场婚礼的时间和神父主持时间,神父只能在某场婚礼开始或结束主持,问能否主持所有的婚礼
思路:2-sat 先根据婚礼的矛盾关系建图,然后2-sat求解,输出解时先缩点,建反向边,再染色,最后拓扑排序输出

#include <iostream>  #include <cstdio>  #include <cstring>  #include <string>  #include <algorithm>  #include <cmath>  #include <map>  #include <set>  #include <stack>  #include <queue>  #include <vector>  #include <bitset>  using namespace std;#define LL long long  const int INF = 0x3f3f3f3f;#define MAXN 100100  #define MAXM 5000100  struct node{    int u, v, next;} edge[MAXM],newedge[MAXM];int dfn[MAXN], low[MAXN], s[MAXN], Stack[MAXN], in[MAXN], block[MAXN], tt[MAXN],vis[MAXN],cf[MAXN],     tim[MAXN],du[MAXN],news[MAXN];int cnt, tot, index, bnum, n,ncnt;void init(){    memset(s, -1, sizeof s);    memset(dfn, 0, sizeof dfn);    memset(low, 0, sizeof low);    memset(Stack, 0, sizeof Stack);    memset(in, 0, sizeof in);    memset(block, 0, sizeof block);    cnt = tot = index = bnum = 0;}void add(int u, int v){    edge[cnt].u = u;    edge[cnt].v = v;    edge[cnt].next = s[u];    s[u] = cnt++;}void tarjan(int x){    dfn[x] = low[x] = ++tot;    Stack[++index] = x;    in[x] = 1;    for (int i = s[x]; ~i; i = edge[i].next)    {        int v = edge[i].v;        if (!dfn[v])        {            tarjan(v);            low[x] = min(low[x], low[v]);        }        else if (in[v])        {            low[x] = min(low[x], low[v]);        }    }    if (dfn[x] == low[x])    {        bnum++;        do        {            //printf("%d ",Stack[index]);              block[Stack[index]] = bnum;            in[Stack[index--]] = 0;        } while (Stack[index + 1] != x);        // printf("\n");      }}void solve(){    for (int i = 0; i < 2 * n; i++)        if (!dfn[i])            tarjan(i);    for (int i = 0; i<2 * n; i += 2)    {        if (block[i] == block[i ^ 1])        {            printf("NO\n");            return;        }        cf[block[i]] = block[i ^ 1];        cf[block[i^1]] = block[i];    }    printf("YES\n");    ncnt=0;    memset(news, -1, sizeof news);    for (int i = 0; i < cnt; i++)    {        if (block[edge[i].u] != block[edge[i].v])        {            newedge[ncnt].u = block[edge[i].v];            newedge[ncnt].v = block[edge[i].u];            newedge[ncnt].next = news[block[edge[i].v]];            news[block[edge[i].v]] = ncnt++;            du[block[edge[i].u]]++;        }    }    memset(vis, 0, sizeof vis);    queue<int>q;    for (int i = 1; i <= bnum; i++)    {        if (du[i] == 0)        {            q.push(i);        }    }    while (!q.empty())    {        int f = q.front();        q.pop();        if (vis[f] == 0)        {            vis[f] = 1;            vis[cf[f]] = -1;        }        for (int i = news[f]; ~i; i = newedge[i].next)        {            if (--du[newedge[i].v] == 0) q.push(newedge[i].v);        }    }    for (int i = 0; i<2 * n; i += 2)    {        if (vis[block[i]] == 1)        {            printf("%02d:%02d %02d:%02d\n", tt[i] / 60, tt[i] % 60, (tt[i] + tim[i/2]) / 60, (tt[i] + tim[i/2]) % 60);        }        else        {            printf("%02d:%02d %02d:%02d\n", (tt[i^1] - tim[i/2]) / 60, (tt[i^1] - tim[i/2]) % 60,tt[i^1] / 60, tt[i^1] % 60);        }    }    return ;}/*308:00 10:00 7009:00 09:30 109:00 10:00 20*/int main(){    int H, M;    while (~scanf("%d", &n))    {        init();        for (int i = 0; i < n; i++)        {            scanf("%d:%d", &H, &M);            tt[2 * i] = H * 60 + M;            scanf("%d:%d", &H, &M);            tt[2 * i + 1] = H * 60 + M;            scanf("%d", &tim[i]);        }        for (int i = 0; i < n; i++)        {            for (int j = i+1; j < n; j++)            {                int xx = 2 * i, yy = 2 * j;                if (tt[xx] + tim[xx / 2] <= tt[yy] || tt[xx] >= tt[yy] + tim[yy / 2])                    ;                else                    add(xx, yy ^ 1), add(yy, xx ^ 1);                xx = 2 * i+1, yy = 2 * j;                if (tt[xx] <= tt[yy] || tt[xx]-tim[xx/2] >= tt[yy] + tim[yy / 2])                    ;                else                    add(xx, yy ^ 1), add(yy, xx ^ 1);                xx = 2 * i, yy = 2 * j+1;                if (tt[xx] + tim[xx / 2] <= tt[yy]-tim[yy/2] || tt[xx] >= tt[yy])                    ;                else                    add(xx, yy ^ 1), add(yy, xx ^ 1);                xx = 2 * i+1, yy = 2 * j+1;                if (tt[xx]  <= tt[yy]-tim[yy/2] || tt[xx]-tim[xx/2] >= tt[yy])                    ;                else                    add(xx, yy ^ 1), add(yy, xx ^ 1);            }        }        solve();    }    return 0;}
阅读全文
0 0
原创粉丝点击