题目

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


题意

(我尽然没用Goggle翻译就看懂了题目)

我大致翻译一下:

有一个小镇上只有一个牧师。这个小镇上有一个传说,在九月一日结婚的人会受到爱神的保佑,但是要牧师举办一个仪式。这个仪式要么在婚礼刚刚开始的时候举行,要么举行完婚礼正好结束。

现在已知有n场婚礼,告诉你每一场的开始和结束时间,以及举行仪式所需要的时间。问牧师能否参加所有的婚礼,如果能则输出一种方案。


解决

可能上面的题意还没讲清楚,先上一张图。












现在应该就非常清楚了。

继续化简题目:有n*2条线段,其中每两条(我设的是i和i+n)中只能够选择一条,问能否给出n条线段使得任意两条不存在交点。







一组(或者一个)东西有且仅有两种选择,要么选这个,要么选那个,还有一堆的约束条件。

这是一个很明显的2-sat问题。




(如果对于2-sat问题一脸懵逼,建议回去看另外两道题【按难度、分先后】)

【POI2001】和平委员会

http://blog.csdn.net/qq_30974369/article/details/73927421

【POJ3207】Ikki's Story IV - Panda's Trick

http://blog.csdn.net/qq_30974369/article/details/73930235


这题跟POJ 3207(就是上面的第二个链接) 神似,时间依旧只是一个媒介,对于所有的某两个仪式的时间段(i,j),如果存在重合,则连边(i,j')【因为是依次枚举任意两个时间段,所以在枚举到(j,i)是会建边(j,i')】



存在重合的情况也跟POJ 3207神似。。。


![这里写图片描述](http://img.blog.csdn.net/20170701102018672?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzA5NzQzNjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)


看到没,还是两个时间点交叉出现的时候存在重合
只需要加判断是否重合然后建边了。


接下来的一步和POJ 3207 也是一样的,Tarjan缩点,判断是否可行


但是,这题多一个步骤,要求出一组可行性解。
根据Dalao的Blog(在上面的第一个链接里面有dalao博客的链接),这一步应该进行拓扑排序,然后依次尝试染色(此时必定有解)即可。

这题就讲这么多,其他的具体实现参考一下代码。

/*

POJ 3683 Priest John's Busiest Day

题目大意:
(我竟然没用翻译就看懂了题目)
一个牧师要给n个婚礼进行一个仪式
给定婚礼开始的时间和结束的时间
仪式要么在婚礼刚刚开始的时候进行,要么进行完婚礼正好结束
问牧师能否参加所有的婚礼
并输出方案 思路:
跟POJ 3207神似的一道题目
依旧是把题目转化为2-sat来做
这题的时间也是一个媒介
如果某两个婚礼进行仪式的时间有重合,
那么就存在了矛盾关系,通过这些关系连边
Tarjan缩点,判断
拓扑排序染色(把仪式在 开始/结束 进行染色)
最后输出解即可 */ #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int MAX=10500; int Col[MAX],In[MAX],len[MAX],Opp[MAX];
int s[MAX],G[MAX],dfn[MAX],low[MAX];
int group=0,top=0,tim=0;
bool vis[MAX];
int n;
vector<int> E[MAX];//超级点就用vector来存边,方便些 struct Time
{
int b,e;//b表示开始时间,e表示结束时间
}t[MAX];
struct Node
{
int v,next;
}e[MAX*MAX];//跑Tarjan时用来存边的邻接表 int h[MAX],cnt=0;//邻接表存储 void Add(int u,int v);//连边,从u->v
void Link();//连边
bool Wrong(int i,int j);//检查两个时间是否矛盾
void Tarjan(int u);//缩点
void Top();//拓扑排序 int main()
{
while(cin>>n)
{
memset(h,-1,sizeof(h));
cnt=0;tim=group=top=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(In,0,sizeof(In));
memset(vis,0,sizeof(vis));
memset(Opp,0,sizeof(Opp));
for(int i=1;i<=n;++i)
{
int a,b,c,d,e;
scanf("%d:%d %d:%d %d",&a,&b,&c,&d,&len[i]);
//这里都把时间转化为分钟来计算
t[i].b=a*60+b;
t[i+n].e=c*60+d;
t[i].e=t[i].b+len[i];//计算开头的结束时间
t[i+n].b=t[i+n].e-len[i];//计算结尾的开始时间
//i 表示第i个婚礼的仪式在开始的时候举行
//i+m 表示第i个婚礼的仪式在结束的时候举行
} Link();//连边 for(int i=1;i<=2*n;++i)//缩点
if(!dfn[i])Tarjan(i); for(int i=1;i<=n;++i)//判断是否有解
{
if(G[i]==G[i+n])//某个婚礼的 开始/结束 在同一个强连通分量中,无解
{
cout<<"NO"<<endl;
return 0;
}
Opp[G[i]]=G[i+n];//存一下超级点的的对立点
Opp[G[i+n]]=G[i];//反向也要存
} cout<<"YES"<<endl;//不存在矛盾,则有解 Top();//Top排序 for(int i=1;i<=n;++i)
{
if(Col[G[i]]==1)//选择了再婚礼开始时
printf("%.2d:%.2d %.2d:%.2d\n",t[i].b/60,t[i].b%60,t[i].e/60,t[i].e%60);
else //在婚礼结束时
printf("%.2d:%.2d %.2d:%.2d\n",t[i+n].b/60,t[i+n].b%60,t[i+n].e/60,t[i+n].e%60);
}
} } inline void Add(int u,int v)//连边,从u->v
{
e[cnt]=(Node){v,h[u]};
h[u]=cnt++;
} bool Wrong(int i,int j)//判断两个时间是否矛盾
{
if(t[i].b>=t[j].e||t[i].e<=t[j].b)
return false;
else
return true;
//只要某一个结束时间在另一个的开始时间之前则无矛盾
//反之必定存在矛盾
} void Link()//连边
{
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)//枚举任意两组仪式的时间,查看是否存在矛盾
{
if(i==j)continue;//自己和自己没有比较的意义
if(Wrong(i,j))//开头和开头矛盾
Add(i,j+n);
if(Wrong(i,j+n))//开头和结尾矛盾
Add(i,j);
if(Wrong(i+n,j))//结尾和开头矛盾
Add(i+n,j+n);
if(Wrong(i+n,j+n))//结尾和结尾矛盾
Add(i+n,j);
}
} void Tarjan(int u)//缩点
{
dfn[u]=low[u]=++tim;
s[++top]=u;
vis[u]=true;
int v;
for(int i=h[u];i!=-1;i=e[i].next)
{
v=e[i].v;
if(!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else
if(vis[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
++group;
do
{
v=s[top--];
vis[v]=false;
G[v]=group;
}while(v!=u&&top);
}
} void Top()//拓扑排序
{
memset(In,0,sizeof(In));//存入度
memset(Col,-1,sizeof(Col));//存颜色
for(int i=1;i<=2*n;++i)//连接超级点
{
for(int j=h[i];j!=-1;j=e[j].next)//枚举邻边
{
int v=e[j].v;
if(G[i]==G[v])continue;//在同一个强连通分量中无需考虑
E[G[v]].push_back(G[i]);//边要反着存
In[G[i]]++;//统计入度
}
}
queue<int> Q;//拓扑排序的队列
for(int i=1;i<=group;++i)
if(In[i]==0)Q.push(i);//入度为0的进队
while(!Q.empty())
{
int v=Q.front();
Q.pop();
if(Col[v]==-1)//未染色
{
Col[v]=1; //染为1
Col[Opp[v]]=0;//对立点染为0
}
for(int i=0;i<E[v].size();++i)//依次减入度
{
In[E[v][i]]--;
if(In[E[v][i]]==0)//入度为0
Q.push(E[v][i]);
}
}
}
/*
写在AC后面:
这几道2-sat题真是花式出错
这道的错误是缩点以后,并不是按照超级点来输出(明显会挂呀)
然而。。。查了1小时的错
感叹一句:对拍真是利器也。。。
*/

【POJ3683】Priest John's Busiest Day的更多相关文章

  1. poj3683 2-sat Priest John's Busiest Day

    Description John is the only priest in his town. September 1st is the John's busiest day in a year b ...

  2. Poj3683:Priest John's Busiest Day

    题意 n对夫妻要结婚,第i对夫妻结婚的婚礼持续时间为[Si, Ti],他们会举行一个仪式,仪式时间为Di,这个仪式只能举行在开头或者结尾举行,要么[Si, Si+Di],要么[Ti-Di, Ti],然 ...

  3. 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 ...

  4. 图论(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 ...

  5. poj 3686 Priest John's Busiest Day

    http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...

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

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6900   Accept ...

  7. POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10010   Accep ...

  8. Priest John's Busiest Day(POJ 3683)

    原题如下: Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12162   ...

  9. POJ3683 Priest John's Busiest Day 【2-sat】

    题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...

随机推荐

  1. 自己制作ssl证书:自己签发免费ssl证书,为nginx生成自签名ssl证书

    这里说下Linux 系统怎么通过openssl命令生成 证书. 首先执行如下命令生成一个key openssl genrsa -des3 -out ssl.key 1024 然后他会要求你输入这个ke ...

  2. 洛谷P4014 分配问题【最小/大费用流】题解+AC代码

    洛谷P4014 分配问题[最小/大费用流]题解+AC代码 题目描述 有 n 件工作要分配给 n 个人做.第 i 个人做第 j 件工作产生的效益为c ij. 试设计一个将 n 件工作分配给 n 个人做的 ...

  3. sql server在一个字段相同值时,另一个字段结果拼接

    如下字段红框里的信息都一样的,通过转换实现字段拼接 SELECT formmain_id,(SELECT field0040+';' FROM formson_5489 WHERE formmain_ ...

  4. 怎么添加用户到sudo用户组

    跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux 前段时间用Ubuntu的sudo用惯了,回到kali发现自己的用户太low了, ...

  5. eclipse设置git忽略文件

    使用eclipse开发的程序员们经常会接触版本控制软件,这里只要说下eclipse使用egit的情况下设置忽略文件. 特此说明在这里使用window->team->ignored对于git ...

  6. SpringMvc笔记-对RESTFUL风格的配置

    1.@RequestMapping注解可以使用如下参数: 1,params:例如params={'username',"age!=100"}表示需要usernmame并且age 属 ...

  7. Win10 部署 依赖 NET3.5 项目,报错 无法安装 NET3.5 ,该如何解决?

    下载 NetFx3.cab Cab 安装包 拷贝 NetFx3.cab 文件至 C:\Windows 目录 打开命令行窗口(管理员权限) 输入以下内容: dism /online /Enable-Fe ...

  8. uva10603 倒水问题

    状态搜索.类似八数码问题 AC代码 #include<cstdio> #include<queue> #include<cstring> #include<a ...

  9. NLP+VS︱深度学习数据集标注工具、方法摘录,欢迎补充~~

    ~~因为不太会使用opencv.matlab工具,所以在找一些比较简单的工具. . . 一.NLP标注工具BRAT BRAT是一个基于web的文本标注工具,主要用于对文本的结构化标注,用BRAT生成的 ...

  10. LVDS/DVI/HDMI Interface

    数字视频信号 以SXGA为例,其时序如下: 垂直:         水平: 图中DSPTMG为使能信号,VSYNC为场同步信号,HSYNC为行同步信号.在行场的消隐期(T1与T7),DSPTMG为低电 ...