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

网友投稿 339 2022-08-31

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 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

208:00 09:00 3008:15 09:00 20

Sample Output

YES08:00 08:3008:40 09:00

题意

一个小镇里面只有一个牧师,现在有些新人要结婚,需要牧师分别去主持一个仪式,给出每对新人婚礼的开始时间 s 和结束时间 t ,还有他们俩的这个仪式需要的时间(每对新人需要的时间长短可能不同) d ,牧师可以在婚礼开始的时间 d 内(s 到 s+d)或者是结束前的时间 d 内(t - d 到 t)完成这个仪式。现在问能否给出一种安排,让牧师能完成所有夫妇婚礼的仪式,如果可以,输出一种安排。

思路

对于每一个结婚仪式 i ,只有在开始或结束时进行特别仪式两种选择,因此可以定义变量 xi ,xi 为真代表 i

则对于结婚仪式 i,j ,假如 xi,xj 冲突,那么 ¬xi∨¬xj

同理我们再对开始和结束、结束和开始、结束和结束这几种情况分别得出所有子句,最终的合取范式为: (¬xi∨¬xj)∧(xi∨¬xj)∧(¬xi∨xj)∧(xi∨xj)

于是这个问题便转化为 2-SAT 问题啦。

AC 代码

#include#include#include#includeusing namespace std;typedef __int64 LL;const int maxn = 4e6+10;class TwoSAT{public: struct node { int to; int next; } edge[maxn]; int head[maxn],tot,n; bool vis[maxn]; int S[maxn],top; void init(int n) { memset(head,-1,sizeof(head)); tot = 0; this->n = n; } void addedge(int u,int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } bool dfs(int x) { if(vis[x^1])return false; if(vis[x])return true; vis[x] = true; S[top++] = x; for(int i=head[x]; i!=-1; i=edge[i].next) if(!dfs(edge[i].to)) return false; return true; } bool solve() { memset(vis,false,sizeof(vis)); for(int i=0; i P;vector

cnt;int n;void build(){ for(int i=0; imax(cnt[i].first,cnt[j].first)) sat.addedge(i,j^1),sat.addedge(j,i^1); if(min(cnt[i].second,cnt[j^1].second)>max(cnt[i].first,cnt[j^1].first)) sat.addedge(i,j),sat.addedge(i^1,j^1); if(min(cnt[i^1].second,cnt[j].second)>max(cnt[i^1].first,cnt[j].first)) sat.addedge(i^1,j^1),sat.addedge(j,i); if(min(cnt[i^1].second,cnt[j^1].second)>max(cnt[i^1].first,cnt[j^1].first)) sat.addedge(i^1,j),sat.addedge(j^1,i); } }}int main(){ while(~scanf("%d",&n)) { cnt.clear(); n*=2; sat.init(n); int h1,m1,h2,m2,d; for(int i=0; i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:HDU 6152 Friend-Graph (最大团)
下一篇:乐评营销第 5 年,网易云音乐用一个公式升级!(什么叫乐评)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~