zoukankan      html  css  js  c++  java
  • poj3683(2-SAT 求任意方案)

    基础的2-SAT求任意方案的题目。

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

     
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <vector>
    #include <map>
    #include <queue>
    #include <sstream>
    #include <iostream>
    using namespace std;
    #define INF 0x3fffffff
    #define N 2020
    
    struct node
    {
        int from,to,next;
    }edge[N*N];
    
    int n;
    int b[N],d[N],k[N];
    int stk[N],vis[N],low[N],link[N],mark[N];
    int top,index,id,du[N];//记录入度数
    int pre[N],cnt,g[N];// g 用来记录排序后的结果
    
    int g1[N]; //用来记录缩点后的每一个点所含的点
    
    int check(int x,int y,int x1,int y1)
    {
        if( y<=x1 || x>=y1 )
            return 0;
        return 1;
    }
    
    void dfs(int s)
    {
        mark[s]=1;
        vis[s]=index++;
        low[s]=vis[s];
        stk[top++]=s;
        for(int p=pre[s];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            if(mark[v]==0) dfs(v);
            if(mark[v]==1) low[s]=min(low[s],low[v]);
        }
        if(low[s]==vis[s])
        {
            int tmp;
            id++;
            do
            {
                tmp=stk[top-1];
                link[tmp]=id;
                mark[tmp]=-1;
            }while(stk[--top]!=s);
        }
    }
    
    void add_edge(int u,int v)
    {
        edge[cnt].from=u;
        edge[cnt].to=v;
        edge[cnt].next=pre[u];
        pre[u]=cnt++;
    }
    
    void topsort()
    {
        memset(mark,0,sizeof(mark));
        top=0;
        int tcnt=0;
        for(int i=1;i<=id;i++)
            if(du[i]==0)
            {
                mark[i]=1;
                stk[top++]=i;//把这个节点入栈
                g[tcnt++]=i;
            }
        while(top!=0)
        {
            int cur=stk[--top];
            for(int p=pre[cur];p!=-1;p=edge[p].next)
            {
                int v=edge[p].to;
                if(mark[v]==1) continue;
                du[v]--;
                if(du[v]==0)
                {
                    mark[v]=1;
                    stk[top++]=v;
                    g[tcnt++]=v;
                }
            }
        }
        //排好了序
    
    }
    
    void dfs1(int s)
    {
        mark[s]=-1; //表示这个点不可以选
        for(int p=pre[s];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            dfs1(v);
        }
    }
    
    int main()
    {
        //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        //freopen("C:\Users\Administrator\Desktop\in.txt","w",stdout);
        while(~scanf("%d",&n))
        {
            cnt=0;
            memset(pre,-1,sizeof(pre));
            for(int i=0;i<n;i++)
            {
                int x,y,x1,y1,w;
                scanf("%d:%d %d:%d %d",&x,&y,&x1,&y1,&w);
                x=x*60+y;
                x1=x1*60+y1;
                b[i]=x; d[i]=x1;
                k[i]=w; //分别记录可以用的第一次和第二次
            }
            for(int i=0;i<n;i++)
            {
                for(int j=i+1;j<n;j++)
                {
                    if( check(b[i],b[i]+k[i],b[j],b[j]+k[j]) ) //判断是否有相交部分.
                    {
                         add_edge(2*i,2*j+1);
                         add_edge(2*j,2*i+1);
                    }
                    if( check(b[i],b[i]+k[i],d[j]-k[j],d[j]) )
                    {
                        add_edge(2*i,2*j);
                        add_edge(2*j+1,2*i+1);
                    }
                    if( check(d[i]-k[i],d[i],b[j],b[j]+k[j]))
                    {
                        add_edge(2*i+1,2*j+1);
                        add_edge(2*j,2*i);
                    }
                    if( check(d[i]-k[i],d[i],d[j]-k[j],d[j]))
                    {
                        add_edge(2*i+1,2*j);
                        add_edge(2*j+1,2*i);
                    }
                }
            }
            //构好了图  ,然后就是判断是否可行然后求出可行解
            top=index=id=0;
            memset(mark,0,sizeof(mark));
            for(int i=0;i<2*n;i++)
                if(mark[i]==0)
                    dfs(i);
            int flag=0;
            for(int i=0;i<2*n;i+=2)
            {
                if(link[i]==link[i+1])
                {
                    flag=1;
                    break;
                }
            }
    
            if(flag)
            {
                printf("NO
    ");
            }
            else
            {
                printf("YES
    ");
                //关键的时候,怎么找出所有的可行解
                // 先构建一张逆图先
                int tcnt=cnt;//重新构建一张图
                memset(g1,0,sizeof(g1));
    
                cnt=0;
                memset(pre,-1,sizeof(pre));
                for(int i=0;i<2*n;i++)
                {
                    g1[ link[i] ]=i;
                }
    
                memset(du,0,sizeof(du));
                for(int i=0;i<tcnt;i++)
                {
                    int x=edge[i].from;
                    int y=edge[i].to;
                    if(link[x] != link[y])
                    {
                        add_edge(link[y],link[x]);//建逆图
                        du[ link[x] ]++;
                    }
    
                    //g1[ link[x] ]
                }
                //然后就是topsort了
                topsort();
                memset(mark,0,sizeof(mark));
    
                for(int i=0;i<id;i++) //从最低层开始
                {
                    if(mark[ g[i] ]!=-1)//表示这个点可以选
                    {
                        mark[ g[i] ]=1;
                        //将所有对立可能的点全部标记为不能去
                        int key=g1[ g[i] ];
                        key=key^1;
                        key=link[key]; //对立点所缩成的一个点
                        dfs1(key);
                    }
                }
    
                for(int i=0;i<n;i++)
                {
                    if(mark[ link[2*i] ]==1)
                    {
                        int x,y,x1,y1;
                        x=b[i]/60;
                        y=b[i]%60;
                        x1=(b[i]+k[i])/60;
                        y1=(b[i]+k[i])%60;
                        printf("%02d:%02d %02d:%02d
    ",x,y,x1,y1);
                    }
                    else
                    {
                        int x,y,x1,y1;
                        x=(d[i]-k[i])/60;
                        y=(d[i]-k[i])%60;
                        x1=(d[i])/60;
                        y1=(d[i])%60;
                        printf("%02d:%02d %02d:%02d
    ",x,y,x1,y1);
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    [转]UNI-APP开发笔记之使用uni.navigateBack修改上一个页面值
    [转]移动端人员选择的设计思考
    [转]nginx安装及其配置详细教程
    [转]Vue 使用use、prototype自定义自己的全局组件
    [转]uniapp项目运行支付宝小程序,报错:xxx.json中没有申明component: true
    支付宝(钉钉)小程序使用uView控制台报错Cannot read property 'title-all' of undefined
    [转]commonJS规范和require,import区别
    [转]module.exports和export详解
    [转]如何在组件中去使用vuex的值和方法?
    [转]CryptoJS中AES256(CBC)加密算法简单使用
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3348872.html
Copyright © 2011-2022 走看看