POJ 3683 Priest John's Busiest Day (2-SAT)
来源:互联网 发布:红帆oa软件 编辑:程序博客网 时间:2024/06/04 18:49
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)完成这个仪式。现在问能否给出一种安排,让牧师能完成所有夫妇婚礼的仪式,如果可以,输出一种安排。
思路
对于每一个结婚仪式
则对于结婚仪式
同理我们再对开始和结束、结束和开始、结束和结束这几种情况分别得出所有子句,最终的合取范式为:
于是这个问题便转化为 2-SAT 问题啦。
AC 代码
#include<iostream>#include<cstring>#include<cstdio>#include<vector>using 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<n; i+=2) { if(vis[i]||vis[i^1])continue; top = 0; if(!dfs(i)) { while(top) vis[S[--top]] = false; if(!dfs(i^1))return false; } } return true; }} sat;typedef pair<int,int> P;vector<P> cnt;int n;void build(){ for(int i=0; i<n; i+=2) { for(int j=i+2; j<n; j+=2) { if(min(cnt[i].second,cnt[j].second)>max(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<n; i+=2) { scanf("%d%*c%d%d%*c%d%d",&h1,&m1,&h2,&m2,&d); h1 = h1*60+m1; h2 = h2*60+m2; cnt.push_back(P(h1,h1+d)); cnt.push_back(P(h2-d,h2)); } build(); if(!sat.solve()) puts("NO"); else { puts("YES"); for(int i=0; i<n; i+=2) { int index = sat.vis[i]?i:i^1; printf("%02d:%02d %02d:%02d\n",cnt[index].first/60,cnt[index].first%60,cnt[index].second/60,cnt[index].second%60); } } } return 0;}
- POJ 3683 Priest John's Busiest Day(2-SAT)
- poj - 3683 - Priest John's Busiest Day(2-SAT)
- POJ 3683 Priest John's Busiest Day(2-sat)
- poj 3683 Priest John's Busiest Day(2-SAT)
- POJ 3683 Priest John's Busiest Day(2-SAT)
- POJ 3683 Priest John's Busiest Day (2-SAT)
- POJ 3683 Priest John's Busiest Day(2-SAT)
- POJ 3683 Priest John's Busiest Day (2-SAT)
- poj 3683 Priest John's Busiest Day 2-sat
- poj 3683 Priest John's Busiest Day 2-SAT
- 2-sat->poj 3683 Priest John's Busiest Day
- 【POJ】3683 Priest John's Busiest Day 2-sat
- poj 3683 Priest John's Busiest Day 2-sat
- poj 3683 Priest John's Busiest Day 2-sat
- POJ 3683 - Priest John's Busiest Day(2-SAT)
- poj 3683 Priest John's Busiest Day 2-SAT
- POJ - 3683 Priest John's Busiest Day(2-SAT)
- poj 3683 Priest John's Busiest Day 2-SAT输出
- Qt递归遍历文件和文件夹
- 百度笔试题分享
- SQLSERVER2008EXPRESS安装教程
- 文章标题
- http://blog.csdn.net/u012043391/article/details/54972127
- POJ 3683 Priest John's Busiest Day (2-SAT)
- 49 款人脸检测/识别的API、库和软件 .
- java单例设计模式
- 对于bresenham画圆算法的一点点理解
- Faster-RCNN训练自己的样本
- 用python画AR模型时序图
- 使用centos(linux)命令行常用命令
- C++类的静态数据成员及其用法
- 操作系统-管程、进程和线程之间的差别