// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<ll, ll, ll> tll;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 2005;
const int MAXL = 100005;
const int BAG = 5;
int n, m, t, l, e;
vi adj[MAXN * 3];
int w[MAXN * 3];
int x[MAXL];
ll rd[MAXN][MAXN][BAG];
int fi[MAXN][MAXN][BAG], se[MAXN][MAXN][BAG];
int par[MAXN][2];
ll dist[MAXN * 3][MAXN * 3][2];
priority_queue<tll, vector<tll>, greater<tll>> pq;
void dijkstra(int s) {
REP (i, 0, e) {
dist[s][i][0] = dist[s][i][1] = LINF;
}
REP (i, 0, n + 1) {
par[i][0] = par[i][1] = -1;
if (s <= n) {
fi[s][i][0] = se[s][i][0] = -1;
}
}
dist[s][s][0] = w[s];
pq.push(tll{w[s], s, 0});
while (!pq.empty()) {
auto [d, u, tp] = pq.top(); pq.pop();
if (d != dist[s][u][tp]) continue;
for (int v : adj[u]) {
int cpar = par[u][tp];
if (u <= n && v > n && (v ^ 1) == cpar) {
continue;
}
if (u > n) {
cpar = adj[u ^ 1][0];
}
ll nd = d + w[v];
if (mnto(dist[s][v][0], nd)) {
pq.push(tll{nd, v, 0});
if (v <= n) {
if (s <= n) {
fi[s][v][0] = fi[s][cpar][0] == -1 ? u : fi[s][cpar][0];
se[s][v][0] = u;
}
par[v][0] = u;
}
} else if (mnto(dist[s][v][1], nd) && v <= n && par[v][0] != u) {
pq.push(tll{nd, v, 1});
par[v][1] = u;
}
}
}
}
bool toprint;
ll dp[MAXL * 4][BAG][BAG];
void upd(int p, int u = 1, int lo = 1, int hi = l) {
if (lo == hi) {
REP (i, 0, BAG) {
REP (j, 0, BAG) {
dp[u][i][j] = 0;
}
}
return;
}
int mid = lo + hi >> 1;
int lc = u << 1, rc = u << 1 ^ 1;
if (p <= mid) {
upd(p, lc, lo, mid);
} else {
upd(p, rc, mid + 1, hi);
}
toprint = p == 2 && hi == 2;
if (toprint) {
cerr << u << ' ' << lc << ' ' << rc << ' ' << ' ' << lo << ' ' << mid << ' ' << hi << '\n';
}
REP (l, 0, BAG) {
REP (r, 0, BAG) {
int a = -1, b = x[mid], c = x[mid + 1], d = -1;
if (mid - 1 >= lo) {
a = x[mid - 1];
}
if (mid + 2 <= hi) {
d = x[mid + 2];
}
dp[u][l][r] = LINF;
set<pll> lst, rst;
map<ll, vll> lmp, rmp;
REP (ml, 0, BAG) {
int pse = -1;
if (a != -1) {
pse = se[a][b][ml];
}
lst.insert({dp[lc][l][ml], pse});
lmp[pse].pb(dp[lc][l][ml]);
}
REP (mr, 0, BAG) {
int nfi = -1;
if (d != -1) {
nfi = fi[c][d][mr];
}
rst.insert({dp[rc][mr][r], nfi});
rmp[nfi].pb(dp[rc][mr][r]);
}
REP (k, 0, BAG) {
if (a == -1 && l != k) {
continue;
}
if (d == -1 && r != k) {
continue;
}
int cfi = fi[b][c][k] ^ 1;
for (ll x : lmp[cfi]) {
lst.erase({x, cfi});
}
int cse = se[b][c][k] ^ 1;
for (ll x : rmp[cse]) {
rst.erase({x, cse});
}
mnto(dp[u][l][r], lst.begin() -> FI +
rd[x[mid]][x[mid + 1]][k] +
rst.begin() -> FI);
for (ll x : lmp[cfi]) {
lst.insert({x, cfi});
}
for (ll x : rmp[cse]) {
rst.insert({x, cse});
}
}
/*
REP (ml, 0, BAG) {
REP (mr, 0, BAG) {
REP (k, 0, BAG) {
int a = -1, b = x[mid], c = x[mid + 1], d = -1;
if (mid - 1 >= lo) {
a = x[mid - 1];
}
if (mid + 2 <= hi) {
d = x[mid + 2];
}
if (a == -1 && l != k) {
continue;
}
if (d == -1 && r != k) {
continue;
}
int pse = -1;
if (a != -1) {
pse = se[a][b][ml];
}
int cfi = fi[b][c][k];
if ((cfi ^ 1) == pse) {
continue;
}
int cse = se[b][c][k];
int nfi = -1;
if (d != -1) {
nfi = fi[c][d][mr];
}
if ((cse ^ 1) == nfi) {
continue;
}
mnto(dp[u][l][r], dp[lc][l][ml] +
rd[x[mid]][x[mid + 1]][k] +
dp[rc][mr][r]);
}
}
}
*/
if (toprint) {
cerr << l << ' ' << r << ": " << dp[u][l][r] << '\n';
}
}
}
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n >> m >> t >> l;
e = n + 1;
if (e & 1) {
e++;
}
REP (i, 0, m) {
int a, b, c; cin >> a >> b >> c;
w[e] = c;
adj[a].pb(e);
adj[e].pb(b);
e++;
w[e] = c;
adj[b].pb(e);
adj[e].pb(a);
e++;
}
REP (i, 1, e) {
dijkstra(i);
}
REP (i, 1, n + 1) {
REP (j, 1, n + 1) {
if (i == j) continue;
REP (z, 0, BAG) {
rd[i][j][z] = LINF;
}
rd[i][j][0] = dist[i][j][0];
for (int u : adj[i]) {
for (int v : adj[j]) {
v ^= 1;
REP (z, 1, 3) {
if ((z == 1 && u == fi[i][j][0]) ||
(z == 2 && v == se[i][j][0])) {
continue;
}
if (mnto(rd[i][j][z], dist[u][v][0])) {
fi[i][j][z] = u;
se[i][j][z] = v;
}
}
}
}
if (rd[i][j][1] != LINF) {
for (int u : adj[i]) {
for (int v : adj[j]) {
v ^= 1;
if (u == fi[i][j][0] || v == se[i][j][1]) continue;
if (mnto(rd[i][j][3], dist[u][v][0])) {
fi[i][j][3] = u;
se[i][j][3] = v;
}
}
}
}
if (rd[i][j][2] != LINF) {
for (int u : adj[i]) {
for (int v : adj[j]) {
v ^= 1;
if (u == fi[i][j][2] || v == se[i][j][0]) continue;
if (mnto(rd[i][j][4], dist[u][v][0])) {
fi[i][j][4] = u;
se[i][j][4] = v;
}
}
}
}
/*
REP (z, 0, BAG) {
cerr << i << ' ' << j << ' ' << z << ": " << rd[i][j][z] << ' ' << fi[i][j][z] << ' ' << se[i][j][z] << '\n';
}
//*/
}
}
x[0] = -1;
REP (i, 1, l + 1) {
cin >> x[i];
}
REP (i, 1, l + 1) {
upd(i);
}
while (t--) {
int p, q; cin >> p >> q;
x[p] = q;
upd(p);
ll ans = LINF;
REP (i, 0, BAG) {
REP (j, 0, BAG) {
mnto(ans, dp[1][i][j]);
}
}
if (ans == LINF) {
ans = -1;
}
cout << ans << '\n';
}
return 0;
}
Compilation message
wild_boar.cpp: In function 'void upd(int, int, int, int)':
wild_boar.cpp:101:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
101 | int mid = lo + hi >> 1;
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
692 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
736 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
3 ms |
724 KB |
Output is correct |
15 |
Correct |
4 ms |
724 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
664 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
692 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
736 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
3 ms |
724 KB |
Output is correct |
15 |
Correct |
4 ms |
724 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
664 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
41 ms |
1376 KB |
Output is correct |
27 |
Execution timed out |
18005 ms |
27164 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
692 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
736 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
3 ms |
724 KB |
Output is correct |
15 |
Correct |
4 ms |
724 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
664 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
41 ms |
1376 KB |
Output is correct |
27 |
Execution timed out |
18005 ms |
27164 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
692 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
736 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
3 ms |
724 KB |
Output is correct |
15 |
Correct |
4 ms |
724 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
664 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
41 ms |
1376 KB |
Output is correct |
27 |
Execution timed out |
18005 ms |
27164 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |