POJ 1556 The Doors

Description:

You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length.
img

Input:

The input data for the illustrated chamber would appear as follows.

2
4 2 7 8 9
7 3 4.5 6 7

The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1.

Output:

The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.

Sample Input:

1
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1

Sample Output:

10.00
10.06

题目链接

题目要求起点(0,5)到终点(10,5)的最短距离,对每两个可行路径建边,权值为其距离,建图之后求起点到终点的最短路即可。

AC代码:

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
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

const double INF = 1e20;
const int maxn = 1e4 + 5;

struct Point {
double X, Y;

Point operator - (const Point &B) const {
return Point {X - B.X, Y - B.Y};
}

double operator * (const Point &B) const {
return X * B.X + Y * B.Y;
}

double operator ^ (const Point &B) const {
return X * B.Y - Y * B.X;
}
};

struct Segment {
Point S, T;

double operator ^ (const Segment &B) const {
return (T - S) ^ (B.T - B.S);
}
};

struct Edge {
int V;
double Weight;
int Next;
};

Edge edges[maxn << 2];
int Head[maxn];
int Tot;
int N;
double X, Y1, Y2, Y3, Y4;
Point points[maxn << 2];
double Dis[maxn];

void Init() {
Tot = 0;
memset(Head, -1, sizeof(Head));
}

void AddEdge(int U, int V, double Weight) {
edges[Tot] = Edge {V, Weight, Head[U]};
Head[U] = Tot++;
edges[Tot] = Edge {U, Weight, Head[V]};
Head[V] = Tot++;
}

double Distance(Point A, Point B) {
return sqrt((B - A) * (B - A));
}

bool IsIntersect(Segment A, Segment B) {
return
(A ^ Segment {A.S, B.S}) * (A ^ Segment {A.S, B.T}) <= 0 &&
(B ^ Segment {B.S, A.S}) * (B ^ Segment {B.S, A.T}) <= 0;
}

struct Cmp {
bool operator () (const int &A, const int &B) {
return Dis[A] > Dis[B];
}
};

void Dijkstra(int Start) {
priority_queue<int, vector<int>, Cmp> Que;
for (int i = 0; i <= 4 * N + 1; ++i) {
Dis[i] = INF;
}
Dis[Start] = 0;
Que.push(Start);
while (!Que.empty()) {
int Cur = Que.top(); Que.pop();
for (int i = Head[Cur]; ~i; i = edges[i].Next) {
if (Dis[edges[i].V] > Dis[Cur] + edges[i].Weight) {
Dis[edges[i].V] = Dis[Cur] + edges[i].Weight;
Que.push(edges[i].V);
}
}
}
}

int main(int argc, char *argv[]) {
while (~scanf("%d", &N) && ~N) {
Init();

points[0] = Point {0, 5};
for (int i = 1; i <= N; ++i) {
scanf("%lf%lf%lf%lf%lf", &X, &Y1, &Y2, &Y3, &Y4);
points[i * 4 - 3] = Point {X, Y1};
points[i * 4 - 2] = Point {X, Y2};
points[i * 4 - 1] = Point {X, Y3};
points[i * 4] = Point {X, Y4};
}
points[4 * N + 1] = Point {10, 5};

for (int i = 1; i <= 4 * N; ++i) {
bool Flag = true;
Segment Judge = Segment {points[0], points[i]};
for (int j = 4; j < i; j += 4) {
double CurX = points[j].X;
if (IsIntersect(Judge, Segment {Point {CurX, 0}, points[j - 3]}) ||
IsIntersect(Judge, Segment {points[j - 2], points[j - 1]}) ||
IsIntersect(Judge, Segment {points[j], Point {CurX, 10}})) {
Flag = false;
break;
}
}
if (Flag) {
AddEdge(0, i, Distance(points[0], points[i]));
}

Flag = true;
Judge = Segment {points[i], points[4 * N + 1]};
for (int j = 4 * N; j - 3 > i; j -= 4) {
double CurX = points[j].X;
if (IsIntersect(Judge, Segment {Point {CurX, 0}, points[j - 3]}) ||
IsIntersect(Judge, Segment {points[j - 2], points[j - 1]}) ||
IsIntersect(Judge, Segment {points[j], Point {CurX, 10}})) {
Flag = false;
break;
}
}
if (Flag) {
AddEdge(i, 4 * N + 1, Distance(points[i], points[4 * N + 1]));
}
}

for (int i = 1; i <= 4 * N; ++i) {
for (int l = 4 * N; l - 3 > i; l -= 4) {
for (int j = l; j >= l - 3; --j) {
bool Flag = true;
Segment Judge = Segment {points[i], points[j]};
for (int k = l - 4; k - 3 > i; k -= 4) {
double CurX = points[k].X;
if (IsIntersect(Judge, Segment {Point {CurX, 0}, points[k - 3]}) ||
IsIntersect(Judge, Segment {points[k - 2], points[k - 1]}) ||
IsIntersect(Judge, Segment {points[k], Point {CurX, 10}})) {
Flag = false;
break;
}
}
if (Flag) {
AddEdge(i, j, Distance(points[i], points[j]));
}
}
}
}

bool Flag = true;
Segment Judge = Segment {points[0], points[4 * N + 1]};
for (int j = 4; j <= 4 * N; j += 4) {
double CurX = points[j].X;
if (IsIntersect(Judge, Segment {Point {CurX, 0}, points[j - 3]}) ||
IsIntersect(Judge, Segment {points[j - 2], points[j - 1]}) ||
IsIntersect(Judge, Segment {points[j], Point {CurX, 10}})) {
Flag = false;
break;
}
}
if (Flag) {
AddEdge(0, 4 * N + 1, Distance(points[0], points[4 * N + 1]));
}

Dijkstra(0);
printf("%.2f\n", Dis[4 * N + 1]);
}
return 0;
}
文章目录
  1. 1. Description:
  2. 2. Input:
  3. 3. Output:
  4. 4. Sample Input:
  5. 5. Sample Output:
    1. 5.1. 题目链接
  6. 6. AC代码:
|