#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 2e3 + 10; // 1e5 ?
const ll MAXL = 1e5 + 10;
const ll INF = 4e18;
vector<int> adj[MAXN];
int from[MAXN], to[MAXN], n, m, t, l, sup_list[MAXL];
ll dist[MAXN][3][MAXN][3], w[MAXN], ans[2][MAXN << 2];
inline int f(int ind, int v) {
return from[ind] + to[ind] - v;
}
inline int edge_side(int ind, int v) {
return from[ind] == v ? 0 : 1;
}
inline void dijkstra(int tv, int tstate) {
dist[tv][tstate][tv][tstate] = 0;
priority_queue<plll, vector<plll>, greater<plll>> pq;
pq.push({0, {tv, tstate}});
while (!pq.empty()) {
int fv = pq.top().Y.X, fstate = pq.top().Y.Y;
ll d_v = pq.top().X;
pq.pop();
if (d_v != dist[tv][tstate][fv][fstate]) continue;
int lst = fv, v = 0;
if (fstate == 0) v = from[lst];
else if (fstate == 1) v = to[lst];
else v = lst, lst = -1;
for (int ind : adj[v]) {
if (ind == lst) continue;
int u = f(ind, v), state = edge_side(ind, u);
ll d_u = d_v + w[ind];
if (d_u < dist[tv][tstate][ind][state]) {
dist[tv][tstate][ind][state] = d_u;
pq.push({d_u, {ind, state}});
}
}
}
}
inline vector<pll> v_states(int v) {
vector<pll> ans;
for (int ind : adj[v])
ans.push_back({ind, edge_side(ind, v)});
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m >> t >> l;
for (int i = 1; i <= m; i++) {
cin >> from[i] >> to[i] >> w[i];
adj[from[i]].push_back(i);
adj[to[i]].push_back(i);
}
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < 3; j++)
for (int a = 0; a < MAXN; a++)
for (int b = 0; b < 3; b++)
dist[i][j][a][b] = INF;
for (int v = 1; v <= n; v++)
dijkstra(v, 2);
for (int i = 1; i <= m; i++)
dijkstra(i, 1), dijkstra(i, 0);
for (int i = 1; i <= l; i++) cin >> sup_list[i];
while (t--) {
int p, c;
cin >> p >> c;
sup_list[p] = c;
}
ans[1][0] = 0; // !!!
ll fans = INF;
for (int i = 2; i <= l; i++) {
vector<pll> A = v_states(sup_list[i - 1]), B = v_states(sup_list[i]);
int ind = i & 1;
if (i == 2) A = {{sup_list[i - 1], 2}};
fill(ans[ind], ans[ind] + int(B.size()), INF);
fans = INF;
for (int i = 0; i < int(B.size()); i++) {
for (int j = 0; j < int(A.size()); j++)
ans[ind][i] = min(ans[ind][i], ans[ind ^ 1][j] + dist[A[j].X][A[j].Y][B[i].X][B[i].Y]);
fans = min(fans, ans[ind][i]);
}
/*
if (i == 3) {
for (pll e : B)
cerr << e.X << sep << e.Y << endl;
debug(ans[ind][1])
}
*/ }
cout << (fans == INF ? -1 : fans) << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
284908 KB |
Output is correct |
2 |
Correct |
110 ms |
284992 KB |
Output is correct |
3 |
Correct |
117 ms |
285004 KB |
Output is correct |
4 |
Correct |
109 ms |
284968 KB |
Output is correct |
5 |
Correct |
108 ms |
284912 KB |
Output is correct |
6 |
Correct |
109 ms |
284884 KB |
Output is correct |
7 |
Correct |
111 ms |
284996 KB |
Output is correct |
8 |
Correct |
109 ms |
284968 KB |
Output is correct |
9 |
Correct |
108 ms |
284992 KB |
Output is correct |
10 |
Correct |
107 ms |
284908 KB |
Output is correct |
11 |
Correct |
106 ms |
284988 KB |
Output is correct |
12 |
Correct |
111 ms |
285024 KB |
Output is correct |
13 |
Correct |
111 ms |
284960 KB |
Output is correct |
14 |
Correct |
106 ms |
284892 KB |
Output is correct |
15 |
Correct |
110 ms |
284916 KB |
Output is correct |
16 |
Correct |
110 ms |
284884 KB |
Output is correct |
17 |
Correct |
110 ms |
284988 KB |
Output is correct |
18 |
Correct |
109 ms |
284980 KB |
Output is correct |
19 |
Correct |
108 ms |
284976 KB |
Output is correct |
20 |
Correct |
110 ms |
284964 KB |
Output is correct |
21 |
Correct |
120 ms |
284996 KB |
Output is correct |
22 |
Correct |
110 ms |
284868 KB |
Output is correct |
23 |
Correct |
109 ms |
284948 KB |
Output is correct |
24 |
Correct |
108 ms |
284928 KB |
Output is correct |
25 |
Correct |
108 ms |
284984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
284908 KB |
Output is correct |
2 |
Correct |
110 ms |
284992 KB |
Output is correct |
3 |
Correct |
117 ms |
285004 KB |
Output is correct |
4 |
Correct |
109 ms |
284968 KB |
Output is correct |
5 |
Correct |
108 ms |
284912 KB |
Output is correct |
6 |
Correct |
109 ms |
284884 KB |
Output is correct |
7 |
Correct |
111 ms |
284996 KB |
Output is correct |
8 |
Correct |
109 ms |
284968 KB |
Output is correct |
9 |
Correct |
108 ms |
284992 KB |
Output is correct |
10 |
Correct |
107 ms |
284908 KB |
Output is correct |
11 |
Correct |
106 ms |
284988 KB |
Output is correct |
12 |
Correct |
111 ms |
285024 KB |
Output is correct |
13 |
Correct |
111 ms |
284960 KB |
Output is correct |
14 |
Correct |
106 ms |
284892 KB |
Output is correct |
15 |
Correct |
110 ms |
284916 KB |
Output is correct |
16 |
Correct |
110 ms |
284884 KB |
Output is correct |
17 |
Correct |
110 ms |
284988 KB |
Output is correct |
18 |
Correct |
109 ms |
284980 KB |
Output is correct |
19 |
Correct |
108 ms |
284976 KB |
Output is correct |
20 |
Correct |
110 ms |
284964 KB |
Output is correct |
21 |
Correct |
120 ms |
284996 KB |
Output is correct |
22 |
Correct |
110 ms |
284868 KB |
Output is correct |
23 |
Correct |
109 ms |
284948 KB |
Output is correct |
24 |
Correct |
108 ms |
284928 KB |
Output is correct |
25 |
Correct |
108 ms |
284984 KB |
Output is correct |
26 |
Correct |
114 ms |
284916 KB |
Output is correct |
27 |
Correct |
137 ms |
285660 KB |
Output is correct |
28 |
Correct |
132 ms |
285628 KB |
Output is correct |
29 |
Correct |
229 ms |
285588 KB |
Output is correct |
30 |
Correct |
350 ms |
285564 KB |
Output is correct |
31 |
Correct |
1465 ms |
285680 KB |
Output is correct |
32 |
Correct |
1603 ms |
285620 KB |
Output is correct |
33 |
Correct |
221 ms |
285764 KB |
Output is correct |
34 |
Correct |
208 ms |
285712 KB |
Output is correct |
35 |
Correct |
1916 ms |
285608 KB |
Output is correct |
36 |
Correct |
997 ms |
285616 KB |
Output is correct |
37 |
Correct |
235 ms |
285668 KB |
Output is correct |
38 |
Correct |
206 ms |
285712 KB |
Output is correct |
39 |
Correct |
550 ms |
285612 KB |
Output is correct |
40 |
Correct |
202 ms |
285632 KB |
Output is correct |
41 |
Correct |
196 ms |
285708 KB |
Output is correct |
42 |
Correct |
558 ms |
285732 KB |
Output is correct |
43 |
Correct |
191 ms |
285664 KB |
Output is correct |
44 |
Correct |
195 ms |
285732 KB |
Output is correct |
45 |
Correct |
163 ms |
285860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
284908 KB |
Output is correct |
2 |
Correct |
110 ms |
284992 KB |
Output is correct |
3 |
Correct |
117 ms |
285004 KB |
Output is correct |
4 |
Correct |
109 ms |
284968 KB |
Output is correct |
5 |
Correct |
108 ms |
284912 KB |
Output is correct |
6 |
Correct |
109 ms |
284884 KB |
Output is correct |
7 |
Correct |
111 ms |
284996 KB |
Output is correct |
8 |
Correct |
109 ms |
284968 KB |
Output is correct |
9 |
Correct |
108 ms |
284992 KB |
Output is correct |
10 |
Correct |
107 ms |
284908 KB |
Output is correct |
11 |
Correct |
106 ms |
284988 KB |
Output is correct |
12 |
Correct |
111 ms |
285024 KB |
Output is correct |
13 |
Correct |
111 ms |
284960 KB |
Output is correct |
14 |
Correct |
106 ms |
284892 KB |
Output is correct |
15 |
Correct |
110 ms |
284916 KB |
Output is correct |
16 |
Correct |
110 ms |
284884 KB |
Output is correct |
17 |
Correct |
110 ms |
284988 KB |
Output is correct |
18 |
Correct |
109 ms |
284980 KB |
Output is correct |
19 |
Correct |
108 ms |
284976 KB |
Output is correct |
20 |
Correct |
110 ms |
284964 KB |
Output is correct |
21 |
Correct |
120 ms |
284996 KB |
Output is correct |
22 |
Correct |
110 ms |
284868 KB |
Output is correct |
23 |
Correct |
109 ms |
284948 KB |
Output is correct |
24 |
Correct |
108 ms |
284928 KB |
Output is correct |
25 |
Correct |
108 ms |
284984 KB |
Output is correct |
26 |
Correct |
114 ms |
284916 KB |
Output is correct |
27 |
Correct |
137 ms |
285660 KB |
Output is correct |
28 |
Correct |
132 ms |
285628 KB |
Output is correct |
29 |
Correct |
229 ms |
285588 KB |
Output is correct |
30 |
Correct |
350 ms |
285564 KB |
Output is correct |
31 |
Correct |
1465 ms |
285680 KB |
Output is correct |
32 |
Correct |
1603 ms |
285620 KB |
Output is correct |
33 |
Correct |
221 ms |
285764 KB |
Output is correct |
34 |
Correct |
208 ms |
285712 KB |
Output is correct |
35 |
Correct |
1916 ms |
285608 KB |
Output is correct |
36 |
Correct |
997 ms |
285616 KB |
Output is correct |
37 |
Correct |
235 ms |
285668 KB |
Output is correct |
38 |
Correct |
206 ms |
285712 KB |
Output is correct |
39 |
Correct |
550 ms |
285612 KB |
Output is correct |
40 |
Correct |
202 ms |
285632 KB |
Output is correct |
41 |
Correct |
196 ms |
285708 KB |
Output is correct |
42 |
Correct |
558 ms |
285732 KB |
Output is correct |
43 |
Correct |
191 ms |
285664 KB |
Output is correct |
44 |
Correct |
195 ms |
285732 KB |
Output is correct |
45 |
Correct |
163 ms |
285860 KB |
Output is correct |
46 |
Correct |
310 ms |
284996 KB |
Output is correct |
47 |
Execution timed out |
18087 ms |
285868 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
284908 KB |
Output is correct |
2 |
Correct |
110 ms |
284992 KB |
Output is correct |
3 |
Correct |
117 ms |
285004 KB |
Output is correct |
4 |
Correct |
109 ms |
284968 KB |
Output is correct |
5 |
Correct |
108 ms |
284912 KB |
Output is correct |
6 |
Correct |
109 ms |
284884 KB |
Output is correct |
7 |
Correct |
111 ms |
284996 KB |
Output is correct |
8 |
Correct |
109 ms |
284968 KB |
Output is correct |
9 |
Correct |
108 ms |
284992 KB |
Output is correct |
10 |
Correct |
107 ms |
284908 KB |
Output is correct |
11 |
Correct |
106 ms |
284988 KB |
Output is correct |
12 |
Correct |
111 ms |
285024 KB |
Output is correct |
13 |
Correct |
111 ms |
284960 KB |
Output is correct |
14 |
Correct |
106 ms |
284892 KB |
Output is correct |
15 |
Correct |
110 ms |
284916 KB |
Output is correct |
16 |
Correct |
110 ms |
284884 KB |
Output is correct |
17 |
Correct |
110 ms |
284988 KB |
Output is correct |
18 |
Correct |
109 ms |
284980 KB |
Output is correct |
19 |
Correct |
108 ms |
284976 KB |
Output is correct |
20 |
Correct |
110 ms |
284964 KB |
Output is correct |
21 |
Correct |
120 ms |
284996 KB |
Output is correct |
22 |
Correct |
110 ms |
284868 KB |
Output is correct |
23 |
Correct |
109 ms |
284948 KB |
Output is correct |
24 |
Correct |
108 ms |
284928 KB |
Output is correct |
25 |
Correct |
108 ms |
284984 KB |
Output is correct |
26 |
Correct |
114 ms |
284916 KB |
Output is correct |
27 |
Correct |
137 ms |
285660 KB |
Output is correct |
28 |
Correct |
132 ms |
285628 KB |
Output is correct |
29 |
Correct |
229 ms |
285588 KB |
Output is correct |
30 |
Correct |
350 ms |
285564 KB |
Output is correct |
31 |
Correct |
1465 ms |
285680 KB |
Output is correct |
32 |
Correct |
1603 ms |
285620 KB |
Output is correct |
33 |
Correct |
221 ms |
285764 KB |
Output is correct |
34 |
Correct |
208 ms |
285712 KB |
Output is correct |
35 |
Correct |
1916 ms |
285608 KB |
Output is correct |
36 |
Correct |
997 ms |
285616 KB |
Output is correct |
37 |
Correct |
235 ms |
285668 KB |
Output is correct |
38 |
Correct |
206 ms |
285712 KB |
Output is correct |
39 |
Correct |
550 ms |
285612 KB |
Output is correct |
40 |
Correct |
202 ms |
285632 KB |
Output is correct |
41 |
Correct |
196 ms |
285708 KB |
Output is correct |
42 |
Correct |
558 ms |
285732 KB |
Output is correct |
43 |
Correct |
191 ms |
285664 KB |
Output is correct |
44 |
Correct |
195 ms |
285732 KB |
Output is correct |
45 |
Correct |
163 ms |
285860 KB |
Output is correct |
46 |
Correct |
310 ms |
284996 KB |
Output is correct |
47 |
Execution timed out |
18087 ms |
285868 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |