-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI. Interplanetário.cpp
156 lines (121 loc) · 2.79 KB
/
I. Interplanetário.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
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 400;
const ll oo = maxn * 1'0'000ll;
int n, m;
ll temp[maxn];
vector<pair<ll,int>> adj[maxn];
ll dist[maxn][maxn];
ll fdist[maxn][maxn];
int RANK[maxn];
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
dist[i][i] = 0;
for (int j = 0; j < n; j++) {
if (i != j) {
dist[i][j] = oo;
fdist[i][j] = oo;
}
}
}
for (int i = 0; i < n; i++) {
cin >> temp[i];
}
for (int i = 0, a, b,c; i < m; i++) {
cin >> a >> b >> c;
a--, b--;
dist[a][b] = dist[b][a] = c;
fdist[a][b] = fdist[b][a] = c;
adj[a].emplace_back(b, c);
adj[b].emplace_back(a, c);
}
int q;
cin >> q;
using a4 = array<int,5>;
vector<a4> qs(q);
vector<int> ans(q);
int qid = 0;
for (auto &[a,b,k,t, id] : qs) {
cin >> a >> b >> k >> t;
id = qid++;
a--, b--;
}
// ordena pelo k
sort(qs.begin(), qs.end(), [&](const a4 &a, const a4 &b) -> bool{
return a[2] < b[2];
});
// pegar o rank dos mais frios
vector<pair<int,int>> temp_id;
temp_id.reserve(n);
for (int i = 0; i < n; i++) {
temp_id.emplace_back(temp[i], i);
}
sort(temp_id.begin(), temp_id.end());
RANK[temp_id[0].second] = 1;
for (int i = 1; i < n; i++) {
auto [pt, pi] = temp_id[i-1];
auto [ct, ci] = temp_id[i];
RANK[ci] = RANK[pi] + (ct > pt);
}
int pi = 0; // proximo id a ser adicionado
for (int i = 0; i < q; i++) {
auto &[a, b, k, t, qid] = qs[i];
if (t) continue;
while (pi < n and RANK[temp_id[pi].second] <= k) {
auto x = temp_id[pi].second;
for (int u = 0; u < n; u++) {
for (int v = 0; v < n; v++) {
// usa x pra relaxar entre u e v
ll du = dist[u][x], dv = dist[x][v];
if (du + dv < dist[u][v]) {
dist[u][v] = du + dv;
}
}
}
pi++;
}
ans[qid] = dist[a][b];
}
// pegar o rank dos mais quentes
sort(temp_id.begin(), temp_id.end(), greater<>());
RANK[temp_id[0].second] = 1;
for (int i = 1; i < n; i++) {
auto [pt, pi] = temp_id[i-1];
auto [ct, ci] = temp_id[i];
RANK[ci] = RANK[pi] + (ct < pt);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j< n; j++) {
dist[i][j] = fdist[i][j];
}
}
pi = 0; // proximo id a ser adicionado
for (int i = 0; i < q; i++) {
auto &[a, b, k, t, qid] = qs[i];
if (!t) continue;
while (pi < n and RANK[temp_id[pi].second] <= k) {
auto x = temp_id[pi].second;
for (int u = 0; u < n; u++) {
for (int v = 0; v < n; v++) {
// usa x pra relaxar entre u e v
ll du = dist[u][x], dv = dist[x][v];
if (du + dv < dist[u][v]) {
dist[u][v] = du + dv;
}
}
}
pi++;
}
ans[qid] = dist[a][b];
}
for (int i = 0; i < q; i++) {
auto ai = ans[i];
cout << (ai == oo ? -1 : ai) << '\n';
}
}
// AC, graphs, floyd warshall