poj3683

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 Sito 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 Sito 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

 题意:n代表有n场婚礼。给出每场婚礼的计划起止时间以及婚礼的举行时长。婚礼只能在 [起始时间,起始时间+时常]或

[截至时间-时长,截至时间],有一个牧师问能不能同时参加这n场婚礼。如果可以输出每场婚礼的起止时间。

分析:

i表示甲婚礼前段,^i表示甲婚礼后段

j表示乙婚礼前段,^j表示乙婚礼后段

建图:

i与j有冲突,i-->^j,j-->^i

i与^j有冲突,i-->j,^j-->^i

^i与j有冲突,^i-->^j,j-->i

^i与^j有冲突,^i-->j,^j-->i

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
const int maxn=100000+10;
struct tt
{
    int st,en,d;
    tt(){}
    tt(int st,int en,int d):st(st),en(en),d(d){}
}ti[maxn];

struct TwoSAT
{
    int n;
    vector<int> G[maxn*5];
    int S[maxn*5],c;
    bool mark[maxn*2];

    bool dfs(int x)
    {
        if(mark[x^1]) return false;
        if(mark[x]) return true;
        mark[x]=true;
        S[c++]=x;
        for(int i=0;i<G[x].size();i++)
            if(!dfs(G[x][i])) return false;
        return true;
    }

    void init(int n)
    {
        this->n=n;
        for(int i=0;i<n*2;i++) G[i].clear();
        memset(mark,0,sizeof(mark));
    }

    void add_clause(int x,int xval,int y,int yval)//这里的函数做了修改,只加单向边
    {
        x=x*2+xval;
        y=y*2+yval;
        G[x].push_back(y);
    }

    bool solve()
    {
        for(int i=0;i<2*n;i+=2)
        if(!mark[i] && !mark[i+1])
        {
            c=0;
            if(!dfs(i))
            {
                while(c>0) mark[S[--c]]=false;
                if(!dfs(i+1)) return false;
            }
        }
        return true;
    }
}TT;
int main()
{
    int n;
    scanf("%d",&n);
    TT.init(n);
 for(int i=0;i<n;i++)
    {
        int a,b,c,d,e;
        scanf("%d:%d %d:%d %d",&a,&b,&c,&d,&e);
        int x=a*60+b;
        int y=c*60+d;
        ti[i]=tt(x,y,e);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(ti[i].st<ti[j].st+ti[j].d&&ti[j].st<ti[i].st+ti[i].d)
            {
                TT.add_clause(i,0,j,1);
                TT.add_clause(j,0,i,1);
            }
            if(ti[i].en-ti[i].d<ti[j].en&&ti[j].en-ti[j].d<ti[i].en)
            {
                  TT.add_clause(i,1,j,0);
                  TT.add_clause(j,1,i,0);
            }
             if(ti[i].st<ti[j].en&&ti[j].en-ti[j].d<ti[i].st+ti[i].d)
            {
                  TT.add_clause(i,0,j,0);
                  TT.add_clause(j,1,i,1);
            }
            if(ti[i].en-ti[i].d<ti[j].st+ti[j].d&&ti[j].st<ti[i].en)
            {
                 TT.add_clause(i,1,j,1);
                 TT.add_clause(j,0,i,0);
            }
        }
    }
    if(TT.solve())
    {
        printf("YES\n");
        for(int i=0;i<n;i++)
        {
            if(TT.mark[2*i])
            {
                 printf("%02d:%02d %02d:%02d\n",ti[i].st/60,ti[i].st%60,(ti[i].st+ti[i].d)/60,(ti[i].st+ti[i].d)%60);
            }
            else
            {
                printf("%02d:%02d %02d:%02d\n",(ti[i].en-ti[i].d)/60,(ti[i].en-ti[i].d)%60,ti[i].en/60,ti[i].en%60);
            }
        }
    }
    else
    {
        printf("NO\n");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值