Priest John's Busiest Day(POJ 3683)
- 原题如下:
Priest John's Busiest Day
Time 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 ...
随机推荐
- Vue 给页面加水印指令(directive)
页面需要水印 import Vue from 'vue' /** * watermark 指令 * 解决: 给页面生成水印 * 基本原理:给选择器添加背景图片 * 用法:v-watermark=&qu ...
- LeetCode 413 Arithmetic Slices详解
这个开始自己做的动态规划复杂度达到了O(n), 是用的是2维的矩阵来存前面的数据,复杂度太高了, 虽然好理解,但是没效率,后面看这个博客发现没有动态规划做了这个题 也是比较厉害. 转载地址: http ...
- 如何限制ip访问Oracle数据库
一.概述 本文将给大家介绍如何限制某个ip或某个ip段才能访问Oracle数据库 通过sqlnet.ora 通过/etc/hosts.deny和/etc/hosts.allow 通过iptables ...
- DNF手游公测或将只有安卓版 iOS系统怎么办?
DNF手游在8月10号确定延期后,目前还不知道新的上线时间.玩家都很关心DNF手游新的公测时间,DNF手游官网的预约数据也是不断突破新高,最终突破了五千万!我们目前拿到的小道消息,DNF手游会在9月1 ...
- Dbeaver连接Hive和Mysql的配置
1.连接Hive 首选需要配置Hive 这里我们采用的是JDBC的连接方式 (1) 在Hive中后台启动hiveserver2 [root@hadoop-101 hive]# bin/hiveserv ...
- 9.hbase相关进程作用
1.协调服务组件Zookeeper Zookeeper的作用如下: 1. 保证任何时候,集群中只有一个HMaster: 2. 存储所有的HRegion的寻址入口: 3. 实时监控HRegionServ ...
- 【转】Ubuntu下解决Depends: xxx(< 1.2.1) but xxx is to be installed
在ubuntu下由于更新package不成功,或者误删除了一些文件会出现Depends: xxx(< 1.2.1) but xxx is to be installed解决方法是先试着安装所缺的 ...
- 计算机网络-应用层(4)DNS协议
域名系统(Domain Name System, DNS):一个分层的由DNS服务器实现的分布式数据库+一个使得主机能够查询分布式数据库的应用层协议 DNS服务器通常是运行BIND (Berkeley ...
- 如何使用python移除/删除非空文件夹?
移除/删除非空文件夹/目录的最有效方法是什么? 1.标准库参考:shutil.rmtree. 根据设计,rmtree在包含只读文件的文件夹树上失败.如果要删除文件夹,不管它是否包含只读文件,请使用 i ...
- spring4.1及以下跨域配置
springMVC4.1及以下,就需要对该请求配置filter,,设置请求头可支持跨域 1.web.xml配置 <!-- 跨域问题解决 --> <filter> <fil ...