zoukankan      html  css  js  c++  java
  • poj3683

    Priest John's Busiest Day
    Time Limit: 2000MS   Memory Limit: 65536K
             

    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
    题意:题意:一个牧师必须参加所给出所有婚礼,可以在Si参加到Si+Di或者从Ti-Di参加到Ti,参加1个即可,牧师不能
    同时参加两个婚礼,求能否参加并且输出参加时间表。
    题解:典型的2-sat题目,把从一开始参加的时间段和从中途参加的时间段看成两个点,如果两个点在同一强连通分量中,就不行.
    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<string>
    #include<queue>
    using namespace std;
    // 0--n-1 1   n -- 2n-1 0
    const int maxn = 2005, maxm = 5000000;
    int tot,h[maxn], hh[maxn],ru[maxn],t[maxn],top,dfn[maxn],low[maxn],idx,place[maxn],scccnt;
    int opp[maxn], color[maxn],st[maxn], ed[maxn], st1[maxn], ed1[maxn], d[maxn];
    int n,m;
    queue <int> Q;vector <int> vec[maxn];
    bool ins[maxn];
    struct edge{
        int u,to,nxt;
    }G[maxm];
    int get(string a){
        int x = 60*(a[0]-'0')*10+60*(a[1]-'0');
        int y = (a[3]-'0')*10+a[4]-'0';
        return x+y;
    }
    
    void add(int u,int v){G[++tot].nxt = h[u];G[tot].u = u;G[tot].to = v;h[u] = tot;}
    bool check(int i, int j){
        if(st[i] <= ed1[j] && ed1[j] < st1[i] && st1[i] <= ed[j])return 1;
        if(st[i] >= ed1[j] && ed[j] > st[i] && st1[i] >= ed[j])return 1;
        if(st[i] <= ed1[j] && ed[j] <= st1[i])return 1;
        if(st[i] >= ed1[j] && ed[j] >= st1[i])return 1;
        return 0;
    }
    void build(int i, int j){
        if(st[i] > st[j])swap(i, j);
        if(st[j]<st1[i]){
            add(i, j+n);
            add(j, i+n);
        }
        if(ed1[i] > ed1[j])swap(i, j);
        if(ed1[j]<ed[i]){
            add(j+n, i);
            add(i+n, j);
        }
        if(check(i, j)){//sti edj
            add(i, j);
            add(j+n, i+n);
        }
        if(check(j, i)){//stj edi
            add(j, i);
            add(i+n, j+n);
        }
    } 
    void tarjan(int x){
        dfn[x] = low[x] = ++idx;
        t[++top] = x, ins[x] = 1;
        for(int i = h[x]; i; i = G[i].nxt){
            int v = G[i].to;
            if(!dfn[v]){
                tarjan(v);
                low[x] = min(low[x], low[v]);
            }
            else if(ins[v]) low[x] = min(low[x], dfn[v]);
            
        }
        
        if(dfn[x] == low[x]){
            scccnt++;
            while(1){
                place[t[top]] = scccnt;
                ins[t[top--]] = 0;
                if(t[top+1] == x)break;
            }
        }
    }
    void topsort(){
        for(int i = 1; i <= scccnt; i++)
            if(!ru[i])Q.push(i);
        while(!Q.empty()){
            int u = Q.front(); Q.pop();
            if(!color[u]){
                color[u] = 1;
                color[opp[u]] = 2;
            }
            for(int i = 0; i < vec[u].size(); i++){
                int v = vec[u][i];
                ru[v]--;
                if(!ru[v]) Q.push(v);
            }        
        }
        
        
    }
    void tb(int i){
        int qs = st[i], qd = st1[i];
        if(i > n)qs = ed1[i-n], qd = ed[i-n];
        printf("%02d:%02d %02d:%02d
    ",qs/60,qs%60,qd/60,qd%60);
        
    }
    int main(){
        
        bool ans = 1;
        scanf("%d",&n);
        for(int i = 1; i <= n; i++){
            string a, b;
            cin>>a>>b;
            scanf("%d",&d[i]);
            st[i] = get(a);
            st1[i] = st[i]+d[i];
            ed[i] = get(b);
            ed1[i] = ed[i]-d[i];
        }
        for(int i = 1; i <= n; i++)
            for(int j = i+1; j <= n; j++)
                build(i, j);
        for(int i = 1; i <= 2*n; i++)
            if(!dfn[i])tarjan(i);
        for(int i = 1; i <= n; i++)
            if(place[i] == place[i+n])ans = 0;
            else opp[place[i]] = place[i+n], opp[place[i+n]] = place[i];
        if(!ans){
            puts("NO");return 0; 
        }
        //for(int i = 0; i<n ;i++)cout<<place[i]<<" "<<place[i+n]<<endl;
        for(int i = 1; i <= 2*n; i++)
            for(int j = h[i]; j; j = G[j].nxt){
                int v = G[j].to;
                if(place[v] != place[i])
                    vec[place[v]].push_back(place[i]), ru[place[i]]++;
            }
        topsort();
        puts("YES");
        for(int i = 1; i <=n; i++)
            if(color[place[i]]==1)tb(i);
            else tb(i+n);
        
    }
    /*
    5
    10:00 10:20 10
    11:00 12:00 30
    11:30 12:40 20
    10:15 11:00 20
    10:30 11:00 25
    */
    View Code
    
    
    




  • 相关阅读:
    ztz11的noip模拟赛T3:评分系统
    20181030NOIP模拟赛T3
    20181030NOIP模拟赛T2
    20181030noip模拟赛T1
    20181029NOIP模拟赛T3
    20181029NOIP模拟赛T2
    20181029noip模拟赛T1
    最优贸易(tarjan,spfa)
    [ZJOI2009]假期的宿舍(二分图匹配)
    上白泽慧音(tarjan,图的染色)
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/8995376.html
Copyright © 2011-2022 走看看