Priest John's Busiest Day(POJ 3683)
- 原题如下:
Priest John's Busiest DayTime Limit: 2000MS Memory Limit: 65536K Total Submissions: 12162 Accepted: 4138 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 20Sample Output YES 
 08:00 08:30
 08:40 09:00
- 题解:定义变量xi用于表示对于结婚仪式i在开始还是结束时进行特别仪式:xi为真↔在开始时进行特别仪式
 这样,对于结婚仪式i和j,如果Si~Si+Di和Sj~Sj+Dj冲突,就有¬xi∨¬xj为真。对于开始和结束、结束和开始、结束和结束三种情况,也可以得到类似的条件。于是,要保证所有特别仪式的时间不冲突,只要考虑将所有的子句用∧连接起来所得到的布尔公式就好了。对于输入样例,可以得到布尔公式(¬x1∨¬x2)∧(x1∨¬x2)∧(x1∨x2),当x1为真而x2为假时,其值为真。这样原问题就转为了2-SAT问题。
 注:判断两个区间[s1, e1]、[s2, e2]是否相交:若max(s1, s2)<min(e1, e2)为真,则两区间相交。
- 代码:
#include <cstdio> 
 #include <algorithm>
 #include <vector>
 #include <stack>
 #include <cstring> using namespace std; const int MAX_N=;
 int N, V;
 int S[MAX_N], T[MAX_N], D[MAX_N];
 vector<int> G[MAX_N*];
 stack<int> s;
 int dfn[MAX_N*], low[MAX_N*];
 int index;
 int cmp[MAX_N*];
 bool instack[MAX_N*];
 int componentnumber; void add_edge(int x, int y)
 {
 G[x].push_back(y);
 } void tarjan(int i)
 {
 dfn[i]=low[i]=index++;
 instack[i]=true;
 s.push(i);
 int j;
 for (int e=; e<G[i].size(); e++)
 {
 j=G[i][e];
 if (dfn[j]==-)
 {
 tarjan(j);
 low[i]=min(low[i], low[j]);
 }
 else
 if (instack[j]) low[i]=min(low[i], dfn[j]);
 }
 if (dfn[i]==low[i])
 {
 componentnumber++;
 do
 {
 j=s.top();
 s.pop();
 instack[j]=false;
 cmp[j]=componentnumber;
 }
 while (j!=i);
 }
 } int main()
 {
 memset(dfn, -, sizeof(dfn));
 scanf("%d", &N);
 for (int i=; i<N; i++)
 {
 int a, b, c, d;
 scanf("%d:%d %d:%d %d", &a, &b, &c, &d, &D[i]);
 S[i]=a*+b;
 T[i]=c*+d;
 }
 V=N*;
 for (int i=; i<N; i++)
 {
 for (int j=; j<i; j++)
 {
 if (min(S[i]+D[i], S[j]+D[j])>max(S[i], S[j]))
 {
 add_edge(i, N+j);
 add_edge(j, N+i);
 }
 if (min(S[i]+D[i], T[j])>max(S[i], T[j]-D[j]))
 {
 add_edge(i, j);
 add_edge(N+j, N+i);
 }
 if (min(T[i], S[j]+D[j])>max(T[i]-D[i], S[j]))
 {
 add_edge(N+i, N+j);
 add_edge(j, i);
 }
 if (min(T[i], T[j])>max(T[i]-D[i], T[j]-D[j]))
 {
 add_edge(N+i, j);
 add_edge(N+j, i);
 }
 }
 }
 for (int i=; i<V; i++)
 {
 if (dfn[i]==-) tarjan(i);
 }
 for (int i=; i<N; i++)
 {
 if (cmp[i]==cmp[N+i])
 {
 printf("NO\n");
 return ;
 }
 }
 printf("YES\n");
 for (int i=; i<N; i++)
 {
 if (cmp[i]<=cmp[N+i])
 {
 printf("%02d:%02d %02d:%02d\n", S[i]/, S[i]%, (S[i]+D[i])/, (S[i]+D[i])%);
 }
 else
 {
 printf("%02d:%02d %02d:%02d\n", (T[i]-D[i])/, (T[i]-D[i])%, T[i]/, T[i]%);
 }
 }
 }
Priest John's Busiest Day(POJ 3683)的更多相关文章
- 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 ... 
- POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)
		Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10010 Accep ... 
- POJ  3683  Priest John's Busiest Day (2-SAT)
		Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6900 Accept ... 
- poj 3686 Priest John's Busiest Day
		http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ... 
- 【POJ3683】Priest John's Busiest Day
		题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ... 
- 图论(2-sat):Priest John's Busiest Day
		Priest John's Busiest Day Description John is the only priest in his town. September 1st is the Jo ... 
- 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 ... 
- POJ3683 Priest John's Busiest Day(2-SAT)
		Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11049 Accepted: 3767 Special Judge ... 
- HDU 2491 Priest John's Busiest Day(贪心)(2008 Asia Regional Beijing)
		Description John is the only priest in his town. October 26th is the John's busiest day in a year be ... 
随机推荐
- C#LeetCode刷题之#443-压缩字符串(String Compression)
			问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3943 访问. 给定一组字符,使用原地算法将其压缩. 压缩后的长度 ... 
- C#LeetCode刷题之#500-键盘行(Keyboard Row)
			问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3796 访问. 给定一个单词列表,只返回可以使用在键盘同一行的字母 ... 
- vue或者js中平均分割数组
			vue中 把一段长数组按照指定份数 均分 sliceArray(array, size) { var result = []; for (var x = 0; x < Math.ceil(arr ... 
- pycharm激活,此方法为永久激活。
			1.下载JetbrainsCrack-3.1-release-enc.jar文件 链接: https://pan.baidu.com/s/1eN4paXtLVLeUN1nLP335rA 提取码: yg ... 
- 群晖系统设置链路聚合并配置静态IP的教程【江东网 JDX86.COM】
			1.进入控制面板 > 网络 > 网络接口.请单击创建 > 创建 Bond 2.进入聚合配置向导,选择你想要的模式,这里有几种模式意思分别为: 自适应负载平衡: 此模式优化了 Syno ... 
- 华为云配置Objected-based Storage System
			本实验要求基于开源的对象文件系统(例如Ceph),搭建视频点播(VoD)网站.可参考使用Ceph, wordpress, php 以及nginx 实现相关功能,主要包含以下方面: 配置Ceph; 配置 ... 
- Ubuntu 磁盘满了处理方法。
			Ubuntu 磁盘满了处理方法: 1. 如果是虚拟机安装ubuntu,直接给虚拟机安装ubuntu 系统所在的盘符动态分配一点磁盘容量,就可以了. 2. 如果不是虚拟机安装ubuntu,那么有两个办法 ... 
- 问卷星的数据导入spss后变量乱码如何处理?
			一般是字符编码问题.打开一个空的SPSS数据集,选择[编辑]-[选项]-[常规]-[数据和语法的字符编码].修改下当前的编码系统,原来是第一种就换成第二种,原来是第二种就换成第一种,打开一个数据再看看 ... 
- 封装Vue Element的upload上传组件
			本来昨天就想分享封装的这个upload组件,结果刚写了两句话,就被边上的同事给偷窥上了,于是在我全神贯注地写分享的时候他就神不知鬼不觉地突然移动到我身边,腆着脸问我在干啥呢.卧槽你妈,当场就把我吓了一 ... 
- [状压DP]P1441 题解 砝码称重
			前置知识:状压DP 洛谷传送门 emm....看到题目,我第一个想到的就是枚举.暴力大法好! 具体怎么枚举?当然是子集枚举啦!枚举出每一个可能的砝码选择方案.对于每一个合法的(也就是选取数量等于\(n ... 
