poj3683 Priest John's Busiest Day

http://www.elijahqi.win/archives/3069
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

POJ Founder Monthly Contest – 2008.08.31, Dagger and Facer
仍然是将婚礼先拆成两个点 反正不在前半段就在后半段 然后利用和上一题的思想 先把时间转化为线段 然后再判断相交先tarjan然后再拓扑排序 将我的方案试出 然后icefox巨佬说其实并不需要每回深搜 只要针对我当前这个位置 还有我相反状态改一下即可 因为拓扑序可以看作是倒着做 然后我相反状态的顺序看作正着做 一路其实都已经改对了

#include<queue>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline bool read(int &x){
    x=0;int f=1;char ch=gc();if (ch==EOF) return 0;
    while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=gc();x*=f;
    return 1;
}
const int N=2010;
const int M=2e6+20;
struct node1{
    int st,ed;
}p[N];
struct node{
    int y,next;
}data[M],data1[M];
int h[N],h1[N],num,id1[N],id2[N],in[N];
inline void insert1(int x,int y){
    data[++num].y=y;data[num].next=h[x];h[x]=num;
}
inline void insert2(int x,int y){
    data1[++num].y=y;data1[num].next=h1[x];h1[x]=num;
}
int dfn[N],low[N],q[N],top,col[N],b[N],s,n,op[N];
bool stackf[N];
inline void tarjan(int x){
    dfn[x]=low[x]=++num;stackf[x]=1;q[++top]=x;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;
        if (!dfn[y]) tarjan(y),low[x]=min(low[x],low[y]);else if(stackf[y]) low[x]=min(low[x],dfn[y]);
    }
    if (low[x]==dfn[x]){
        int y;++s;
        do{y=q[top--];stackf[y]=0;b[y]=s;
        }while(y!=x);
    }
}
inline bool check(int i,int j){
    return !(p[i].st>=p[j].ed||p[i].ed<=p[j].st);
}
int main(){
    freopen("poj3683.in","r",stdin);
    read(n);
    for (int i=0;i<n;++i){
        static int a,b,c,d,last;
        read(a);read(b);read(c);read(d);read(last);
        p[2*i].st=a*60+b,p[2*i].ed=a*60+b+last;id1[i]=2*i;
        p[2*i+1].st=c*60+d-last,p[2*i+1].ed=c*60+d;id2[i]=2*i+1;
    }num=0;
//  for (int i=0;i<n<<1;++i) printf("%d %d\n",p[i].st,p[i].ed); 
    for (int i=0;i<n;++i){
        for (int j=0;j<n;++j){
            if (i==j) continue;
            if (check(id1[i],id1[j])) insert1(id1[i],id2[j]);
            if (check(id1[i],id2[j])) insert1(id1[i],id1[j]);
            if (check(id2[i],id1[j])) insert1(id2[i],id2[j]);
            if (check(id2[i],id2[j])) insert1(id2[i],id1[j]);
        }
    }
    //for (int i=1;i<=num;++i) printf("%d %d\n",data[i].x,data[i].y);
    num=0;s=0;
    for (int i=0;i<n<<1;++i) if (!dfn[i]) tarjan(i);
    queue<int>q;memset(col,-1,sizeof(col));num=0;
    for (int i=0;i<n<<1;++i){
        for (int j=h[i];j;j=data[j].next){
            int y=data[j].y;if (b[i]==b[y]) continue;
            insert2(b[y],b[i]);++in[b[i]];
        }
    }
    for (int i=1;i<=s;++i) if (!in[i]) q.push(i);
    for (int i=0;i<n;++i){
        op[b[id1[i]]]=b[id2[i]];
        op[b[id2[i]]]=b[id1[i]];
        if (b[i<<1]==b[i<<1|1]) {puts("NO");return 0;}
    }puts("YES");
    while(!q.empty()){
        int x=q.front();q.pop();
        if (col[x]==-1) col[x]=1,col[op[x]]=0;
        for (int i=h1[x];i;i=data1[i].next){
            int y=data1[i].y;
            if (!--in[y]) q.push(y);
        }
    }
    for (int i=0;i<n;++i){
        if (col[b[id1[i]]]) printf("%02d:%02d %02d:%02d\n",p[id1[i]].st/60,p[id1[i]].st%60,p[id1[i]].ed/60,p[id1[i]].ed%60);
        else printf("%02d:%02d %02d:%02d\n",p[id2[i]].st/60,p[id2[i]].st%60,p[id2[i]].ed/60,p[id2[i]].ed%60);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值