【POJ3683】Priest John's Busiest Day(Special Judge)

【POJ3683】Priest John's Busiest Day(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

【问题描述】
    John是镇里唯一的神父。9月1日是John一年中最忙的一天,因为镇上有一个古老传说:这一天结婚的新人将会得到爱神永远的祝福。今年有N对新人打算在这个被祝福的日子结婚。第i对新人打算从时刻Si至时刻Ti举行他们的婚礼。根据传统必须举行一个特殊的仪式,新人站在神父前并接受祝福。第i对新人需要Di分钟来完成这个仪式。
并且,这个仪式必须在婚礼的开始或结尾举行(也就是说,它必须从时刻Si至时刻Si+Di或从时刻Ti-Di至时刻Ti举行)。请你告诉John怎样安排他的行程,使得他能出席每一个婚礼的特殊仪式。注意John不能同时出现在两个婚礼上。


对于每一个婚礼,我们会发现有两段时间是可用的。我们将每一个婚礼分为i和i'两个点。
如果存在i和j的冲突,就连接i和j'
然后Sat输出一组合理的解。 
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
inline int read(){
	char c;int rec=0;
	while((c=getchar())<'0'||c>'9');
	while(c>='0'&&c<='9')rec=rec*10+c-'0',c=getchar();
	return rec;
}
int n;
struct Event {int st,ed;} w[5005];
struct Branch {int next,to;}f[100005],g[100005];
int h[50005],e[50005],cnt;
inline void add(int x,int y){
	f[++cnt].to=y;f[cnt].next=h[x];h[x]=cnt;
	g[cnt].to=x;g[cnt].next=e[y];e[y]=cnt;
	return ;
}
int tot,group[5005];
int a[5005],b[5005],vis[5005];
inline void DfsF(int v){
	vis[v]=1;
	for(int i=h[v];i;i=f[i].next){
		int j=f[i].to;
		if(!vis[j])DfsF(j);
	}a[++tot]=v;return ;
}
inline void DfsG(int v){
	vis[v]=0;group[v]=tot;
	for(int i=e[v];i;i=g[i].next){
		int j=g[i].to;
		if(vis[j]==1)DfsG(j);
	}return ;
}
bool Check(){
	for(int i=1;i<=n;i++)
		if(group[i*2-1]==group[i*2])return 1;
	return 0;
}
inline void Delete(int v){
	vis[v]=2;
	for(int i=e[v];i;i=g[i].next){
		int j=g[i].to;
		if(vis[j]==0&&group[v]==group[j])Delete(j);
	}return ;
}
inline void Sat(int v){
	vis[v]=1;
	for(int i=e[v];i;i=g[i].next){
		int j=g[i].to;
		if(vis[j]==0&&group[v]==group[j])Sat(j);
	}if(v%2==0)Delete(v-1);else Delete(v+1);return ;
}
inline void Print(int v){
	int t;
	for(int i=0;i<=1;i++){
		t=i?w[v].ed:w[v].st;t/=60;
        if(t<10)cout<<0;cout<<t<<":";
        t=i?w[v].ed:w[v].st;t%=60;
        if(t<10)cout<<0;cout<<t;
        cout<<" ";
	}cout<<'\n';return ;
}
int main(){
	n=read();int x,y,z;
	for(int i=1;i<=n;i++){
		x=read()*60+read();
		y=read()*60+read();
		z=read();
		w[i*2-1].st=x;w[i*2-1].ed=x+z;
		w[i*2].st=y-z;w[i*2].ed=y;
	}
	for(int i=1;i<=n<<1;i++)
        for(int j=1;j<=n<<1;j++)
            if(((i+1)/2!=(j+1)/2)&&!(w[i].ed<=w[j].st||w[i].st>=w[j].ed)){  
		        int k;
				if(j%2==1)k=j+1;else k=j-1;
                add(i,k);
         }
	for(int i=1;i<=n<<1;i++)
		if(!vis[i])DfsF(i);
	tot=0;
	for(int i=n<<1;i>=1;i--)
		if(vis[a[i]]==1)
			b[++tot]=a[i],DfsG(a[i]);
	if(Check()){cout<<"NO";return 0;}
	cout<<"YES\n";
	for(int i=tot;i>=1;i--)
		if(vis[b[i]]==0)
		    Sat(b[i]);
	for(int i=1;i<=n<<1;i++)
		if(vis[i]==1)
			Print(i);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值