HDU 4179 Difficult Routes

链接

传送门

代码

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

const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int maxn = 10010;
const int maxm = 60010;
int x[maxn], y[maxn], z[maxn];
int u[maxm], v[maxm], dif[maxm];
double len[maxm >> 1];

struct Edge {
int from, to;
double dist;
Edge(int u, int v, double d): from(u), to(v), dist(d) {}
};

struct Dijkstra {
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn];
double d[maxn];
int p[maxn];

void init(int n) {
this->n = n;
for (int i = 0; i <= n; ++i) {
G[i].clear();
}
edges.clear();
}

void AddEdge(int from, int to, double dist) {
edges.push_back(Edge(from, to, dist));
m = int(edges.size());
G[from].push_back(m - 1);
}

struct HeapNode {
double d;
int u;
HeapNode(double d, int u): d(d), u(u) {}
bool operator < (const HeapNode& rhs) const {
return d > rhs.d;
}
};

void dijkstra(int s) {
priority_queue<HeapNode> Q;
for (int i = 0; i < n; ++i) {
d[i] = inf;
}
d[s] = 0;
memset(done, 0, sizeof done);
Q.push(HeapNode(0, s));
while (!Q.empty()) {
HeapNode x = Q.top();
Q.pop();
int u = x.u;
if (done[u]) {
continue;
}
done[u] = true;
for (int i = 0; i < G[u].size(); ++i) {
Edge& e = edges[G[u][i]];
if (d[e.to] > d[u] + e.dist) {
d[e.to] = d[u] + e.dist;
p[e.to] = G[u][i];
Q.push(HeapNode(d[e.to], e.to));
}
}
}
}
} a, b;

int Dif(int i, int j) {
if (z[j] <= z[i]) {
return 0;
}
return floor(100.0 * (z[j] - z[i]) / sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])));
}

double Dis(int i, int j) {
return sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j]));
}

int main() {
int n, m;
while (~scanf("%d%d", &n, &m) && (n || m)) {
a.init(n + 1), b.init(n + 1);
for (int i = 0; i < n; ++i) {
scanf("%d%d%d", &x[i], &y[i], &z[i]);
}
m <<= 1;
for (int i = 0; i < m; i += 2) {
scanf("%d%d", &u[i], &v[i]);
v[i + 1] = --u[i], u[i + 1] = --v[i];
len[i >> 1] = Dis(u[i], v[i]);
}
int s, t, d;
scanf("%d%d%d", &s, &t, &d);
--s, --t;
for (int i = 0; i < m; ++i) {
dif[i] = Dif(u[i], v[i]);
double l = len[i >> 1];
if (dif[i] <= d) {
a.AddEdge(u[i], v[i], l);
b.AddEdge(v[i], u[i], l);
}
}
a.dijkstra(s), b.dijkstra(t);
double ans = inf;
for (int i = 0; i < m; ++i) {
if (dif[i] == d) {
double l = a.d[u[i]] + b.d[v[i]] + len[i >> 1];
if (l < ans) {
ans = l;
}
}
}
if (fabs(ans - inf) < eps) {
puts("None");
} else {
printf("%.1f\n", ans);
}
}
return 0;
}