您的位置:首页 > 其它

UVALive 3211 - Now or later(2-SAT + 二分)

2015-04-07 17:30 471 查看
题目:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=72618#problem/E

题意:

n架灰机, 有早着陆和晚着陆两个时间,要使得着陆安全即同一时间只能有一架灰机着陆,求出相邻两个着陆时间的最小值.

思路:

最小值尽量大, 使用二分.

使用强连通分量的2-SAT会超时.

要使用染色法

AC.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cmath>

using namespace std;
const int maxn = 2005;
int n, T[maxn][2];

struct Twosat{
int n;
vector<int> G[maxn*2];
bool mark[maxn*2];
int S[maxn*2], c;

bool dfs(int x) {
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x] = true;
S[c++] = x;
for(int i = 0; i < G[x].size(); ++i) {
if(!dfs(G[x][i])) return false;
}
return true;
}

void init(int n)
{
this->n = n;
for(int i = 0; i < 2*n; ++i) {
G[i].clear();
}
memset(mark, 0, sizeof(mark));
}

void add_clause(int x, int xval, int y, int yval)
{
x = x*2+xval;
y = y*2+yval;
G[x^1].push_back(y);
G[y^1].push_back(x);
}
bool solve() {
for(int i = 0; i < n*2; i+=2) {
if(!mark[i] && !mark[i+1]) {
c = 0;
if(!dfs(i)) {
while(c > 0) {
mark[S[--c]] = 0;
}
if(!dfs(i+1)) return false;
}
}
}
return 1;
}
}solver;

bool test(int mid)
{
solver.init(n);
for(int i = 0; i < n; ++i) {
for(int a = 0; a < 2; ++a) {
for(int j = i+1; j < n; ++j) {
for(int b = 0; b < 2; ++b) {
if(abs(T[i][a] - T[j][b]) < mid) {
solver.add_clause(i, a^1, j, b^1);
}
}
}
}
}
return solver.solve();
}
int main()
{
//freopen("in", "r", stdin);
while(~scanf("%d", &n)) {
int l = 0, r = 0;
for(int i = 0; i < n; ++i ) {
for(int a = 0; a < 2; ++a) {
scanf("%d", &T[i][a]);
r = max(r, T[i][a]);
}
}

while(l < r) {
int mid = l + (r-l+1)/2;
if(test(mid)) l = mid;
else r = mid - 1;
}

printf("%d\n", l);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UVALive