poj 2749(二分+2-SAT)

poj 2749
1、加边视情况加
2、2-SAT里面的c在solve里面千万不要再int c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#define ll long long
#define db double
#define ms(i,j) memset(i,j)
#define INF 100000000
#define llINF 1LL<<60
using namespace std;
const int MAXN = (50000 + 5) * 2, LEFT = 0, RIGHT = 1;
struct TwoSAT
{
vector<int> G[MAXN*2];
int mark[MAXN*2];
int S[MAXN*2];
int n, c;
void init(int n)
{
this->n = n;
for (int i=0;i<=n*2;i++)
{
mark[i] = false;
G[i].clear();
}
}
void addE(int x, int xv, int y, int yv)
{
x = x*2 + xv;
y = y*2 + yv;
G[x^1].push_back(y);
G[y^1].push_back(x);
}
int 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;
}
bool solve()
{
for (int i = 0;i < n;i += 2)
if (!mark[i]&&!mark[i+1])
{
c = 0;
if (!dfs(i))
{
for (int j = 0; j < c; j++)
mark[S[j]] = false;
c = 0;
if (!dfs(i+1)) return false;
}
}
return true;
}
}ts;
int n, A, B, sx1, sy1, sx2, sy2, xi[MAXN], yi[MAXN], hate[MAXN][2], fri[MAXN][2], d1[MAXN], d2[MAXN],di;
int dis(int x1, int y1, int x2, int y2)
{
return abs(x1-x2)+abs(y1-y2);
}
int judge(int mid)
{
ts.init(n);
for (int i=0;i<n;i++)
for (int j=i+1;j<n;j++)
{
int x = i, y = j;
if (d1[i]+d1[j]>mid) ts.addE(x, 1, y, 1);
if (d2[i]+d2[j]>mid) ts.addE(x, 0, y, 0);
if (d1[i]+di+d2[j]>mid) ts.addE(x, 1, y, 0);
if (d2[i]+di+d1[j]>mid) ts.addE(x, 0, y, 1);
}
for (int i=0;i<A;i++)
{
int x = hate[i][LEFT] - 1, y = hate[i][RIGHT] - 1;
ts.addE(x, 0, y, 0);
ts.addE(x, 1, y, 1);
}
for (int i=0;i<B;i++)
{
int x = fri[i][LEFT] - 1, y = fri[i][RIGHT] - 1;
ts.addE(x, 0, y, 1);
ts.addE(y, 0, x, 1);
}
return ts.solve();
}
int init()
{
scanf("%d%d%d%d", &sx1, &sy1, &sx2, &sy2);
for (int i=0;i<n;i++)
{
scanf("%d%d", &xi[i], &yi[i]);
}
for (int i=0;i<A;i++)
{
scanf("%d%d", &hate[i][LEFT], &hate[i][RIGHT]);
}
for (int i=0;i<B;i++)
{
scanf("%d%d", &fri[i][LEFT], &fri[i][RIGHT]);
}
}
int solve()
{
di = dis(sx1, sy1, sx2, sy2);
for (int i=0;i<n;i++)
{
d1[i] = dis(xi[i], yi[i], sx1, sy1);
d2[i] = dis(xi[i], yi[i], sx2, sy2);
}
int l = 0, r = 8000000, ans = 10000000;
while (l < r)
{
int mid = (l + r) / 2;
if (judge(mid))
{
ans = mid;
r = mid;
} else l = mid+1;
}
if (ans!=10000000)
{
printf("%d\n", ans);
} else printf("%d\n", -1);
}
int main()
{
while (scanf("%d%d%d", &n, &A, &B)==3)
{
init();
solve();
}
return 0;
}

------ 本文结束 ------