题目大意

给定一个正方形,和若干条起点和终点均在正方形上的线段。此时图中线段由正方形的某边,初始的线段,相交形成的新线段组成。问:在正方形外,只能穿过某一线段中点的情况下,最少需要穿过几个线段才能到达指定坐标点?

想法

线段相交判断是经典问题,而最少穿过的线段数目,可以考虑建图转化为最短路解决。

题目中限制了,每次穿过的点均要为图中某两个线段交点的中点,因此我们可以考虑:

  1. 求出图中所有的交点,得到图中所有的线段
  2. 处理出所有可以通过的中点坐标
  3. 在正方形外建一个超级源点,连接所有处在正方形边上的可行的中点坐标
  4. 对于剩下的点,两两之间进行枚举,如果两点组成的线段,不与其他的线段产生规范相交,则连一条权值为$1$的边,否则连一条权值为极大值的边。
  5. 以超级源点为起点,跑单源最短路径。由于至多可能会跑到$900$个点左右,选用虽然编写难度较低但是时间复杂度较大的$Floyd$算法有一定风险。

由此,我们就可以分拆任务,一个个完成编写。

代码(UVA754 AC, But POJ1066 TLE/MLE)

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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// 1066.cpp
// UVA PASS, POJ MLE/TLE

#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2", 3)
#pragma GCC target("avx", "sse2")

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <vector>
#define maxn 2005
using namespace std;
int T, n, lcnt, pcnt;
double dis[maxn][maxn], ftx, fty;
const double eps = 1e-8;
int sgn(double x) {
if (fabs(x) < eps)
return 0;

return x > 0 ? 1 : -1;
}
struct Point {
double x, y;
Point() {}
Point(double _x, double _y) {
x = _x;
y = _y;
}
Point operator+(const Point &A) {
return Point(x + A.x, y + A.y);
}
Point operator-(const Point &A) {
return Point(x - A.x, y - A.y);
}
double operator^(const Point &A) {
return x * A.y - y * A.x;
}
double operator*(const Point &A) {
return x * A.x + y * A.y;
}
bool operator==(const Point &A) {
return sgn(x - A.x) == 0 && sgn(y - A.y) == 0;
}
bool operator<(Point &A) {
return sgn(x - A.x) == 0 ? y < A.y : x < A.x;
}
double distance(Point v) {
return sqrt((v.x - x) * (v.x - x) + (v.y - y) * (v.y - y));
}
void print() {
printf("[Point] x: %lf, y: %lf\n", x, y);
}
} P[maxn];
struct Line {
Point s, e;
Line() {}
Line(Point _s, Point _e) {
s = _s;
e = _e;
}
int segcrossseg(Line v) {
int d1 = sgn((e - s) ^ (v.s - s));
int d2 = sgn((e - s) ^ (v.e - s));
int d3 = sgn((v.e - v.s) ^ (s - v.s));
int d4 = sgn((v.e - v.s) ^ (e - v.s));

if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2)
return 2;

return (d1 == 0 && sgn((v.s - s) * (v.s - e)) <= 0) ||
(d2 == 0 && sgn((v.e - s) * (v.e - e)) <= 0) ||
(d3 == 0 && sgn((s - v.s) * (s - v.e)) <= 0) ||
(d4 == 0 && sgn((e - v.s) * (e - v.e)) <= 0);
}
Point crosspoint(Line v) {
double a1 = (v.e - v.s) ^ (s - v.s);
double a2 = (v.e - v.s) ^ (e - v.s);
return Point((s.x * a2 - e.x * a1) / (a2 - a1), (s.y * a2 - e.y * a1) / (a2 - a1));
}
bool pointonseg(Point p) {
return sgn((p - s) ^ (e - s)) == 0 && sgn((p - s) * (p - e)) <= 0;
}
bool parallel(Line v) {
return sgn((e - s) ^ (v.e - v.s)) == 0;
}
int relation(Point p) {
int c = sgn((p - s) ^ (e - s));

if (c < 0)
return 1;
else if (c > 0)
return 2;
else
return 3;
}
int linecrossline(Line v) {
if ((*this).parallel(v)) {
return v.relation(s) == 3;
}

return 2;
}
void print() {
printf("[Line] x1: %lf, y1: %lf, x2: %lf, y2:%lf\n", s.x, s.y, e.x, e.y);
}
} L[maxn];
vector<Point> mid;
void build(int source) {
int siz = mid.size();

for (int i = 0; i < siz; i++) {
if (i == source) {
dis[i][i] = 0;
continue;
}

if (sgn(mid[i].x) == 0 || sgn(mid[i].y) == 0 || sgn(mid[i].x - 100) == 0 || sgn(mid[i].y - 100) == 0) {
dis[source][i] = dis[i][source] = 1;
} else {
dis[source][i] = dis[i][source] = 1e9;
}
}

for (int i = 0; i < siz; i++) {
for (int j = 0; j < siz; j++) {
if (i == source || j == source)
continue;

if (i == j)
dis[i][j] = 0;
else {
int flag = 1;

for (int k = 1; k <= lcnt; k++) {
if (Line(mid[i], mid[j]).segcrossseg(L[k]) == 2) {
flag = 0;
break;
}
}

dis[i][j] = flag ? sgn(mid[i].distance(mid[j])) : 1e9;
}
}
}
}
void floyd() {
int siz = mid.size();

for (int k = 0; k < siz; k++)
for (int i = 0; i < siz; i++)
for (int j = 0; j < siz; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
double dij[maxn];
int vis[maxn];
void dijkstra(int n, int s) {
for (int i = 0; i <= n; i++)
dij[i] = 1e9, vis[i] = 0;

dij[s] = 0;

for (int i = 0; i <= n; i++) {
int u = 0, mind = 0x3f3f3f3f;

for (int j = 0; j <= n; j++)
if (!vis[j] && dij[j] < mind)
u = j, mind = dij[j];

vis[u] = true;

for (int j = 1; j <= n; j++) {
if (dij[j] > dij[u] + dis[u][j]) {
dij[j] = dij[u] + dis[u][j];
}
}
}
}
bool check_seg(Point A, Point B) {
for (int i = 1; i <= lcnt; i++) {
if (L[i].pointonseg(A) && L[i].pointonseg(B)) {
return true;
}
}

return false;
}
void solve() {
mid.clear();
pcnt = lcnt = 0;
scanf("%d", &n);

P[++pcnt] = Point(0, 0);
P[++pcnt] = Point(0, 100);
P[++pcnt] = Point(100, 100);
P[++pcnt] = Point(100, 0);
L[++lcnt] = Line(P[1], P[2]);
L[++lcnt] = Line(P[2], P[3]);
L[++lcnt] = Line(P[3], P[4]);
L[++lcnt] = Line(P[4], P[1]);

for (int i = 1; i <= n; i++) {
double sx, st, tx, tt;
scanf("%lf %lf %lf %lf", &sx, &st, &tx, &tt);
P[++pcnt] = Point(sx, st);
P[++pcnt] = Point(tx, tt);
L[++lcnt] = Line(P[pcnt], P[pcnt - 1]);
}

scanf("%lf %lf", &ftx, &fty);

for (int i = 1; i <= lcnt; i++) {
for (int j = i + 1; j <= lcnt; j++) {
//maybe repeat
if (L[i].segcrossseg(L[j]) && L[i].linecrossline(L[j]) == 2) {
P[++pcnt] = L[i].crosspoint(L[j]);
}
}
}

sort(P + 1, P + pcnt + 1);
pcnt = (unique(P + 1, P + pcnt + 1) - P) - 1;

for (int i = 1; i <= pcnt; i++) {
for (int j = 1; j <= pcnt; j++) {
if (i == j)
continue;

int flag = check_seg(P[i], P[j]);

if (!flag)
continue;

for (int k = 1; k <= pcnt; k++) {
if (k == i || k == j)
continue;

if (Line(P[i], P[j]).pointonseg(P[k])) {
flag = 0;
break;
}
}

if (!flag)
continue;

mid.push_back(Point((P[i].x + P[j].x) / 2, (P[i].y + P[j].y) / 2));
}
}

mid.push_back(Point(ftx, fty));
mid.push_back(Point(-1, -1));
sort(mid.begin(), mid.end());
vector<Point>::iterator pos = unique(mid.begin(), mid.end());
mid.erase(pos, mid.end());
int siz = mid.size(), pos1 = -1, pos2 = -1;

for (int i = 0; i < siz; i++) {
if (mid[i] == Point(ftx, fty))
pos1 = i;

if (mid[i] == Point(-1, -1))
pos2 = i;
}

build(pos2); //floyd();
dijkstra(siz - 1, pos2);
printf("Number of doors = %d\n", (int)(dij[pos1]) - 1);
}
int main(void) {
int T;
scanf("%d", &T);
while(T--){
solve();
if(T) putchar('\n');
}
return 0;
}

由于代码经过多次修改,因此略显凌乱(

其他想法

后寻找题解,发现不需要这么复杂。

只需要枚举最外围的点作为起点,然后起点与终点间连一个线段$L$,判断给出的线段中,有多少与$L$相交即可。

证明可以参考:算法:poj1066 宝藏猎人问题。 - 荒土 - 博客园

我是🤡