#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define eps 1e-8
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
// change if necessary
#define MAXN 2010
#define MAXL 100010
using namespace std;
struct path {
int start, end;
ll length;
path(int s, int e, ll l) : start(s), end(e), length(l) {}
path() : path(0, 0, 0) {}
};
struct min_cost {
vector<path> min;
};
int n, m, t, l;
int nums[MAXL];
min_cost min_dist[MAXN][MAXN];
vector<int> adj[MAXN];
vector<pair<int, ll>> edge;
bool same(int x, int y) {
return x == y || (x ^ 1) == y;
}
bool set_vals(vector<path> &cur, ll cost, int first, int last) {
if (cur.size() >= 5) return false;
int x_cnt = 0;
int y_cnt = 0;
for (auto &p : cur) {
if (p.start == first) x_cnt++;
if (p.end == last) y_cnt++;
if (p.start == first && p.end == last) return false;
}
if (x_cnt < 2 && y_cnt < 2) {
cur.emplace_back(first, last, cost);
return true;
}
return false;
}
// segtree stuff
struct segtree {
struct node {
int l, r, sz;
vector<path> combo;
node(int l, int r, int sz) : l(l), r(r), sz(sz) {}
node() : node(0, 0, 0) {}
int size() const { return sz; }
};
static int n;
node st[4 * MAXL];
void combo(node &ans, const vector<path> &left, const vector<path> &right) {
map<pair<int, int>, ll> m;
for (auto &p1 : left) {
for (auto &p2 : right) {
if (same(p1.end, p2.start) || p2.length == 0) continue;
pair<int, int> pp = {p1.start, p2.end};
if (!m.count(pp)) {
m[pp] = p1.length + p2.length;
} else {
m[pp] = min(m[pp], p1.length + p2.length);
}
}
}
vector<path> v;
for (auto p : m) {
v.emplace_back(p.first.first, p.first.second, p.second);
}
sort(v.begin(), v.end(), [](const path &p1, const path &p2) {
return p1.length < p2.length;
});
for (auto p : v) {
set_vals(ans.combo, p.length, p.start, p.end);
}
}
void combo(node &ans, const vector<path> &left, const vector<path> &right, int x1, int x2) {
map<pair<int, int>, ll> m;
for (auto &p1 : left) {
for (auto &p2 : right) {
for (auto &join : min_dist[x1][x2].min) {
if (same(p1.end, join.start) || same(join.end, p2.start) || join.length == 0) continue;
pair<int, int> pp = {p1.start, p2.end};
if (!m.count(pp)) {
m[pp] = p1.length + p2.length + join.length;
} else {
m[pp] = min(m[pp], p1.length + p2.length + join.length);
}
}
}
}
vector<path> v;
for (auto p : m) {
v.emplace_back(p.first.first, p.first.second, p.second);
}
sort(v.begin(), v.end(), [](const path &p1, const path &p2) {
return p1.length < p2.length;
});
for (auto p : v) {
set_vals(ans.combo, p.length, p.start, p.end);
}
}
node merge(const node &left, const node &right) {
node ans;
ans.l = left.l;
ans.r = right.r;
ans.sz = left.sz + right.sz;
if (left.size() == 1) {
for (path &p : min_dist[left.l][right.r].min) {
if (p.length > 0) {
ans.combo.push_back(p);
}
}
return ans;
}
if (right.size() == 1) {
combo(ans, left.combo, min_dist[left.r][right.l].min);
} else {
combo(ans, left.combo, right.combo, left.r, right.l);
}
return ans;
}
void build(int v = 1, int l = 0, int r = n - 1) {
if (l == r) {
st[v] = node(nums[l], nums[l], 1);
} else {
int m = (l + r) / 2;
build(2 * v, l, m);
build(2 * v + 1, m + 1, r);
st[v] = merge(st[2 * v], st[2 * v + 1]);
}
}
void upd(int x, int y, int v = 1, int l = 0, int r = n - 1) {
if (l == r) {
nums[x] = y;
st[v] = node(nums[l], nums[l], 1);
} else {
int m = (l + r) / 2;
if (x <= m) {
upd(x, y, 2 * v, l, m);
} else {
upd(x, y, 2 * v + 1, m + 1, r);
}
st[v] = merge(st[2 * v], st[2 * v + 1]);
}
}
} st;
int segtree::n = 1;
void dijk(int x) {
// {dist, pos, first edge, last edge}
priority_queue<array<ll, 4>, vector<array<ll, 4>>, greater<array<ll, 4>>> pq;
min_dist[x][x].min.emplace_back(x, x, 0);
for (int e : adj[x]) {
auto [nxt, cost] = edge[e];
pq.push({cost, nxt, e, e});
}
while (!pq.empty()) {
auto [cost, pos, first, last] = pq.top();
pq.pop();
bool res = set_vals(min_dist[x][pos].min, cost, first, last);
if (!res) continue;
for (int e : adj[pos]) {
if (!same(e, last)) {
auto [nxt, cost2] = edge[e];
pq.push({cost + cost2, nxt, first, e});
}
}
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> t >> l;
for (int i = 0; i < m; i++) {
int a, b; ll c;
cin >> a >> b >> c;
a--; b--;
adj[a].push_back(edge.size());
edge.push_back({b, c});
adj[b].push_back(edge.size());
edge.push_back({a, c});
}
for (int i = 0; i < n; i++) {
dijk(i);
}
for (int i = 0; i < l; i++) {
cin >> nums[i];
nums[i]--;
}
segtree::n = l;
st.build();
for (int i = 0; i < t; i++) {
int p, q; cin >> p >> q;
p--; q--;
st.upd(p, q);
if (st.st[1].combo.empty()) {
cout << "-1\n";
} else {
ll ans = INFLL;
for (auto &p : st.st[1].combo) {
ans = min(ans, p.length);
}
cout << ans << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
110956 KB |
Output is correct |
2 |
Correct |
77 ms |
110956 KB |
Output is correct |
3 |
Correct |
75 ms |
110956 KB |
Output is correct |
4 |
Correct |
75 ms |
110956 KB |
Output is correct |
5 |
Correct |
76 ms |
111212 KB |
Output is correct |
6 |
Correct |
75 ms |
110956 KB |
Output is correct |
7 |
Correct |
75 ms |
110956 KB |
Output is correct |
8 |
Correct |
75 ms |
110956 KB |
Output is correct |
9 |
Correct |
75 ms |
110956 KB |
Output is correct |
10 |
Correct |
77 ms |
111084 KB |
Output is correct |
11 |
Correct |
76 ms |
110944 KB |
Output is correct |
12 |
Correct |
79 ms |
110956 KB |
Output is correct |
13 |
Correct |
77 ms |
110956 KB |
Output is correct |
14 |
Correct |
76 ms |
110956 KB |
Output is correct |
15 |
Correct |
76 ms |
110956 KB |
Output is correct |
16 |
Correct |
75 ms |
111084 KB |
Output is correct |
17 |
Correct |
75 ms |
110956 KB |
Output is correct |
18 |
Correct |
76 ms |
110956 KB |
Output is correct |
19 |
Correct |
76 ms |
110956 KB |
Output is correct |
20 |
Correct |
78 ms |
110956 KB |
Output is correct |
21 |
Correct |
76 ms |
111084 KB |
Output is correct |
22 |
Correct |
75 ms |
110956 KB |
Output is correct |
23 |
Correct |
77 ms |
110956 KB |
Output is correct |
24 |
Correct |
75 ms |
110956 KB |
Output is correct |
25 |
Correct |
76 ms |
110956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
110956 KB |
Output is correct |
2 |
Correct |
77 ms |
110956 KB |
Output is correct |
3 |
Correct |
75 ms |
110956 KB |
Output is correct |
4 |
Correct |
75 ms |
110956 KB |
Output is correct |
5 |
Correct |
76 ms |
111212 KB |
Output is correct |
6 |
Correct |
75 ms |
110956 KB |
Output is correct |
7 |
Correct |
75 ms |
110956 KB |
Output is correct |
8 |
Correct |
75 ms |
110956 KB |
Output is correct |
9 |
Correct |
75 ms |
110956 KB |
Output is correct |
10 |
Correct |
77 ms |
111084 KB |
Output is correct |
11 |
Correct |
76 ms |
110944 KB |
Output is correct |
12 |
Correct |
79 ms |
110956 KB |
Output is correct |
13 |
Correct |
77 ms |
110956 KB |
Output is correct |
14 |
Correct |
76 ms |
110956 KB |
Output is correct |
15 |
Correct |
76 ms |
110956 KB |
Output is correct |
16 |
Correct |
75 ms |
111084 KB |
Output is correct |
17 |
Correct |
75 ms |
110956 KB |
Output is correct |
18 |
Correct |
76 ms |
110956 KB |
Output is correct |
19 |
Correct |
76 ms |
110956 KB |
Output is correct |
20 |
Correct |
78 ms |
110956 KB |
Output is correct |
21 |
Correct |
76 ms |
111084 KB |
Output is correct |
22 |
Correct |
75 ms |
110956 KB |
Output is correct |
23 |
Correct |
77 ms |
110956 KB |
Output is correct |
24 |
Correct |
75 ms |
110956 KB |
Output is correct |
25 |
Correct |
76 ms |
110956 KB |
Output is correct |
26 |
Correct |
77 ms |
111084 KB |
Output is correct |
27 |
Correct |
99 ms |
113260 KB |
Output is correct |
28 |
Correct |
93 ms |
113260 KB |
Output is correct |
29 |
Correct |
416 ms |
126060 KB |
Output is correct |
30 |
Correct |
448 ms |
126444 KB |
Output is correct |
31 |
Correct |
445 ms |
126956 KB |
Output is correct |
32 |
Correct |
434 ms |
127084 KB |
Output is correct |
33 |
Correct |
367 ms |
125384 KB |
Output is correct |
34 |
Correct |
403 ms |
125420 KB |
Output is correct |
35 |
Correct |
396 ms |
127852 KB |
Output is correct |
36 |
Correct |
467 ms |
128236 KB |
Output is correct |
37 |
Correct |
402 ms |
126316 KB |
Output is correct |
38 |
Correct |
325 ms |
124780 KB |
Output is correct |
39 |
Correct |
455 ms |
129260 KB |
Output is correct |
40 |
Correct |
317 ms |
124652 KB |
Output is correct |
41 |
Correct |
317 ms |
124524 KB |
Output is correct |
42 |
Correct |
368 ms |
130032 KB |
Output is correct |
43 |
Correct |
277 ms |
124908 KB |
Output is correct |
44 |
Correct |
279 ms |
124908 KB |
Output is correct |
45 |
Correct |
157 ms |
120812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
110956 KB |
Output is correct |
2 |
Correct |
77 ms |
110956 KB |
Output is correct |
3 |
Correct |
75 ms |
110956 KB |
Output is correct |
4 |
Correct |
75 ms |
110956 KB |
Output is correct |
5 |
Correct |
76 ms |
111212 KB |
Output is correct |
6 |
Correct |
75 ms |
110956 KB |
Output is correct |
7 |
Correct |
75 ms |
110956 KB |
Output is correct |
8 |
Correct |
75 ms |
110956 KB |
Output is correct |
9 |
Correct |
75 ms |
110956 KB |
Output is correct |
10 |
Correct |
77 ms |
111084 KB |
Output is correct |
11 |
Correct |
76 ms |
110944 KB |
Output is correct |
12 |
Correct |
79 ms |
110956 KB |
Output is correct |
13 |
Correct |
77 ms |
110956 KB |
Output is correct |
14 |
Correct |
76 ms |
110956 KB |
Output is correct |
15 |
Correct |
76 ms |
110956 KB |
Output is correct |
16 |
Correct |
75 ms |
111084 KB |
Output is correct |
17 |
Correct |
75 ms |
110956 KB |
Output is correct |
18 |
Correct |
76 ms |
110956 KB |
Output is correct |
19 |
Correct |
76 ms |
110956 KB |
Output is correct |
20 |
Correct |
78 ms |
110956 KB |
Output is correct |
21 |
Correct |
76 ms |
111084 KB |
Output is correct |
22 |
Correct |
75 ms |
110956 KB |
Output is correct |
23 |
Correct |
77 ms |
110956 KB |
Output is correct |
24 |
Correct |
75 ms |
110956 KB |
Output is correct |
25 |
Correct |
76 ms |
110956 KB |
Output is correct |
26 |
Correct |
77 ms |
111084 KB |
Output is correct |
27 |
Correct |
99 ms |
113260 KB |
Output is correct |
28 |
Correct |
93 ms |
113260 KB |
Output is correct |
29 |
Correct |
416 ms |
126060 KB |
Output is correct |
30 |
Correct |
448 ms |
126444 KB |
Output is correct |
31 |
Correct |
445 ms |
126956 KB |
Output is correct |
32 |
Correct |
434 ms |
127084 KB |
Output is correct |
33 |
Correct |
367 ms |
125384 KB |
Output is correct |
34 |
Correct |
403 ms |
125420 KB |
Output is correct |
35 |
Correct |
396 ms |
127852 KB |
Output is correct |
36 |
Correct |
467 ms |
128236 KB |
Output is correct |
37 |
Correct |
402 ms |
126316 KB |
Output is correct |
38 |
Correct |
325 ms |
124780 KB |
Output is correct |
39 |
Correct |
455 ms |
129260 KB |
Output is correct |
40 |
Correct |
317 ms |
124652 KB |
Output is correct |
41 |
Correct |
317 ms |
124524 KB |
Output is correct |
42 |
Correct |
368 ms |
130032 KB |
Output is correct |
43 |
Correct |
277 ms |
124908 KB |
Output is correct |
44 |
Correct |
279 ms |
124908 KB |
Output is correct |
45 |
Correct |
157 ms |
120812 KB |
Output is correct |
46 |
Correct |
233 ms |
116844 KB |
Output is correct |
47 |
Correct |
2237 ms |
160468 KB |
Output is correct |
48 |
Correct |
2917 ms |
193056 KB |
Output is correct |
49 |
Correct |
3380 ms |
219316 KB |
Output is correct |
50 |
Correct |
3373 ms |
219212 KB |
Output is correct |
51 |
Correct |
3286 ms |
220624 KB |
Output is correct |
52 |
Correct |
3586 ms |
248648 KB |
Output is correct |
53 |
Correct |
3565 ms |
249240 KB |
Output is correct |
54 |
Correct |
3571 ms |
247864 KB |
Output is correct |
55 |
Correct |
3527 ms |
246432 KB |
Output is correct |
56 |
Correct |
3728 ms |
263344 KB |
Output is correct |
57 |
Correct |
3834 ms |
281416 KB |
Output is correct |
58 |
Correct |
3853 ms |
292912 KB |
Output is correct |
59 |
Correct |
3925 ms |
307952 KB |
Output is correct |
60 |
Correct |
3953 ms |
322332 KB |
Output is correct |
61 |
Correct |
3916 ms |
337928 KB |
Output is correct |
62 |
Correct |
3840 ms |
356488 KB |
Output is correct |
63 |
Correct |
3831 ms |
377884 KB |
Output is correct |
64 |
Correct |
1565 ms |
304720 KB |
Output is correct |
65 |
Correct |
1570 ms |
304740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
110956 KB |
Output is correct |
2 |
Correct |
77 ms |
110956 KB |
Output is correct |
3 |
Correct |
75 ms |
110956 KB |
Output is correct |
4 |
Correct |
75 ms |
110956 KB |
Output is correct |
5 |
Correct |
76 ms |
111212 KB |
Output is correct |
6 |
Correct |
75 ms |
110956 KB |
Output is correct |
7 |
Correct |
75 ms |
110956 KB |
Output is correct |
8 |
Correct |
75 ms |
110956 KB |
Output is correct |
9 |
Correct |
75 ms |
110956 KB |
Output is correct |
10 |
Correct |
77 ms |
111084 KB |
Output is correct |
11 |
Correct |
76 ms |
110944 KB |
Output is correct |
12 |
Correct |
79 ms |
110956 KB |
Output is correct |
13 |
Correct |
77 ms |
110956 KB |
Output is correct |
14 |
Correct |
76 ms |
110956 KB |
Output is correct |
15 |
Correct |
76 ms |
110956 KB |
Output is correct |
16 |
Correct |
75 ms |
111084 KB |
Output is correct |
17 |
Correct |
75 ms |
110956 KB |
Output is correct |
18 |
Correct |
76 ms |
110956 KB |
Output is correct |
19 |
Correct |
76 ms |
110956 KB |
Output is correct |
20 |
Correct |
78 ms |
110956 KB |
Output is correct |
21 |
Correct |
76 ms |
111084 KB |
Output is correct |
22 |
Correct |
75 ms |
110956 KB |
Output is correct |
23 |
Correct |
77 ms |
110956 KB |
Output is correct |
24 |
Correct |
75 ms |
110956 KB |
Output is correct |
25 |
Correct |
76 ms |
110956 KB |
Output is correct |
26 |
Correct |
77 ms |
111084 KB |
Output is correct |
27 |
Correct |
99 ms |
113260 KB |
Output is correct |
28 |
Correct |
93 ms |
113260 KB |
Output is correct |
29 |
Correct |
416 ms |
126060 KB |
Output is correct |
30 |
Correct |
448 ms |
126444 KB |
Output is correct |
31 |
Correct |
445 ms |
126956 KB |
Output is correct |
32 |
Correct |
434 ms |
127084 KB |
Output is correct |
33 |
Correct |
367 ms |
125384 KB |
Output is correct |
34 |
Correct |
403 ms |
125420 KB |
Output is correct |
35 |
Correct |
396 ms |
127852 KB |
Output is correct |
36 |
Correct |
467 ms |
128236 KB |
Output is correct |
37 |
Correct |
402 ms |
126316 KB |
Output is correct |
38 |
Correct |
325 ms |
124780 KB |
Output is correct |
39 |
Correct |
455 ms |
129260 KB |
Output is correct |
40 |
Correct |
317 ms |
124652 KB |
Output is correct |
41 |
Correct |
317 ms |
124524 KB |
Output is correct |
42 |
Correct |
368 ms |
130032 KB |
Output is correct |
43 |
Correct |
277 ms |
124908 KB |
Output is correct |
44 |
Correct |
279 ms |
124908 KB |
Output is correct |
45 |
Correct |
157 ms |
120812 KB |
Output is correct |
46 |
Correct |
233 ms |
116844 KB |
Output is correct |
47 |
Correct |
2237 ms |
160468 KB |
Output is correct |
48 |
Correct |
2917 ms |
193056 KB |
Output is correct |
49 |
Correct |
3380 ms |
219316 KB |
Output is correct |
50 |
Correct |
3373 ms |
219212 KB |
Output is correct |
51 |
Correct |
3286 ms |
220624 KB |
Output is correct |
52 |
Correct |
3586 ms |
248648 KB |
Output is correct |
53 |
Correct |
3565 ms |
249240 KB |
Output is correct |
54 |
Correct |
3571 ms |
247864 KB |
Output is correct |
55 |
Correct |
3527 ms |
246432 KB |
Output is correct |
56 |
Correct |
3728 ms |
263344 KB |
Output is correct |
57 |
Correct |
3834 ms |
281416 KB |
Output is correct |
58 |
Correct |
3853 ms |
292912 KB |
Output is correct |
59 |
Correct |
3925 ms |
307952 KB |
Output is correct |
60 |
Correct |
3953 ms |
322332 KB |
Output is correct |
61 |
Correct |
3916 ms |
337928 KB |
Output is correct |
62 |
Correct |
3840 ms |
356488 KB |
Output is correct |
63 |
Correct |
3831 ms |
377884 KB |
Output is correct |
64 |
Correct |
1565 ms |
304720 KB |
Output is correct |
65 |
Correct |
1570 ms |
304740 KB |
Output is correct |
66 |
Correct |
300 ms |
122092 KB |
Output is correct |
67 |
Correct |
373 ms |
144748 KB |
Output is correct |
68 |
Correct |
986 ms |
239860 KB |
Output is correct |
69 |
Correct |
1096 ms |
242436 KB |
Output is correct |
70 |
Correct |
1257 ms |
243820 KB |
Output is correct |
71 |
Correct |
7818 ms |
162520 KB |
Output is correct |
72 |
Correct |
11515 ms |
201724 KB |
Output is correct |
73 |
Correct |
7850 ms |
250408 KB |
Output is correct |
74 |
Correct |
7941 ms |
253880 KB |
Output is correct |
75 |
Correct |
7841 ms |
250500 KB |
Output is correct |
76 |
Correct |
8943 ms |
220068 KB |
Output is correct |
77 |
Correct |
11327 ms |
222004 KB |
Output is correct |
78 |
Correct |
9309 ms |
219260 KB |
Output is correct |
79 |
Correct |
7512 ms |
282716 KB |
Output is correct |
80 |
Correct |
7461 ms |
298128 KB |
Output is correct |
81 |
Correct |
11431 ms |
284456 KB |
Output is correct |
82 |
Correct |
7132 ms |
325056 KB |
Output is correct |
83 |
Correct |
12048 ms |
329644 KB |
Output is correct |
84 |
Correct |
6615 ms |
381368 KB |
Output is correct |
85 |
Correct |
2484 ms |
307564 KB |
Output is correct |