http://poj.org/problem?id=3683

2-sat 问题判定,输出一组可行解

http://www.cnblogs.com/TheRoadToTheGold/p/8436948.html

注:

本代码在判断两个时间段部分有误,数据弱A了

#include<cstdio>
#include<vector> using namespace std; #define N 1001 struct TIME
{
int h1,m1;
int h2,m2;
int tim;
}e[N]; int n;
int tot; int front[N<<],to[N*N*],nxt[N*N*]; int dfn[N<<],low[N<<];
int st[N<<],top;
bool vis[N<<]; int cnt;
int id[N<<];
vector<int>V[N<<]; int FRONT[N<<],TO[N*N*],NXT[N*N*],TOT;
int in[N<<]; bool use[N<<],cut[N<<]; struct ANS
{
int h1,m1;
int h2,m2;
}ans[N]; void add(int u,int v)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
} void ADD(int u,int v)
{
TO[++TOT]=v; NXT[TOT]=FRONT[u]; FRONT[u]=TOT;
in[v]++;
} bool judge(int h1s,int m1s,int h1t,int m1t,int h2s,int m2s,int h2t,int m2t)
{
int s1=(h1s-)*+m1s;
int t1=(h1t-)*+m1t;
int s2=(h2s-)*+m2s;
int t2=(h2t-)*+m2t;
if(s1<=s2)
{
if(s2>=t1) return false;
return true;
}
else
{
if(t2<=s1) return false;
return true;
}
} void tarjan(int u)
{
dfn[u]=low[u]=++tot;
st[++top]=u;
vis[u]=true;
for(int i=front[u];i;i=nxt[i])
{
if(!dfn[to[i]])
{
tarjan(to[i]);
low[u]=min(low[u],low[to[i]]);
}
else if(vis[to[i]]) low[u]=min(low[u],dfn[to[i]]);
}
if(dfn[u]==low[u])
{
cnt++;
while(st[top]!=u)
{
id[st[top]]=cnt;
vis[st[top]]=false;
V[cnt].push_back(st[top--]);
}
id[u]=cnt;
vis[u]=false;
V[cnt].push_back(u);
top--;
}
} void rebuild()
{
for(int k=;k<=n;++k)
{
int i=k<<;
for(int j=front[i];j;j=nxt[j])
if(id[i]!=id[to[j]]) ADD(id[to[j]],id[i]);
i=k<<|;
for(int j=front[i];j;j=nxt[j])
if(id[i]!=id[to[j]]) ADD(id[to[j]],id[i]);
}
} void out(int h,int m)
{
if(m> && m<) printf("%02d:%02d",h,m);
else if(m<)
{
if(!(m%))
{
h+=m/;
printf("%02d:00",h);
}
else
{
h+=m/;
h--;
m%=;
if(m<) m+=;
printf("%02d:%02d",h,m);
}
}
else
{
h+=m/;
m%=;
printf("%02d:%02d",h,m);
}
} void topsort()
{
for(int i=;i<=cnt;++i)
if(!in[i]) st[++top]=i;
int u,v;
while(top)
{
u=st[top--];
if(cut[u]) continue;
use[u]=true;
v=id[V[u][]^];
cut[v]=true;
for(int i=FRONT[u];i;i=NXT[i])
{
in[TO[i]]--;
if(!in[TO[i]]) st[++top]=TO[i];
}
//for(int i=FRONT[v];i;i=NXT[i]) cut[TO[i]]=true;
}
for(int i=;i<=n;++i)
if(use[id[i<<]])
{
ans[i].h1=e[i].h1;
ans[i].m1=e[i].m1;
ans[i].h2=e[i].h1;
ans[i].m2=e[i].m1+e[i].tim;
}
else
{
ans[i].h1=e[i].h2;
ans[i].m1=e[i].m2-e[i].tim;
ans[i].h2=e[i].h2;
ans[i].m2=e[i].m2;
}
puts("YES");
for(int i=;i<=n;++i)
{
out(ans[i].h1,ans[i].m1);
putchar(' ');
out(ans[i].h2,ans[i].m2);
putchar('\n');
}
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;++i)
{
scanf("%d:%d",&e[i].h1,&e[i].m1);
scanf("%d:%d",&e[i].h2,&e[i].m2);
scanf("%d",&e[i].tim);
}
for(int i=;i<n;++i)
for(int j=i+;j<=n;++j)
if(i!=j)
{
if(judge(e[i].h1,e[i].m1,e[i].h1,e[i].m1+e[i].tim,e[j].h1,e[j].m1,e[j].h1,e[j].m1+e[j].tim))
add(i<<,j<<|),add(j<<,i<<|);
if(judge(e[i].h1,e[i].m1,e[i].h1,e[i].m1+e[i].tim,e[j].h2,e[j].m2-e[j].tim,e[j].h2,e[j].m2))
add(i<<,j<<),add(j<<|,i<<|);
if(judge(e[i].h2,e[i].m2-e[i].tim,e[i].h2,e[i].m2,e[j].h1,e[j].m1,e[j].h1,e[j].m1+e[j].tim))
add(i<<|,j<<|),add(j<<,i<<);
if(judge(e[i].h2,e[i].m2-e[i].tim,e[i].h2,e[i].m2,e[j].h2,e[j].m2-e[j].tim,e[j].h2,e[j].m2))
add(i<<|,j<<),add(j<<|,i<<);
}
tot=;
for(int i=;i<=n;++i)
{
if(!dfn[i<<]) tarjan(i<<);
if(!dfn[i<<|]) tarjan(i<<|);
}
for(int i=;i<=n;++i)
if(id[i<<]==id[i<<|])
{
puts("NO");
return ;
}
rebuild();
topsort();
}
Priest John's Busiest Day
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11007   Accepted: 3759   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

Source

poj 3686 Priest John's Busiest Day的更多相关文章

  1. POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题)

    POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题) Descripti ...

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

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6900   Accept ...

  3. POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10010   Accep ...

  4. POJ 3683 Priest John's Busiest Day(2-SAT 并输出解)

    Description John is the only priest in his town. September 1st is the John's busiest day in a year b ...

  5. poj - 3683 - Priest John's Busiest Day(2-SAT)

    题意:有N场婚礼,每场婚礼的开始时间为Si,结束时间为Ti,每场婚礼有个仪式,历时Di,这个仪式要么在Si时刻开始,要么在Ti-Di时刻开始,问能否安排每场婚礼举行仪式的时间,使主持人John能参加所 ...

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

    题意:有n对新人要在同一天结婚.结婚时间为Ti到Di,这里有时长为Si的一个仪式需要神父出席.神父可以在Ti-(Ti+Si)这段时间出席也可以在(Di-Si)-Si这段时间.问神父能否出席所有仪式,如 ...

  7. POJ 3683 Priest John's Busiest Day (2-SAT,常规)

    题意: 一些人要在同一天进行婚礼,但是牧师只有1个,每一对夫妻都有一个时间范围[s , e]可供牧师选择,且起码要m分钟才主持完毕,但是要么就在 s 就开始,要么就主持到刚好 e 结束.因为人数太多了 ...

  8. POJ 3683 Priest John's Busiest Day

    2-SAT简单题,判断一下两个开区间是否相交 #include<cstdio> #include<cstring> #include<cmath> #include ...

  9. POJ 3683 Priest John's Busiest Day[2-SAT 构造解]

    题意: $n$对$couple$举行仪式,有两个时间段可以选择,问是否可以不冲突举行完,并求方案 两个时间段选择对应一真一假,对于有时间段冲突冲突的两人按照$2-SAT$的规则连边(把不冲突的时间段连 ...

随机推荐

  1. SpringBoot日记——编码配置篇

    插入一个小篇章,有人在编写代码的时候,要么控制台乱码,要么页面乱码等等, 我这里有个配置,可以解决各种乱码问题,直接来看. # ==================== 编码配置 ========== ...

  2. Java设置PPT的扇形图,与内嵌Excel联动

    /** * 设置饼图的主方法 * @param slide 图表 * @param index 图标位置 * @param data 需要设置的数据 * @param titles 关联Excel的标 ...

  3. B1030 完美数列 (25 分)

    这是一道二分法的题目,许久不使用二分法,感觉有点生疏. #include<bits/stdc++.h> using namespace std; const int MAXN=100000 ...

  4. SCRUM 12.09 软件工程第二周计划

    第二轮迭代的第二周开始了,上一周我们进行了对代码优化的探索与自我审查. 本周,我们有以下两点目标要实现: 1.对客户端进行优化. 2.网络爬虫爬取美团外卖. 客户端优化主要开发人员:高雅智.牛强.彭林 ...

  5. Linux 第五周 实验: 分析system_call中断处理过程

    姬梦馨 原创博客 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 使用gdb跟踪分析一个系统调用内核函数 ...

  6. 《linux内核》课本第五章读书笔记

  7. SDN交换机迁移2

    关于迁移过程中迁移目标(被迁移的交换机和目标控制器)的选择 SDN中基于过程优化的交换机竞争迁移算法 通信学报 交换机:请求速率大于域内平均请求速率的交换机集合: 控制器:综合网络中时延.流量和控制器 ...

  8. 转帖 OKR

    什么是OKR OKR全称是Objectives and Key Results,即目标与关键成果法.OKR是一套定义和跟踪目标及其完成情况的管理工具和方法.1999年Intel公司发明了这种方法,后来 ...

  9. AOP 貌似是拦截器 对方法进行拦截

    AOP 貌似是拦截器 对方法进行拦截

  10. BZOJ4078 WF2014Metal Processing Plant(二分答案+2-SAT)

    题面甚至没给范围,由数据可得n<=200.容易想到二分答案,暴力枚举某集合的价值,2-SATcheck一下即可.这样是O(n4logn)的. 2-SAT复杂度已经是下界,考虑如何优化枚举.稍微改 ...