「SDOI2013」逃考 - 半平面交+最短路径 | Bill Yang's Blog

「SDOI2013」逃考 - 半平面交+最短路径

题目大意


题目分析

发现$a,b$的控制范围的分界线即为$a,b$连线的中垂线,因此$x$的控制范围即为与其他所有点的连线中垂线的半平面交。
因此做一个半平面交,然后跑最短路即可。

半平面交要记得判重。


代码

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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
inline const int Get_Int() {
int num=0,bj=1;
char x=getchar();
while(x<'0'||x>'9') {
if(x=='-')bj=-1;
x=getchar();
}
while(x>='0'&&x<='9') {
num=num*10+x-'0';
x=getchar();
}
return num*bj;
}
const int maxn=605,maxm=360005;
const double eps=1e-8;
double dcmp(double x) {
if(fabs(x)<=eps)return 0;
if(x>eps)return 1;
return -1;
}
struct Point {
double x,y;
Point(double _x=0,double _y=0):x(_x),y(_y) {}
Point operator + (const Point& a) const {
return Point(x+a.x,y+a.y);
}
Point operator - (const Point& a) const {
return Point(a.x-x,a.y-y);
}
Point operator * (double a) const {
return Point(x*a,y*a);
}
Point operator / (double a) const {
return Point(x/a,y/a);
}
} p[maxn],lim,st;
typedef Point Vector;
double Cross(const Vector& a,const Vector& b) {
return a.x*b.y-b.x*a.y;
}
vector<int> edges[maxn];
void AddEdge(int x,int y) {
edges[x].push_back(y);
}
struct Line {
Point p;
Vector v;
double ang;
int id;
Line() {}
Line(Point p,Vector v,int x):p(p),v(v),id(x) {
ang=atan2(v.y,v.x);
}
bool operator < (const Line& L) const {
return ang<L.ang;
}
};
bool OnLeft(Line L,Point p) {
return dcmp(Cross(L.v,L.p-p))>0;
}
Point GetIntersection(Line a,Line b) {
Vector u=a.p-b.p;
double t=Cross(u,b.v)/Cross(a.v,b.v);
return a.p+a.v*t;
}
Point tmpp[maxm];
Line q[maxm+5];
int n,S;
int Unique(int n,Line* L) {
int cnt=1;
for(int i=2; i<=n; i++) {
if(dcmp(L[cnt].ang-L[i].ang)!=0)L[++cnt]=L[i];
else if(OnLeft(L[cnt],L[i].p))L[cnt]=L[i];
}
return cnt;
}
void HalfplaneIntersection(int n,Line* L,int s) {
sort(L+1,L+n+1);
n=Unique(n,L);
int first=1,last=1;
q[last]=L[1];
for(int i=2; i<=n; i++) {
while(first<last&&!OnLeft(L[i],tmpp[last-1]))last--;
while(first<last&&!OnLeft(L[i],tmpp[first]))first++;
q[++last]=L[i];
if(first<last)tmpp[last-1]=GetIntersection(q[last-1],q[last]);
}
while(first<last&&!OnLeft(q[first],tmpp[last-1]))last--;
if(last-first<=1)return;
for(int i=first; i<=last; i++)AddEdge(s,q[i].id);
if(!S) {
bool bj=1;
for(int i=first; i<=last; i++)
if(!OnLeft(q[i],st)) {
bj=0;
break;
}
if(bj)S=s;
}
}
int cnt=0;
Line L[maxm];
void build(int s) {
cnt=0;
for(int i=1; i<=n; i++)
if(i!=s) {
Point mid=(p[s]+p[i])/2;
Vector pos=p[s]-p[i];
pos=Vector(-pos.y,pos.x);
L[++cnt]=Line(mid,pos,i);
}
L[++cnt]=Line(Point(0,0),Vector(0,-1),0);
L[++cnt]=Line(Point(0,lim.y),Vector(-1,0),0);
L[++cnt]=Line(lim,Vector(0,1),0);
L[++cnt]=Line(Point(lim.x,0),Vector(1,0),0);
HalfplaneIntersection(cnt,L,s);
}
#define mp make_pair
#define pii pair<int,int>
int Dist[maxn];
bool vst[maxn];
void Dijkstra(int s) {
for(int i=0; i<=n; i++)Dist[i]=INT_MAX,vst[i]=0;
priority_queue<pii,vector<pii>,greater<pii> > Q;
Dist[s]=0;
Q.push(mp(0,s));
while(!Q.empty()) {
int Now=Q.top().second;
Q.pop();
if(vst[Now])continue;
vst[Now]=1;
for(int Next:edges[Now])
if(Dist[Next]>Dist[Now]+1) {
Dist[Next]=Dist[Now]+1;
Q.push(mp(Dist[Next],Next));
}
}
}
int main() {
int t=Get_Int();
while(t--) {
n=Get_Int();
lim.x=Get_Int(),lim.y=Get_Int();
st.x=Get_Int(),st.y=Get_Int();
for(int i=1; i<=n; i++) {
p[i].x=Get_Int(),p[i].y=Get_Int();
edges[i].clear();
}
S=0;
for(int i=1; i<=n; i++)build(i);
Dijkstra(S);
printf("%d\n",Dist[0]);
}
return 0;
}
0%