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

Priest John's Busiest Day

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12467 Accepted: 4242 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

题意:

给你同一天里n(n<=1000)件事的开始时间st、结束时间ed和持续时间len,每件事只能在两个时间段进行:

[st,st+len]或者[ed-len,ed]

问你是否存在一种安排使所有事都不矛盾。如果存在,输出每件事的真实开始时间和真实结束时间。

思路:

作为众多加边非常恶心的2-SAT题目之一,这道题就是耗时间的题。。。将时间转化成分钟来算。

需要注意两点:

1、判断两个区间是否相交。

2、输出格式。

具体见代码。

#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn=40010;
int op[maxn],vis[maxn],low[maxn],dfn[maxn];
int pt[maxn],stk[maxn],color[maxn],pos[maxn],deg[maxn];
vector<int>vc[maxn],vc2[maxn];
int n,m,sig,cnt,tot,cont;
struct node
{
    int sta,end;
    int len;
}e[maxn];
int cal(int xx,int yy)
{
    return xx*60+yy;
}
void add(int x,int y){
    vc[x].push_back(y);
}
void top(){
    memset(pt,0,sizeof(pt));
    queue<int>s;
    for(int i=1;i<=sig;i++){
        if(deg[i]==0) s.push(i);
    }
    while(!s.empty()){
        int u=s.front();
        if(pt[u]==0){
            pt[u]=1;
            pt[pos[u]]=2;
        }
        s.pop();
        for(int i=0;i<vc2[u].size();i++){
            int v=vc2[u][i];
            deg[v]--;
            if(deg[v]==0) s.push(v);
        }
    }
    cont=0;
    for(int i=1;i<=n;i++) {
        if(pt[color[i]]==1) op[cont++]=i;
    }
}
void tarjan(int u){
    vis[u]=1;
    dfn[u]=low[u]=cnt++;
    stk[++tot]=u;
    for(int i=0;i<vc[u].size();i++){
        int v=vc[u][i];
        if(vis[v]==0) tarjan(v);
        if(vis[v]==1) low[u]=min(low[u],low[v]);
    }
    if(low[u]==dfn[u]) {
        sig++;
        do{
            vis[stk[tot]]=-1;
            color[stk[tot]]=sig;
        }while(stk[tot--]!=u);
    }
}
void init(){
    sig=0;cnt=1;tot=-1;
    memset(deg,0,sizeof(deg));
    memset(stk,0,sizeof(stk));
    memset(dfn,0,sizeof(dfn));
    memset(vis,0,sizeof(vis));
    memset(color,0,sizeof(color));
    for(int i=0;i<maxn;i++) {
        vc[i].clear();
        vc2[i].clear();
    }
}
int solve(){
    for(int i=1;i<=n*2;i++) {
        if(vis[i]==0) tarjan(i);
    }
    for(int i=1;i<=n;i++) {
        if(color[i]==color[i+n]) return 0;
        pos[color[i]]=color[i+n];
        pos[color[i+n]]=color[i];
    }
    for(int i=1;i<=n*2;i++){
        for(int j=0;j<vc[i].size();j++){
            int v=vc[i][j];
            if(color[i]!=color[v]){
                deg[color[i]]++;
                vc2[color[v]].push_back(color[i]);
            }
        }
    }
    top();
    return 1;
}
bool jud(int x,int y,int xx,int yy)
{
    if(x>y||xx>yy) return 1;
    if(y<=xx) return 0;
    if(yy<=x) return 0;
    return 1;
}
void go(int ans,int w)
{
    int x=ans/60;
    int y=ans%60;
    int xx=(ans+w)/60;
    int yy=(ans+w)%60;
    printf("%02d:%02d %02d:%02d\n",x,y,xx,yy);
}
int main(){
    int T,cas=1;
    while(scanf("%d",&n)!=EOF){
        init();
        int fg=1;
        for(int i=1;i<=n;i++) {
            int x,y,xx,yy;
            scanf("%d:%d %d:%d %d",&x,&y,&xx,&yy,&e[i].len);
            e[i].sta=cal(x,y);
            e[i].end=cal(xx,yy);
            if(e[i].sta+e[i].len>e[i].end) fg=0;
        }
        if(!fg) {puts("NO");continue;}
        for(int i=1;i<=n;i++)
        {
            int tmp1=e[i].sta+e[i].len;
            int tmp2=e[i].end-e[i].len;
            for(int j=i+1;j<=n;j++)
            {
                int tmp3=e[j].sta+e[j].len;
                int tmp4=e[j].end-e[j].len;
                int x=jud(e[i].sta,tmp1,e[j].sta,tmp3);
                int y=jud(e[i].sta,tmp1,tmp4,e[j].end);
                //cout<<x<<" "<<y<<endl;
                if(x&&!y) {add(i,j+n);add(j,i+n);}
                if(x&&y) {add(i,i+n);}
                if(!x&&y) {add(i,j);add(j+n,i+n);}
                x=jud(tmp2,e[i].end,e[j].sta,tmp3);
                y=jud(tmp2,e[i].end,tmp4,e[j].end);
                if(x&&!y) {add(i+n,j+n);add(j,i);}
                if(x&&y) {add(i+n,i);}
                if(!x&&y) {add(i+n,j);add(j+n,i);}
                //cout<<x<<" "<<y<<endl;
            }
        }
        int ans=solve();
        if(ans==1) {
            puts("YES");
            for(int i=1;i<=n;i++)
            if(pt[color[i]]==1)
            {
                //cout<<i<<endl;
                go(e[i].sta,e[i].len);
            }
            else go(e[i].end-e[i].len,e[i].len);
        }
        else puts("NO");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值