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

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



思路:对于每个结婚仪式i,只有在开始或结束时进行特别仪式两种选择。因此,可以定义变量x_i:
                                              x_i为真<=>在开始时进行特别仪式
这样,对于结婚仪式i和j,如果开始和开始冲突,就有¬x_i∨¬x_j成立,同理,还有开始和结束,结束和开始,结束和结束三种情况。要保证所有特别仪式不冲突,显然用∧连接起来即可。这样,我们就把原问题转为了2-SAT问题。


代码:

//输入
int N;
int S[max_N],T[max_N],D[max_N];//S和T是换算成分钟后的时间

void solve()
{
         //0~N-1:x_i
         //N~2N-1:¬x_i
         V=N*2;
         for(int i=0;i<N;i++)
         {
                   for(int j=0;j<i;j++)
                   {
                              if(min(S[i]+D[i],S[j]+D[j])>max(S[i],S[j]))//开始与开始冲突,¬x_i∨¬x_j为真
                              {
                                         //x_i=>¬x_j,x_j=>¬x_i
                                         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]))//开始与结束冲突,¬x_i∨x_j为真
                              {
                                         //x_i=>x_j,¬x_j=>¬x_i
                                         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]))//结束与开始冲突,x_i∨¬x_j为真
                              {
                                         //¬x_i=>¬x_j,x_j=>x_i
                                         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]))//结束与结束冲突,x_i∨x_j为真
                              {
                                         //¬x_i=>x_j,¬x_j=>x_i
                                         add_edge(N+ii,j);
                                         add_edge(N+j,i);
                              }
                    }
          }

          scc();
          
          //判断是否可满足
          for(int i=0;i<N;i++)          
          {                    
                     if(cmp[i]==cmp[N+i])                   
                     {                               
                               printf("NO\n");                               
                               return ;
                     }           
           }
  
          //如果可满足,则给出一组解
          printf("YES\n");
          for(int i=0;i<N;i++)
                    if(cmp[i]>cmp[N+i])
                    {
                               //x_i为真,即在结婚仪式开始时举行
                               printf("%02d:%02d %02d:%02d\n",S[i]/60,S[i]%60,(S[i]+D[i])/60,(S[i]+D[i])%60);
                    }
                    else
                    {
                              //x_i为假,即在结婚仪式结束时举行
                               printf("%02d:%02d %02d:%02d\n",(T[i]-D[i])/60,(T[i]-D[i])%60,T[i]/60,T[i]%60);
                    }
          }
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值