POJ-3683(2-SAT)

28 篇文章 0 订阅
5 篇文章 0 订阅

题目:http://poj.org/problem?id=3683

第三道2-SAT,普通的2-SAT问题,打印schedule只需找出每段时间的选择之后排序一下即可,在比较两段时间是否重叠的时候我是这么想的:区间a要是不与区间b重叠,则a.right <= b.left或者a.left >= b.right,否则两者有重叠。


#include <cstdio>
#include <utility>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define MAX_N	1005

typedef pair<int,int> PII;
int N, S[MAX_N], T[MAX_N], D[MAX_N];
PII interval[MAX_N];
vector<int> out[MAX_N * 2];
bool mark[MAX_N * 2];		//mark[2i] = true, i choose early; mark[2i+1] = true, i choose late
int stack[MAX_N * 2], cnt;

inline int timeToInt(const char* hh_mm){
	return ((hh_mm[0]-'0')*10 + (hh_mm[1]-'0')) * 60 + 
		   ((hh_mm[3]-'0')*10 + (hh_mm[4]-'0')) ;
}
inline char* intToTime(char* hh_mm, int t){
	sprintf(hh_mm, "%02d:%02d", t/60, t%60);
	return hh_mm;
}
inline bool intersect(const PII& a, const PII& b){
	//if not intersect then a must on b's left or on b' right
	return !(a.second <= b.first || b.second <= a.first);
}

void init()
{
	memset(mark, 0, N*2);
	for(int i = N*2-1; i > -1; --i) out[i].clear();
}
void build()
{
	int i, j, x, y;
	char s[8], t[8];
	PII iearly, ilate, jearly, jlate;
	for(i = 0; i < N; ++i){
		x = i << 1;
		scanf("%s%s%d", s, t, D+i);
		iearly.first = S[i] = timeToInt(s);
		ilate.second = T[i] = timeToInt(t);
		iearly.second = S[i] + D[i];
		ilate.first = T[i] - D[i];
		for(j = i-1; j > -1; --j){
			y = j << 1;
			jearly.first = S[j];
			jearly.second = S[j] + D[j];
			jlate.first = T[j] - D[j];
			jlate.second = T[j];
			if(intersect(iearly, jearly)){//if i early j must late, if j early i must late
				out[x].push_back(y+1);
				out[y].push_back(x+1);
			}
			if(intersect(iearly, jlate)){//if i early j must early, if j late i must late
				out[x].push_back(y);
				out[y+1].push_back(x+1);
			}
			if(intersect(ilate, jearly)){//if i late j must late, if j early i must early
				out[x+1].push_back(y+1);
				out[y].push_back(x);
			}
			if(intersect(ilate, jlate)){//if i late j must early, if j late i must early
				out[x+1].push_back(y);
				out[y+1].push_back(x);
			}
		}
	}
}
bool dfs(int x)
{
	if(mark[x ^ 1]) return false;
	if(mark[x]) return true;
	mark[x] = true;
	stack[cnt++] = x;
	const vector<int>& v = out[x];
	for(int i = v.size()-1; i > -1; --i){
		if(!dfs(v[i])) return false;
	}
	return true;
}
bool test()
{
	for(int i = 0; i < N; ++i){
		cnt = 0;
		if(!dfs(i << 1)){
			while(cnt) mark[stack[--cnt]] = false;
			if(!dfs(i << 1 | 1)) return false;
		}
	}
	return true;
}
void schedule()
{
	int i, x;
	char s[8], e[8];

	for(i = 0; i < N; ++i){
		x = i << 1;
		if(mark[x]) interval[i] = PII(S[i], S[i] + D[i]);
		else interval[i] = PII(T[i] - D[i], T[i]);
	}
	sort(interval, interval + N);
	for(i = 0; i < N; ++i){
		printf("%s %s\n", 
				intToTime(s, interval[i].first), 
				intToTime(e, interval[i].second));
	}
}

int main()
{
	while(~scanf("%d", &N)){
		init();
		build();
		if(test()){
			puts("YES");
			schedule();
		}
		else puts("NO");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值