-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
131 lines (111 loc) · 2.56 KB
/
solution.cpp
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
#include <bits/stdc++.h>
using namespace std;
class Dijkstra {
public:
using cost_t = int;
static constexpr cost_t INF = numeric_limits<cost_t>::max();
struct State {
int u;
cost_t cost;
friend bool operator<(const State& lhs, const State& rhs) { return lhs.cost > rhs.cost; }
};
struct Edge {
int u;
int v;
cost_t cost;
};
public:
const vector<vector<Edge>>& graph_;
vector<cost_t> costs_;
vector<int> trace_;
int src_;
int dst_ = -1;
bool doTrace_ = false;
public:
Dijkstra(const vector<vector<Edge>>& adjListGraph, int src=0)
: graph_(adjListGraph), costs_(adjListGraph.size()), trace_(0), src_(src) {}
void setDoTrace() { doTrace_ = true; }
void setSrc(int src) { src_ = src; }
void setDst(int dst) { dst_ = dst; }
const vector<cost_t>& cost() const { return costs_; }
const vector<int>& trace() const { return trace_; }
void solve() {
if (src_ == -1) {
throw runtime_error("src is not defined!");
}
fill(costs_.begin(), costs_.end(), INF);
if (doTrace_) {
trace_.resize(costs_.size());
fill(trace_.begin(), trace_.end(), -1);
}
priority_queue<State> pq;
costs_[src_] = 0;
pq.push({src_, 0});
while (!pq.empty()) {
auto [u, c] = pq.top();
pq.pop();
if (costs_[u] < c) {
continue;
}
if (u == dst_) {
break;
}
for (const auto& [_, v, w] : graph_[u]) {
cost_t nextCost = c + w;
if (nextCost < costs_[v]) {
costs_[v] = nextCost;
pq.push({v, nextCost});
if (doTrace_) {
trace_[v] = u;
}
}
}
}
}
};
struct City {
int s, x, y;
};
void solve() {
int N, T;
scanf("%d %d", &N, &T);
auto getDistance = [T](const City& c1, const City& c2) -> int {
int res = abs(c1.x - c2.x) + abs(c1.y - c2.y);
if (c1.s == 1 && c2.s == 1) {
res = min(res, T);
}
return res;
};
vector<City> cities(N);
vector<vector<Dijkstra::Edge>> graph(N);
for (int i = 0; i < N; ++i) {
auto& [s, x, y] = cities[i];
scanf("%d %d %d", &s, &x, &y);
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (i != j) {
graph[i].emplace_back(i, j, getDistance(cities[i], cities[j]));
}
}
}
Dijkstra dijk(graph);
int M;
scanf("%d", &M);
while (M--) {
int i, j;
scanf("%d %d", &i, &j);
i--; j--;
dijk.setSrc(i);
dijk.setDst(j);
dijk.solve();
printf("%d\n", dijk.cost()[j]);
}
}
int main() {
int T = 1;
while (T--) {
solve();
}
return 0;
}