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


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

    传送门

    Priest John's Busiest Day
    Time Limit: 2000MS		Memory Limit: 65536K
    Total Submissions: 9874		Accepted: 3381		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
    

    2-SAT的重点就在于建图,对于此题,显然每个时间段我们只能选择前D[i]时间或者后D[i]段时间,我们根据每两队时间限制进行连边,例如
    min(S[i]+D[i],S[j]+D[j])>max(S[i],S[j]),我们只能选择其中之一在前半段,另一个在后半段了,这样建了一个新图后,我们求其强连通分量。如果一个时间的前半段和后半段在同一个强联通分量中,很显然出现了矛盾,此时无解。而输出答案时我们需要利用到一个结论:
    x所在的强连通分量的拓扑序在!x所在的强连通分量的拓扑序之后,则x为真。
    这个结论是充要的,所以反之亦然。

    #include <queue>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #define ll long long
    #define inf 1000000000LL
    #define mod 1000000007
    using namespace std;
    int read()
    {
        int x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    const int N=2e3+10;
    int S[N],T[N],D[N];
    vector<int> G[N];       //图
    vector<int>rG[N];       //反向图
    vector<int>vs;          //后序遍历的顶点列表
    bool vis[N];
    int  cmp[N];             //所属强连通分量的拓扑序
    int sum[N];
    int n,tn;
    void Addedge(int from,int to){
        G[from].push_back(to);
        rG[to].push_back(from);
    }
    void dfs(int v){
        vis[v]=true;
        for(int i=0;i<G[v].size();i++){
            if(!vis[G[v][i]]) dfs(G[v][i]);
        }
        vs.push_back(v);
    }
    void rdfs(int v,int k){
        vis[v]=true;
        cmp[v]=k;
        sum[k]++;
        for(int i=0;i<rG[v].size();i++){
             if(!vis[rG[v][i]]) rdfs(rG[v][i],k);
        }
    }
    int scc(){
        memset(vis,0,sizeof(vis));
        vs.clear();
        for(int v=0;v<n;v++){
              if(!vis[v]) dfs(v);
        }
        memset(vis,0,sizeof(vis));
        int k=0;
        for(int i=vs.size()-1;i>=0;i--){
               if(!vis[vs[i]]) rdfs(vs[i],k++); //遍历每个联通分量的点集
        }
        return k;
    }
    int cacu(char *str){
        int tmp=0,h[2],top=0;
        for(int j=0;j<strlen(str);j++){
             if(str[j]==':'){
                    h[top++]=tmp;
                    tmp=0;
              }else{
                    tmp=tmp*10+str[j]-'0';
              }
              if(j==strlen(str)-1){
                    h[top++]=tmp;
              }
        }
        return h[0]*60+h[1];
    }
    void build(){
        n=tn*2;
        for(int i=0;i<tn;i++){
            for(int j=i+1;j<tn;j++){
                if(min(S[i]+D[i],S[j]+D[j])>max(S[i],S[j])){
                    Addedge(i,tn+j);    
                    Addedge(j,tn+i);       
                }
                if(min(S[i]+D[i],T[j])>max(S[i],T[j]-D[j])){
                    Addedge(i,j);       
                    Addedge(tn+j,tn+i);
                }
                if(min(T[i],S[j]+D[j])>max(T[i]-D[i],S[j])){
                    Addedge(tn+i,tn+j);
                    Addedge(j,i);
                }
                if(min(T[i],T[j])>max(T[i]-D[i],T[j]-D[j])){
                    Addedge(tn+i,j);
                    Addedge(tn+j,i);
                }
            }
        }
    }
    int main(){
        tn=read();
        char str1[20],str2[20];
        for(int i=0;i<tn;i++){
            scanf("%s%s%d",str1,str2,&D[i]);
            S[i]=cacu(str1);T[i]=cacu(str2);
        }
        build();
        scc();
        for(int i=0;i<tn;i++){
            if(cmp[i]==cmp[tn+i]){
                puts("NO");
                return 0;
            }
        }
        puts("YES");
        for(int i=0;i<tn;i++){
           if(cmp[i]>cmp[tn+i]){
                printf("%02d:%02d %02d:%02d
    ",S[i]/60,S[i]%60,(S[i]+D[i])/60,(S[i]+D[i])%60);
           }else{
                printf("%02d:%02d %02d:%02d
    ",(T[i]-D[i])/60,(T[i]-D[i])%60,T[i]/60,T[i]%60);
           }
        }
        return 0;
    }
    
  • 相关阅读:
    Mininet系列实验(六):Mininet动态改变转发规则实验
    Mininet系列实验(五):Mininet设置带宽之简单性能测试
    Mininet系列实验(三):Mininet命令延伸实验扩展
    Mininet系列实验(一):Mininet使用源码安装
    集合初始化器概览(Visual Basic)
    为什么开发人员不能估算时间?
    Lambda 表达式 Lambda Expressions (Visual Basic)
    宽松委托转换(Relaxed delegate conversion)
    [翻译]SQL Server 未公开的两个存储过程sp_MSforeachtable 和 sp_MSforeachdb
    Visual Basic 2010 新特性
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/6808963.html
Copyright © 2020-2023  润新知