// 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], radj[MAXN];
int w[MAXN * 3];
int x[MAXL];
ll rd[MAXN][MAXN][BAG];
int rfi[MAXN][MAXN][BAG], rse[MAXN][MAXN][BAG];
ll dp[MAXL][BAG];
int par[MAXN * 3][2];
ll dist[MAXN * 3][MAXN * 3][2];
int fi[MAXN * 3][MAXN * 3], se[MAXN * 3][MAXN * 3];
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;
fi[s][i] = se[s][i] = -1;
par[i][0] = par[i][1] = -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]) {
if (v > n && (v ^ 1) == par[u][tp]) {
continue;
}
ll nd = d + w[v];
if (mnto(dist[s][v][0], nd)) {
if (par[v][0] != -1 && par[v][0] != u) {
dist[s][v][1] = dist[s][v][0];
par[v][1] = par[v][0];
pq.push(tll{dist[s][v][1], v, 1});
}
pq.push(tll{nd, v, 0});
fi[s][v] = fi[s][u] == -1 ? v : fi[s][u];
se[s][v] = u;
par[v][0] = u;
} else if (mnto(dist[s][v][1], nd) && par[v][0] != u) {
pq.push(tll{nd, v, 1});
par[v][1] = u;
}
}
}
}
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);
radj[a].pb(e++);
w[e] = c;
adj[b].pb(e);
adj[e].pb(a);
radj[b].pb(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];
rfi[i][j][0] = fi[i][j]; rse[i][j][0] = se[i][j];
for (int u : radj[i]) {
for (int v : radj[j]) {
v ^= 1;
REP (z, 1, 3) {
if ((z == 1 && u == fi[i][j]) ||
(z == 2 && v == se[i][j])) {
continue;
}
cerr << u << ' ' << v << ' ' << z << '\n';
if (mnto(rd[i][j][z], dist[u][v][0])) {
rfi[i][j][z] = u;
rse[i][j][z] = v;
}
}
}
}
if (rd[i][j][1] != LINF) {
for (int u : radj[i]) {
for (int v : radj[j]) {
v ^= 1;
if (u == fi[i][j] || v == rse[i][j][1]) continue;
if (mnto(rd[i][j][3], dist[u][v][0])) {
rfi[i][j][3] = u;
rse[i][j][3] = v;
}
}
}
}
if (rd[i][j][2] != LINF) {
for (int u : radj[i]) {
for (int v : radj[j]) {
v ^= 1;
if (u == rfi[i][j][2] || v == se[i][j]) continue;
if (mnto(rd[i][j][4], dist[u][v][0])) {
rfi[i][j][4] = u;
rse[i][j][4] = v;
}
}
}
}
REP (z, 0, BAG) {
cerr << i << ' ' << j << ' ' << z << ": " << rd[i][j][z] << ' ' << rfi[i][j][z] << ' ' << rse[i][j][z] << '\n';
}
cerr << ' ' << fi[i][j] << ' ' << se[i][j] << '\n';
}
}
x[0] = -1;
REP (i, 1, l + 1) {
cin >> x[i];
}
while (t--) {
int p, q; cin >> p >> q;
x[p] = q;
REP (j, 0, BAG) {
dp[1][j] = 0;
}
REP (i, 2, l + 1) {
int p = x[i - 2], u = x[i - 1], v = x[i];
REP (j, 0, BAG) {
dp[i][j] = LINF;
REP (k, 0, BAG) {
if (p != -1 && (rse[p][u][k] ^ 1) == rfi[u][v][j]) continue;
mnto(dp[i][j], dp[i - 1][k] + rd[u][v][j]);
}
cerr << i << ' ' << j << ": " << dp[i][j] << '\n';
}
}
ll ans = LINF;
REP (j, 0, BAG) {
mnto(ans, dp[l][j]);
}
if (ans >= LINF) {
ans = -1;
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
908 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
1032 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
1032 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
1108 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
852 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
908 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
1032 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
1032 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
1108 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
852 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
3 ms |
2260 KB |
Output is correct |
27 |
Correct |
20 ms |
13004 KB |
Output is correct |
28 |
Correct |
18 ms |
12948 KB |
Output is correct |
29 |
Correct |
91 ms |
27260 KB |
Output is correct |
30 |
Correct |
83 ms |
27224 KB |
Output is correct |
31 |
Correct |
79 ms |
27180 KB |
Output is correct |
32 |
Correct |
79 ms |
27256 KB |
Output is correct |
33 |
Correct |
103 ms |
31260 KB |
Output is correct |
34 |
Correct |
105 ms |
31220 KB |
Output is correct |
35 |
Correct |
85 ms |
31020 KB |
Output is correct |
36 |
Correct |
89 ms |
31084 KB |
Output is correct |
37 |
Correct |
107 ms |
31356 KB |
Output is correct |
38 |
Correct |
110 ms |
35636 KB |
Output is correct |
39 |
Correct |
100 ms |
35560 KB |
Output is correct |
40 |
Correct |
114 ms |
35648 KB |
Output is correct |
41 |
Correct |
115 ms |
35660 KB |
Output is correct |
42 |
Correct |
99 ms |
38468 KB |
Output is correct |
43 |
Correct |
111 ms |
40596 KB |
Output is correct |
44 |
Correct |
110 ms |
40568 KB |
Output is correct |
45 |
Correct |
58 ms |
46028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
908 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
1032 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
1032 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
1108 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
852 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
3 ms |
2260 KB |
Output is correct |
27 |
Correct |
20 ms |
13004 KB |
Output is correct |
28 |
Correct |
18 ms |
12948 KB |
Output is correct |
29 |
Correct |
91 ms |
27260 KB |
Output is correct |
30 |
Correct |
83 ms |
27224 KB |
Output is correct |
31 |
Correct |
79 ms |
27180 KB |
Output is correct |
32 |
Correct |
79 ms |
27256 KB |
Output is correct |
33 |
Correct |
103 ms |
31260 KB |
Output is correct |
34 |
Correct |
105 ms |
31220 KB |
Output is correct |
35 |
Correct |
85 ms |
31020 KB |
Output is correct |
36 |
Correct |
89 ms |
31084 KB |
Output is correct |
37 |
Correct |
107 ms |
31356 KB |
Output is correct |
38 |
Correct |
110 ms |
35636 KB |
Output is correct |
39 |
Correct |
100 ms |
35560 KB |
Output is correct |
40 |
Correct |
114 ms |
35648 KB |
Output is correct |
41 |
Correct |
115 ms |
35660 KB |
Output is correct |
42 |
Correct |
99 ms |
38468 KB |
Output is correct |
43 |
Correct |
111 ms |
40596 KB |
Output is correct |
44 |
Correct |
110 ms |
40568 KB |
Output is correct |
45 |
Correct |
58 ms |
46028 KB |
Output is correct |
46 |
Correct |
261 ms |
54832 KB |
Output is correct |
47 |
Correct |
3142 ms |
561264 KB |
Output is correct |
48 |
Correct |
3597 ms |
664472 KB |
Output is correct |
49 |
Correct |
3933 ms |
742984 KB |
Output is correct |
50 |
Correct |
3931 ms |
743008 KB |
Output is correct |
51 |
Correct |
3977 ms |
742948 KB |
Output is correct |
52 |
Correct |
4249 ms |
743180 KB |
Output is correct |
53 |
Correct |
4319 ms |
743072 KB |
Output is correct |
54 |
Correct |
4264 ms |
743096 KB |
Output is correct |
55 |
Correct |
4244 ms |
743220 KB |
Output is correct |
56 |
Correct |
4755 ms |
781756 KB |
Output is correct |
57 |
Correct |
5046 ms |
822216 KB |
Output is correct |
58 |
Correct |
4686 ms |
864460 KB |
Output is correct |
59 |
Correct |
5005 ms |
908544 KB |
Output is correct |
60 |
Correct |
5450 ms |
954612 KB |
Output is correct |
61 |
Correct |
4846 ms |
1002600 KB |
Output is correct |
62 |
Runtime error |
4956 ms |
1048576 KB |
Execution killed with signal 9 |
63 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
908 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
1032 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
1032 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
1108 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
852 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
3 ms |
2260 KB |
Output is correct |
27 |
Correct |
20 ms |
13004 KB |
Output is correct |
28 |
Correct |
18 ms |
12948 KB |
Output is correct |
29 |
Correct |
91 ms |
27260 KB |
Output is correct |
30 |
Correct |
83 ms |
27224 KB |
Output is correct |
31 |
Correct |
79 ms |
27180 KB |
Output is correct |
32 |
Correct |
79 ms |
27256 KB |
Output is correct |
33 |
Correct |
103 ms |
31260 KB |
Output is correct |
34 |
Correct |
105 ms |
31220 KB |
Output is correct |
35 |
Correct |
85 ms |
31020 KB |
Output is correct |
36 |
Correct |
89 ms |
31084 KB |
Output is correct |
37 |
Correct |
107 ms |
31356 KB |
Output is correct |
38 |
Correct |
110 ms |
35636 KB |
Output is correct |
39 |
Correct |
100 ms |
35560 KB |
Output is correct |
40 |
Correct |
114 ms |
35648 KB |
Output is correct |
41 |
Correct |
115 ms |
35660 KB |
Output is correct |
42 |
Correct |
99 ms |
38468 KB |
Output is correct |
43 |
Correct |
111 ms |
40596 KB |
Output is correct |
44 |
Correct |
110 ms |
40568 KB |
Output is correct |
45 |
Correct |
58 ms |
46028 KB |
Output is correct |
46 |
Correct |
261 ms |
54832 KB |
Output is correct |
47 |
Correct |
3142 ms |
561264 KB |
Output is correct |
48 |
Correct |
3597 ms |
664472 KB |
Output is correct |
49 |
Correct |
3933 ms |
742984 KB |
Output is correct |
50 |
Correct |
3931 ms |
743008 KB |
Output is correct |
51 |
Correct |
3977 ms |
742948 KB |
Output is correct |
52 |
Correct |
4249 ms |
743180 KB |
Output is correct |
53 |
Correct |
4319 ms |
743072 KB |
Output is correct |
54 |
Correct |
4264 ms |
743096 KB |
Output is correct |
55 |
Correct |
4244 ms |
743220 KB |
Output is correct |
56 |
Correct |
4755 ms |
781756 KB |
Output is correct |
57 |
Correct |
5046 ms |
822216 KB |
Output is correct |
58 |
Correct |
4686 ms |
864460 KB |
Output is correct |
59 |
Correct |
5005 ms |
908544 KB |
Output is correct |
60 |
Correct |
5450 ms |
954612 KB |
Output is correct |
61 |
Correct |
4846 ms |
1002600 KB |
Output is correct |
62 |
Runtime error |
4956 ms |
1048576 KB |
Execution killed with signal 9 |
63 |
Halted |
0 ms |
0 KB |
- |