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]%);
}
}
}
最新文章
- junit单元测试(keeps the bar green to keeps the code clean)
- week 4 日志
- 将HTML段赋值给PHP变量的便捷方法,不使用转义字符
- char与varchar区别-转
- TaskUtil多线程与定时任务
- Linux下配置Node环境变量及问题详解
- virtualbox怎么装系统OVA虚拟包大全一键安装
- Android使用Webview加载网页
- hdu 1234
- DOM ISO - get current element's XPATH
- input 事件与汉字输入法:使用compositionend事件解决
- GO语言一行代码实现反向代理
- 【多线程补充】SimpleDateFormat非线程安全与线程中、线程组中异常的处理
- 在addroutes后,$router.options.routes没有更新的问题(手摸手,带你用vue撸后台 读后感)
- css中元素border属性的构成以及配合属性值transparent可得到一些特殊形状1.0
- 7.24 IO多路复用和协程代码笔记
- [LeetCode] 787. Cheapest Flights Within K Stops_Medium tag: Dynamic Programming, BFS, Heap
- DelaunayTriangulation_VoronoiDiagram_using_OpenCV的实现
- Python 安装 pytesser 处理验证码出现的问题
- SQL Server Job