POJ 3683 Priest John's Busiest Day 【经典2-SAT】


传送门:POJ 3683


Priest John's Busiest Day
Time Limit: 2000MS Memory Limit: 65536K

Problem 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



题意:
有n场婚礼,每场婚礼开始时间为s,结束时间为t,需要时间D,且所有婚礼必须在ss+D或t-Dt进行。求一种可行的方案使婚礼不冲突。


题解:
把每场婚礼看作两场,一场开头,一场结尾,那么有些婚礼是不能同时举办的。我们可以把这些不能同时举办的婚礼看作限制,这样这道题目就变成了经典的2-SAT。

烦啊,最后的输出错了半天没看出来Orz



AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=1100;
int n,top;
bool mark[2*N];
int stack[2*N];
vector<int> g[2*N];
struct Node
{
  int s,t;
}b[2*N];
void init()
{
  memset(mark,false,sizeof(mark));
  for(int i=0;i<2*N;i++)
  {
    g[i].clear();
  }
}
bool Wrong(int i,int j)//判断两条边是否相交
{
  if(b[i].s>=b[j].t||b[i].t<=b[j].s) return false;
  else return true;
}
void addedge(int x,int y)
{
  g[x].push_back(y);
}
bool dfs(int x)
{
  if(mark[x^1]) return false;
  if(mark[x]) return true;
  mark[x]=true;
  stack[++top]=x;
  int len=g[x].size();
  for(int i=0;i<len;i++)
  {
    if(!dfs(g[x][i])) return false;
  }
  return true;
}
bool twosat()
{
  for(int i=0;i<2*n;i+=2)
  {
    if(!mark[i]&&!mark[i+1])
    {
      top=0;
      if(!dfs(i))
      {
        while(top>0)
          mark[stack[top--]]=false;
        if(!dfs(i+1)) return false;
      }
    }
  }
  return true;
}
int main()
{
  int d,h1,m1,h2,m2;
  while(scanf("%d",&n)!=EOF)
  {
    init();
    for(int i=0;i<n;i++)
    {
      int s,t;
      scanf("%d:%d %d:%d %d",&h1,&m1,&h2,&m2,&d);
      s=h1*60+m1;t=h2*60+m2;
      b[2*i].s=s;//开始
      b[2*i].t=s+d;
      b[2*i+1].s=t-d;//结束
      b[2*i+1].t=t;
    }
    for(int i=0;i<2*n;i+=2)
    {
      for(int j=0;j<2*n;j+=2)
      {
        if(i==j) continue;
        if(Wrong(i,j))//开头和开头相交
        {
          addedge(i,j+1);
          addedge(j,i+1);
        }
        if(Wrong(i,j+1))//开头和结尾相交
        {
          addedge(i,j);
          addedge(j+1,i+1);
        }
        if(Wrong(i+1,j))
        {
          addedge(i+1,j+1);
          addedge(j,i);
        }
        if(Wrong(i+1,j+1))//结尾和结尾相交
        {
          addedge(i+1,j);
          addedge(j+1,i);
        }
      }
    }
    if(twosat())
    {
      printf("YES\n");
      for(int i=0;i<n;i++)
      {
        if(mark[2*i])
          printf("%02d:%02d %02d:%02d\n",b[2*i].s/60,b[2*i].s%60,b[2*i].t/60,b[2*i].t%60);
        else
          printf("%02d:%02d %02d:%02d\n",b[2*i+1].s/60,b[2*i+1].s%60,b[2*i+1].t/60,b[2*i+1].t%60);
      }
    }
    else printf("NO\n");
  }
  return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值