poj 3683: Priest John's Busiest Day(2-sat 输出解)

www.cnblogs.com/shaokele/


Priest John's Busiest Day

  Time Limit: 2 Sec
  Memory Limit: 64 MB

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
  

题目地址:  poj 3683
题目大意:

  题目已经很简洁了>_<

题解:

  先判是否有解
  然后对于 A 和 'A
  它们所在的联通块是不能同时选的
  重新建图
  拓扑一下搞出顺序
  就完了


AC代码

//poj3683
#include <cstdio> 
#include <algorithm>
using namespace std;
const int N=1e3+5;
int n,cnt,_cnt;
int S1[N],T1[N],S2[N],T2[N],last[N<<1],_last[N<<1];
int op[N<<1];
inline 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;
}
bool judge(int S1,int T1,int S2,int T2){
    if(T1<=S2 || S1>=T2)return 0;
    return 1;
}
struct edge{
    int to,next;
}e[(N*N)<<2],_e[(N*N)<<2];
void add_edge(int u,int v){
    e[++cnt]=(edge){v,last[u]};last[u]=cnt;
}
int in[N<<1];
void _add_edge(int u,int v){
    in[v]++;
    _e[++_cnt]=(edge){v,_last[u]};_last[u]=_cnt;
}
int ind,top,scc;
int q[N<<1],dfn[N<<1],low[N<<1],col[N<<1];
bool inq[N<<1];
void Tarjan(int u){
    dfn[u]=low[u]=++ind;
    q[++top]=u;inq[u]=1;
    for(int i=last[u];i;i=e[i].next){
        int v=e[i].to;
        if(!dfn[v]){
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(inq[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u]){
        int now=0;scc++;
        while(now!=u){
            now=q[top--];
            col[now]=scc;
            inq[now]=0;
        }
    }
}
int mark[N<<1];
void dfs(int u){
    if(mark[u])return;
    mark[u]=-1;
    for(int i=_last[u];i;i=_e[i].next)
        dfs(_e[i].to);
}
void topsort(){
    top=0;
    for(int i=1;i<=scc;i++)
        if(!in[i])q[++top]=i;
    while(top){
        int u=q[top--];
        if(mark[u])continue;
        mark[u]=1;dfs(op[u]);
        for(int i=_last[u];i;i=_e[i].next){
            int v=_e[i].to;
            in[v]--;
            if(!in[v])q[++top]=v;
        }
    }
}
void print(int x){
    printf("%.2d:",x/60);
    printf("%.2d ",x%60);
}
int main(){
    n=read();
    for(int i=1;i<=n;i++){
        S1[i]=read()*60+read();
        T2[i]=read()*60+read();
        int x=read();
        T1[i]=S1[i]+x;
        S2[i]=T2[i]-x;
    }
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++){
            if(judge(S1[i],T1[i],S1[j],T1[j])){
                add_edge(i*2,j*2-1);
                add_edge(j*2,i*2-1);
            }
            if(judge(S1[i],T1[i],S2[j],T2[j])){
                add_edge(i*2,j*2);
                add_edge(j*2-1,i*2-1);
            }
            if(judge(S2[i],T2[i],S1[j],T1[j])){
                add_edge(i*2-1,j*2-1);
                add_edge(j*2,i*2);
            }
            if(judge(S2[i],T2[i],S2[j],T2[j])){
                add_edge(i*2-1,j*2);
                add_edge(j*2-1,i*2);
            }
        }
    for(int i=1;i<=n*2;i++)
        if(!dfn[i])Tarjan(i);
    for(int i=1;i<=n;i++)
        if(col[i*2-1]==col[i*2]){
            puts("NO");
            return 0;
        }
    puts("YES");
    for(int u=1;u<=2*n;u++)
        for(int i=last[u];i;i=e[i].next)
            if(col[u]!=col[e[i].to])
                _add_edge(col[e[i].to],col[u]);
    for(int i=1;i<=n;i++){
        op[col[i*2]]=col[i*2-1];
        op[col[i*2-1]]=col[i*2];
    }
    topsort();
    for(int i=1;i<=n;i++) 
        if(mark[col[i*2]]==1)
            print(S1[i]),print(T1[i]),puts("");
        else print(S2[i]),print(T2[i]),puts("");
    return 0;
}

转载于:https://www.cnblogs.com/shaokele/p/9455438.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要求选择一些区间,使得这些区间的右端点都小于等于k,并且权重之和最大。请问最大的权重和是多少? 决这个问题的思路是使用动态规划。首先,将区间按照左端点从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端点小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端点小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所求的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值