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 ...
随机推荐
- ECharts 常见的问题总结
以前也用过ECharts(不得不说,这真的是百度的良心产品),但是都是一些简单的示例.这次因为工作的需要,做了很多表格,对ECharts有了更加深刻的理解,现在来总结一下. 第一个肯定是新手经常遇到的 ...
- 安装黑苹果MoJave记录
说实话安装黑苹果并不是一件很简单的事情,它既费时,而且还需要一定的计算机知识,最重要的是对于你来说可能黑苹果并没有Linux或者Windows好用. 好了废话不多说,开始吧. 1.硬件准备 并不是什么 ...
- 配置 Eureka Server 集群
简介 为了使 Eureka Server 实现高可用,我们需要为它配置集群.这样当有一台 Eureka Server 有故障时,集群中的其他 Server 可以进行代替.Eureka 集群之中的 No ...
- Centos7查看端口占用
(1)netstat -lnp|grep 50090 如果提示没有netstat命令,可需要安装:yum -y install net-tools (2) lsof -i:50090 参考链接:lin ...
- 用终端命令行(BASH)将本地项目上传到Github并提交代码
第一步: 在Github上创建自己的repository 第二步:建立本地仓库cd到你的本地项目根目录下,执行git命令 1:$ cd 到你的项目目录下 2:$ git init 第三步:将本地项目工 ...
- A review of learning in biologically plausible spiking neural networks
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Contents: ABSTRACT 1. Introduction 2. Biological background 2.1. Spik ...
- 【转】Linux环境搭建FTP服务器与Python实现FTP客户端的交互介绍
Linux环境搭建FTP服务器与Python实现FTP客户端的交互介绍 FTP 是File Transfer Protocol(文件传输协议)的英文简称,它基于传输层协议TCP建立,用于Interne ...
- Ubuntu 磁盘满了处理方法。
Ubuntu 磁盘满了处理方法: 1. 如果是虚拟机安装ubuntu,直接给虚拟机安装ubuntu 系统所在的盘符动态分配一点磁盘容量,就可以了. 2. 如果不是虚拟机安装ubuntu,那么有两个办法 ...
- jenkins,开源CI工具
目前最热门CI工具的jenkins,学习笔记: 一.jenkins如何实现执行命令 1.执行jenkins同主机上的命令
- 服务器基本配置(ubuntu)
服务器基本配置(ubuntu) 学习目标: 修改初始服务器名字(ubuntu 16.04 ) 修改初始服务器名字(ubuntu 18.04 ) ubuntu换源 更改默认python版本 安装软件出现 ...