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 ...
随机推荐
- 数据源管理 | 分布式NoSQL系统,Cassandra集群管理
本文源码:GitHub·点这里 || GitEE·点这里 一.Cassandra简介 1.基础描述 Cassandra是一套开源分布式NoSQL数据库系统.它最初由Facebook开发,用于储存收件箱 ...
- Window C盘 占满原因之一
最近一段时间,突然C盘 莫名奇妙的满了 ,也没有中毒. 最后查找是因为安装了SQL Reporting 的原因 C:\Program Files\Microsoft SQL Server Repor ...
- vue或者js中平均分割数组
vue中 把一段长数组按照指定份数 均分 sliceArray(array, size) { var result = []; for (var x = 0; x < Math.ceil(arr ...
- mysql-5.7.xx在lcentos7下的安装以及mysql在windows以及linux上的性能差异
前言: 在centos上安装mysql,整整折腾了将近一天,因为是第一次安装,的确是踩了不少坑,这里详细记录下来,方便各位有同样需求的小伙伴参考. 该选择什么版本? mysql5.7有很多小版本,但是 ...
- 区分多个web driver实例
固然可以用加载不同cookie的办法,让3个帐号共享一个web driver登陆,但总感觉切换麻烦,干脆用了3个web driver实例.问题来了,如何区分?不是说程序里如何区分,机器比人聪明,知道外 ...
- 最新通达OA-getshell 漏洞复现
0x00 通达简介 通达OA国内常用的办公系统,使用群体,大小公司都可以,其此次安全更新修复的高危漏洞为任意用户登录漏洞.攻击者在远程且未经授权的情况下,通过利用此漏洞,可以直接以任意用户身份登录到系 ...
- Spring Security认证流程分析--练气后期
写在前面 在前一篇文章中,我们介绍了如何配置spring security的自定义认证页面,以及前后端分离场景下如何获取spring security的CSRF Token.在这一篇文章中我们将来分析 ...
- springboot整合websocket后打包报错:javax.websocket.server.ServerContainer not available
项目整合了websocket以后,打包多次都没有成功,原来是报错了,报错内容如下: Error starting ApplicationContext. To display the conditio ...
- A Case for Lease-Based, Utilitarian Resource Management on Mobile Devices
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! 以下是对本文关键部分的摘抄翻译,详情请参见原文. Abstract 移动应用程序已经成为我们日常生活中不可或缺的一部分,但许多应用程序的设 ...
- 手写一个简单版的SpringMVC
一 写在前面 这是自己实现一个简单的具有SpringMVC功能的小Demo,主要实现效果是; 自己定义的实现效果是通过浏览器地址传一个name参数,打印“my name is”+name参数.不使用S ...