#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) ( (int)(x).size() )
using LL = long long;
template<class T>
inline bool asMn(T &a, const T &b) { return a > b ? a = b, true : false; }
template<class T>
inline bool asMx(T &a, const T &b) { return a < b ? a = b, true : false; }
const LL infLL = 1e18;
mt19937 rng( (int)chrono::steady_clock::now().time_since_epoch().count() );
int n, nDay, nSta;
struct Edge {
int from, to, w;
Edge(int _from, int _to, int _w) : from(_from), to(_to), w(_w) {}
};
vector<Edge> edge;
vector<vector<int> > gr;
vector<vector<LL> > d;
void dijkstra(int s) {
d[s][s] = edge[s].w;
priority_queue<pair<LL, int> > pq;
pq.emplace(-d[s][s], s);
while (sz(pq) ) {
LL curD = -pq.top().first; int u = pq.top().second; pq.pop();
if (curD ^ d[s][u]) continue ;
for (int v : gr[edge[u].to]) if ( (u >> 1) ^ (v >> 1) ) {
if (asMn(d[s][v], curD + edge[v].w) ) pq.emplace(-d[s][v], v);
}
}
}
vector<vector<vector<pair<int, int> > > > optimalE;
vector<int> x;
struct It {
#define lC (i << 1)
#define rC (i << 1 | 1)
#define Mid ( (Left + Right) >> 1)
#define rt int i = 1, int Left = 0, int Right = nSta - 2
struct Node {
map<pair<int, int>, LL> val;
Node() { val.clear(); }
Node operator + (const Node &_) const {
Node ret;
for (auto i : val) {
for (auto j : _.val) if ( (i.first.second >> 1) ^ (j.first.first >> 1) ) {
if (ret.val.count(make_pair(i.first.first, j.first.second) ) ) asMn(ret.val[make_pair(i.first.first, j.first.second)], i.second + j.second);
else ret.val[make_pair(i.first.first, j.first.second)] = i.second + j.second;
}
}
return ret;
}
};
vector<Node> node;
It(int nNode) { node.assign(nNode, Node() ); }
void build(rt) {
if (!(Left ^ Right) ) {
for (auto _ : optimalE[ x[Left] ][ x[Left + 1] ]) if (_ != make_pair(-1, -1) ) node[i].val[_] = d[_.first][_.second];
// cerr << "Left:" << Left << " Right:" << Right << '\n';
// for (auto _ : node[i].val) cerr << _.first.first << ' ' << _.first.second << ' ' << _.second << '\n';
return ;
}
build(lC, Left, Mid);
build(rC, Mid + 1, Right);
node[i] = node[lC] + node[rC];
// cerr << "Left:" << Left << " Right:" << Right << '\n';
// for (auto _ : node[i].val) cerr << _.first.first << ' ' << _.first.second << ' ' << _.second << '\n';
}
void upd(int pos, rt) {
if (Left == Right) {
node[i].val.clear();
for (auto _ : optimalE[ x[Left] ][ x[Left + 1] ]) if (_ != make_pair(-1, -1) ) node[i].val[_] = d[_.first][_.second];
return ;
}
if (pos <= Mid) upd(pos, lC, Left, Mid);
else upd(pos, rC, Mid + 1, Right);
node[i] = node[lC] + node[rC];
}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef FourLeafClover
freopen("input", "r", stdin);
#endif // FourLeafClover
int m; cin >> n >> m >> nDay >> nSta;
gr.assign(n, {} );
while (m--) {
int u, v, w; cin >> u >> v >> w; --u; --v;
gr[u].emplace_back(sz(edge) ); edge.emplace_back(u, v, w);
gr[v].emplace_back(sz(edge) ); edge.emplace_back(v, u, w);
}
d.assign(sz(edge), vector<LL>(sz(edge), infLL) );
for (int s = 0; s < sz(edge); ++s) dijkstra(s);
optimalE.assign(n, vector<vector<pair<int, int> > >(n) );
for (int from = 0; from < n; ++from) {
for (int to = 0; to < n; ++to) if (from ^ to) {
optimalE[from][to].emplace_back(-1, -1);
LL mn = infLL;
for (int s : gr[from]) {
for (int t : gr[to]) if (asMn(mn, d[s][t ^ 1]) ) optimalE[from][to].back() = { s, t ^ 1 };
}
optimalE[from][to].emplace_back(-1, -1);
mn = infLL;
for (int s : gr[from]) if (s ^ optimalE[from][to][0].first) {
for (int t : gr[to]) if (t ^ 1 ^ optimalE[from][to][0].second && asMn(mn, d[s][t ^ 1]) ) optimalE[from][to].back() = { s, t ^ 1 };
}
optimalE[from][to].emplace_back(-1, -1);
mn = infLL;
for (int s : gr[from]) if (s ^ optimalE[from][to][0].first) {
for (int t : gr[to]) if (t ^ 1 ^ optimalE[from][to][1].second && asMn(mn, d[s][t ^ 1]) ) optimalE[from][to].back() = { s, t ^ 1 };
}
optimalE[from][to].emplace_back(-1, -1);
mn = infLL;
for (int s : gr[from]) if (s ^ optimalE[from][to][1].first) {
for (int t : gr[to]) if (t ^ 1 ^ optimalE[from][to][0].second && asMn(mn, d[s][t ^ 1]) ) optimalE[from][to].back() = { s, t ^ 1 };
}
// for (int s : gr[from]) {
// for (int t : gr[to]) optimalE[from][to].emplace_back(s, t ^ 1);
// }
}
}
x.assign(nSta, 0);
for (auto &i : x) cin >> i, --i;
It it( (nSta + 5) << 2); it.build();
// LL ans = infLL;
// for (auto _ : it.node[1].val) asMn(ans, _.second);
// cerr << (ans ^ infLL ? ans : -1) << '\n';
while (nDay--) {
int p, v; cin >> p >> v; --p; --v;
x[p] = v;
if (p) it.upd(p - 1);
if (p + 1 < nSta) it.upd(p);
LL ans = infLL;
for (auto _ : it.node[1].val) asMn(ans, _.second);
cout << (ans ^ infLL ? ans : -1) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
7 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
380 KB |
Output is correct |
11 |
Correct |
5 ms |
380 KB |
Output is correct |
12 |
Correct |
5 ms |
380 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
380 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
7 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
380 KB |
Output is correct |
11 |
Correct |
5 ms |
380 KB |
Output is correct |
12 |
Correct |
5 ms |
380 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
380 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
7 ms |
504 KB |
Output is correct |
27 |
Correct |
59 ms |
30436 KB |
Output is correct |
28 |
Correct |
53 ms |
28920 KB |
Output is correct |
29 |
Correct |
432 ms |
93048 KB |
Output is correct |
30 |
Correct |
476 ms |
99576 KB |
Output is correct |
31 |
Correct |
434 ms |
95480 KB |
Output is correct |
32 |
Correct |
408 ms |
93048 KB |
Output is correct |
33 |
Correct |
404 ms |
87844 KB |
Output is correct |
34 |
Correct |
389 ms |
87544 KB |
Output is correct |
35 |
Correct |
383 ms |
92920 KB |
Output is correct |
36 |
Correct |
473 ms |
103928 KB |
Output is correct |
37 |
Correct |
419 ms |
89532 KB |
Output is correct |
38 |
Correct |
347 ms |
82936 KB |
Output is correct |
39 |
Correct |
599 ms |
122236 KB |
Output is correct |
40 |
Correct |
362 ms |
83832 KB |
Output is correct |
41 |
Correct |
369 ms |
83832 KB |
Output is correct |
42 |
Correct |
365 ms |
95096 KB |
Output is correct |
43 |
Correct |
331 ms |
80888 KB |
Output is correct |
44 |
Correct |
324 ms |
80632 KB |
Output is correct |
45 |
Correct |
176 ms |
54136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
7 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
380 KB |
Output is correct |
11 |
Correct |
5 ms |
380 KB |
Output is correct |
12 |
Correct |
5 ms |
380 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
380 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
7 ms |
504 KB |
Output is correct |
27 |
Correct |
59 ms |
30436 KB |
Output is correct |
28 |
Correct |
53 ms |
28920 KB |
Output is correct |
29 |
Correct |
432 ms |
93048 KB |
Output is correct |
30 |
Correct |
476 ms |
99576 KB |
Output is correct |
31 |
Correct |
434 ms |
95480 KB |
Output is correct |
32 |
Correct |
408 ms |
93048 KB |
Output is correct |
33 |
Correct |
404 ms |
87844 KB |
Output is correct |
34 |
Correct |
389 ms |
87544 KB |
Output is correct |
35 |
Correct |
383 ms |
92920 KB |
Output is correct |
36 |
Correct |
473 ms |
103928 KB |
Output is correct |
37 |
Correct |
419 ms |
89532 KB |
Output is correct |
38 |
Correct |
347 ms |
82936 KB |
Output is correct |
39 |
Correct |
599 ms |
122236 KB |
Output is correct |
40 |
Correct |
362 ms |
83832 KB |
Output is correct |
41 |
Correct |
369 ms |
83832 KB |
Output is correct |
42 |
Correct |
365 ms |
95096 KB |
Output is correct |
43 |
Correct |
331 ms |
80888 KB |
Output is correct |
44 |
Correct |
324 ms |
80632 KB |
Output is correct |
45 |
Correct |
176 ms |
54136 KB |
Output is correct |
46 |
Correct |
200 ms |
11128 KB |
Output is correct |
47 |
Correct |
8385 ms |
237688 KB |
Output is correct |
48 |
Correct |
7266 ms |
267640 KB |
Output is correct |
49 |
Correct |
5646 ms |
295840 KB |
Output is correct |
50 |
Correct |
6082 ms |
292868 KB |
Output is correct |
51 |
Correct |
6978 ms |
289168 KB |
Output is correct |
52 |
Correct |
3655 ms |
279544 KB |
Output is correct |
53 |
Correct |
3681 ms |
279084 KB |
Output is correct |
54 |
Correct |
3668 ms |
279020 KB |
Output is correct |
55 |
Correct |
3651 ms |
278552 KB |
Output is correct |
56 |
Correct |
3616 ms |
292192 KB |
Output is correct |
57 |
Correct |
3616 ms |
307576 KB |
Output is correct |
58 |
Correct |
3599 ms |
323448 KB |
Output is correct |
59 |
Correct |
3528 ms |
341240 KB |
Output is correct |
60 |
Correct |
3455 ms |
359928 KB |
Output is correct |
61 |
Correct |
3395 ms |
380280 KB |
Output is correct |
62 |
Correct |
3266 ms |
402168 KB |
Output is correct |
63 |
Correct |
3189 ms |
425944 KB |
Output is correct |
64 |
Correct |
1726 ms |
452344 KB |
Output is correct |
65 |
Correct |
1714 ms |
452472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
7 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
380 KB |
Output is correct |
11 |
Correct |
5 ms |
380 KB |
Output is correct |
12 |
Correct |
5 ms |
380 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
380 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
7 ms |
504 KB |
Output is correct |
27 |
Correct |
59 ms |
30436 KB |
Output is correct |
28 |
Correct |
53 ms |
28920 KB |
Output is correct |
29 |
Correct |
432 ms |
93048 KB |
Output is correct |
30 |
Correct |
476 ms |
99576 KB |
Output is correct |
31 |
Correct |
434 ms |
95480 KB |
Output is correct |
32 |
Correct |
408 ms |
93048 KB |
Output is correct |
33 |
Correct |
404 ms |
87844 KB |
Output is correct |
34 |
Correct |
389 ms |
87544 KB |
Output is correct |
35 |
Correct |
383 ms |
92920 KB |
Output is correct |
36 |
Correct |
473 ms |
103928 KB |
Output is correct |
37 |
Correct |
419 ms |
89532 KB |
Output is correct |
38 |
Correct |
347 ms |
82936 KB |
Output is correct |
39 |
Correct |
599 ms |
122236 KB |
Output is correct |
40 |
Correct |
362 ms |
83832 KB |
Output is correct |
41 |
Correct |
369 ms |
83832 KB |
Output is correct |
42 |
Correct |
365 ms |
95096 KB |
Output is correct |
43 |
Correct |
331 ms |
80888 KB |
Output is correct |
44 |
Correct |
324 ms |
80632 KB |
Output is correct |
45 |
Correct |
176 ms |
54136 KB |
Output is correct |
46 |
Correct |
200 ms |
11128 KB |
Output is correct |
47 |
Correct |
8385 ms |
237688 KB |
Output is correct |
48 |
Correct |
7266 ms |
267640 KB |
Output is correct |
49 |
Correct |
5646 ms |
295840 KB |
Output is correct |
50 |
Correct |
6082 ms |
292868 KB |
Output is correct |
51 |
Correct |
6978 ms |
289168 KB |
Output is correct |
52 |
Correct |
3655 ms |
279544 KB |
Output is correct |
53 |
Correct |
3681 ms |
279084 KB |
Output is correct |
54 |
Correct |
3668 ms |
279020 KB |
Output is correct |
55 |
Correct |
3651 ms |
278552 KB |
Output is correct |
56 |
Correct |
3616 ms |
292192 KB |
Output is correct |
57 |
Correct |
3616 ms |
307576 KB |
Output is correct |
58 |
Correct |
3599 ms |
323448 KB |
Output is correct |
59 |
Correct |
3528 ms |
341240 KB |
Output is correct |
60 |
Correct |
3455 ms |
359928 KB |
Output is correct |
61 |
Correct |
3395 ms |
380280 KB |
Output is correct |
62 |
Correct |
3266 ms |
402168 KB |
Output is correct |
63 |
Correct |
3189 ms |
425944 KB |
Output is correct |
64 |
Correct |
1726 ms |
452344 KB |
Output is correct |
65 |
Correct |
1714 ms |
452472 KB |
Output is correct |
66 |
Correct |
335 ms |
86264 KB |
Output is correct |
67 |
Correct |
555 ms |
103416 KB |
Output is correct |
68 |
Correct |
1583 ms |
407776 KB |
Output is correct |
69 |
Correct |
1773 ms |
408824 KB |
Output is correct |
70 |
Correct |
1951 ms |
435704 KB |
Output is correct |
71 |
Execution timed out |
18074 ms |
246696 KB |
Time limit exceeded |
72 |
Halted |
0 ms |
0 KB |
- |