POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)

Priest John‘s Busiest Day

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10010   Accepted: 3425   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 SiTi and DiSi 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 20

Sample Output

YES
08:00 08:30
08:40 09:00

题目链接:POJ 3683

挑战程序书上的例题,重新学习了一下2-SAT,一般的2-SAT是给出一些矛盾的条件,然后根据这些矛盾的条件用强连通分量算法来做。

首先要知道把什么东西拆点,一般是去拆具有两个对立面的事物,比如这题就是神父出现在开头和神父出现在结尾,两者就是不能同时发生的对立面,然后记这两个对立面为$x_i$与$\lnot x_i$,显然一般情况下如果a与b矛盾那么就是说“a与b不能同时发生”,转换成标记符号就是$\lnot (a \land b)$,然后把这个式子拆开得到$\lnot a\lor\lnot b$,那么得到这样一个析取范式,我们可以将他转换成蕴含式子,即$(\lnot a \to b)\land(\lnot b \to a)$,这显然可以转换成两条有向边$<\lnot a, b>$与$<\lnot b, a>$,然后对图进行DFS得到强连通分量,然后看$\lnot x_i$与$x_i$是否在同一个强连通分量里,如果在同一个scc中显然是矛盾的,这题的话就第i个时间段和第j个时间段的开头和结束位置共4种关心进行判断,冲突就按上面的方法连边再scc处理即可,如果存在方案,只要判断$x_i$所在的连通分量编号与$\lnot x_i$所在的连通分量编号即可

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1010;
const int MAXV = N * 2;
const int MAXE = N * N * 4 * 2;
struct edge
{
	int to, nxt;
	edge() {}
	edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
};
edge E[MAXE];
int head[MAXV], tot;
int dfn[MAXV], low[MAXV], belong[MAXV], st[MAXV], top, ts, ins[MAXV], sc;
int s[N], t[N], d[N];

void init()
{
	CLR(head, -1);
	tot = 0;
	CLR(dfn, 0);
	CLR(low, 0);
	top = sc = 0;
	CLR(ins, 0);
	sc = 0;
}
inline void add(int s, int t)
{
	E[tot] = edge(t, head[s]);
	head[s] = tot++;
}
void Tarjan(int u)
{
	low[u] = dfn[u] = ++ts;
	st[top++] = u;
	ins[u] = 1;
	for (int i = head[u]; ~i; i = E[i].nxt)
	{
		int v = E[i].to;
		if (!dfn[v])
		{
			Tarjan(v);
			low[u] = min(low[u], low[v]);
		}
		else if (ins[v])
			low[u] = min(low[u], dfn[v]);
	}
	if (low[u] == dfn[u])
	{
		++sc;
		int v;
		do
		{
			v = st[--top];
			ins[v] = 0;
			belong[v] = sc;
		} while (u != v);
	}
}
int main(void)
{
	int n, i, j;
	while (~scanf("%d", &n))
	{
		init();
		for (i = 1; i <= n; ++i)
		{
			int h1, m1, h2, m2;
			scanf(" %d:%d %d:%d %d", &h1, &m1, &h2, &m2, &d[i]);
			s[i] = h1 * 60 + m1;
			t[i] = h2 * 60 + m2;
		}
		for (i = 1; i <= n; ++i) //n*n*4*2
		{
			for (j = 1; j <= n; ++j)
			{
				if (i == j)
					continue;
				if (min(s[i] + d[i], s[j] + d[j]) > max(s[i], s[j])) //s-s
				{
					add(i, j + n);
					add(j, i + n);
				}
				if (min(s[i] + d[i], t[j]) > max(s[i], t[j] - d[j])) //s-t
				{
					add(i, j);
					add(j + n, i + n);
				}
				if (min(t[i], s[j] + d[j]) > max(t[i] - d[i], s[j])) //t-s
				{
					add(i + n, j + n);
					add(j, i);
				}
				if (min(t[i], t[j]) > max(t[i] - d[i], t[j] - d[j])) //t-t
				{
					add(i + n, j);
					add(j + n, i);
				}
			}
		}
		for (i = 1; i <= (n << 1); ++i)
			if (!dfn[i])
				Tarjan(i);
		int flag = 1;
		for (i = 1; i <= n; ++i)
			if (belong[i] == belong[i + n])
				flag = 0;
		if (!flag)
			puts("NO");
		else
		{
			puts("YES");
			for (i = 1; i <= n; ++i)
			{
				if (belong[i] < belong[i + n])
					printf("%02d:%02d %02d:%02d\n", s[i] / 60, s[i] % 60, (s[i] + d[i]) / 60, (s[i] + d[i]) % 60);
				else
					printf("%02d:%02d %02d:%02d\n", (t[i] - d[i]) / 60, (t[i] - d[i]) % 60, (t[i]) / 60, (t[i]) % 60);
			}
		}
	}
	return 0;
}

POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)

时间: 2024-10-18 08:07:24

POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)的相关文章

POJ 3683 Priest John&#39;s Busiest Day (2-SAT+输出可行解)

题目地址:POJ 3683 第一次做需要输出可行解的题目...大体思路是先用强连通来判断是否有可行解,然后用逆序建图,用拓扑排序来进行染色,然后输出可行解.具体思路见传送门 因为判断的时候少写了一个等号..检查了好长时间..sad... 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #incl

POJ 3683(Priest John&#39;s Busiest Day-强连通分量解决2-SAT)[Template:2-SAT]

Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8144   Accepted: 2769   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 le

POJ 3683.Priest John&#39;s Busiest Day 2-SAT

Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10144   Accepted: 3472   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 l

POJ 3683 Priest John&#39;s Busiest Day(2-SAT)

[题目链接] http://poj.org/problem?id=3683 [题目大意] 每个婚礼有两个时段可以进行特别仪式,特别仪式必须要有神父在场, 神父只有一个,问是否能满足所有婚礼的需求, [题解] 因为两个时段必须要满足一个时段,所以如果一个时段被占用那么另一个时段必须被空出来, 我们根据各个婚礼两个时段之间的冲突关系建边,之后跑2-SAT,判断是否冲突, 若无冲突则输出方案. [代码] #include <cstdio> #include <algorithm> #in

POJ 3683 Priest John&#39;s Busiest Day 【2-Sat】

这是一道裸的2-Sat,只要考虑矛盾条件的判断就好了. 矛盾判断: 对于婚礼现场 x 和 y,x 的第一段可以和 y 的第一段或者第二段矛盾,同理,x 的第二段可以和 y 的第一段或者第二段矛盾,条件是 x 的 1或2 段与 y 的 1或2 段有重合,那么选了 x 某一段就不能选择其矛盾的那一段,那就只能选择 y 中的另一段,建立一条 x (u)-> y ( v的对立 ),同理,x (u的对立)<- y ( v ) . 真的,2-sat千万不要用邻接链表!!!卡死卡死卡死!!!稠密图!!!不要

POJ 3684 Priest John&#39;s Busiest Day 2-SAT+输出路径

强连通算法判断是否满足2-sat,然后反向建图,拓扑排序+染色. 一种选择是从 起点开始,另一种是终点-持续时间那个点 开始. 若2个婚礼的某2种时间线段相交,则有矛盾,建边. 容易出错的地方就在于判断线段相交. 若s1<e2&&s2<e1则相交 输出路径的做法可以参考论文2-SAT解法浅析 #include <iostream> #include<cstring> #include<cstdio> #include<string>

HDU2491 Priest John&#39;s Busiest Day

题目链接 题意: 有n个人要进行乒乓球比赛,每个人都一个能力值,每个人出现的次序就是他们住的位置 现在要求进行一场比赛,三个人,裁判的能力值在两个选手之间,住的位置也在两个人的之间 问这种比赛一共可以进行多少次 思路: 用树状数组做,否则TLE,先从左到右扫一遍,计算每点左边大的个数和小的个数, 再从右到左扫一遍,计算每点右边大和小的个数,然后交叉相乘取和就可以了 代码如下: #include<cstdio> #include<cstring> #include<string

UVA - 1420 Priest John&#39;s Busiest Day

题目大意:有一个司仪,要主持 n 场婚礼,给出婚礼的起始时间和终止时间,每个婚礼需要超过一半的时间做为仪式,并且仪式不能终止.问说司仪能否主持 n 场婚礼. 解题思路:贪心,为了尽量主持多的婚礼,每场的仪式时间就一定要尽量短 d = (t - s) / 2 + 1,(因为必须大于一半,所以加 1).然后按照每场婚礼可以最晚结束的时间排序 t - d,(因为要满足所有的婚礼,所以尽量解决早点的仪式,腾出时间来给后面的婚礼),维护一个占用时间值即可. #include <cstdio> #incl

POJ-3683 Priest John&#39;s Busiest Day

图论中的2-SAT.模板题. #include <cstdio> #include <cstdlib> #include <algorithm> #include <cctype> #include <cstring> #include <iostream> using namespace std; #define rep(i, l, r) for(int i=l; i<=r; i++) #define travel(x) fo