#include <bits/stdc++.h>
using namespace std;
const long long INF = (1LL << 60);
long long N, M, T, L, X[100009], A[4009], B[4009], C[4009], dist[4009][4009];
long long prev_[1009], dp[1009]; vector<int> G[4009];
vector<pair<long long, int>> E[2009][4009];
int anti(int pos) {
if (pos < M) return pos + M;
return pos - M;
}
long long solve() {
for (int i = 0; i < 2 * M; i++) { prev_[i] = INF; dp[i] = INF; }
for (int i = 0; i < 2 * M; i++) {
if (B[i] == X[1]) prev_[i] = 0;
}
for (int i = 2; i <= L; i++) {
// 前半パート
vector<pair<long long, int>> F;
for (int j = 0; j < 2 * M; j++) {
if (prev_[j] < INF) F.push_back(make_pair(prev_[j], j));
}
sort(F.begin(), F.end());
// 後半パート
for (int j = 0; j < (int)G[X[i]].size(); j++) {
int pos = anti(G[X[i]][j]), B = min(2, (int)F.size());
for (int k = 0; k < (int)E[X[i - 1]][pos].size(); k++) {
for (int l = 0; l < B; l++) {
if (abs(F[l].second - E[X[i - 1]][pos][k].second) == M && i >= 3) continue;
dp[pos] = min(dp[pos], F[l].first + E[X[i - 1]][pos][k].first);
}
}
}
for (int j = 0; j < 2 * M; j++) { prev_[j] = dp[j]; dp[j] = INF; }
}
long long ans = (1LL << 60);
for (int i = 0; i < 2 * M; i++) ans = min(ans, prev_[i]);
return ans;
}
int main() {
// 入力
cin >> N >> M >> T >> L;
for (int i = 0; i < M; i++) cin >> A[i] >> B[i] >> C[i];
for (int i = 0; i < M; i++) { A[i + M] = B[i]; B[i + M] = A[i]; C[i + M] = C[i]; }
for (int i = 0; i < 2 * M; i++) G[A[i]].push_back(i);
// ワーシャルフロイドで距離を求める
for (int i = 0; i < 2 * M; i++) {
for (int j = 0; j < 2 * M; j++) {
if (i == j) dist[i][j] = 0;
else if (B[i] == A[j] && A[i] != B[j]) dist[i][j] = C[j];
else dist[i][j] = INF;
}
}
for (int k = 0; k < 2 * M; k++) {
for (int i = 0; i < 2 * M; i++) {
for (int j = 0; j < 2 * M; j++) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
for (int i = 0; i < 2 * M; i++) {
for (int j = 0; j < 2 * M; j++) { if (dist[i][j] < INF) dist[i][j] += C[i]; }
}
// 各頂点から各辺に行くコストを求める
for (int i = 1; i <= N; i++) {
for (int j = 0; j < 2 * M; j++) {
for (int k = 0; k < (int)G[i].size(); k++) {
E[i][j].push_back(make_pair(dist[G[i][k]][j], G[i][k]));
}
sort(E[i][j].begin(), E[i][j].end());
while (E[i][j].size() >= 3) E[i][j].pop_back();
}
}
// ここからが本質
for (int i = 1; i <= L; i++) cin >> X[i];
for (int i = 1; i <= T; i++) {
int p, q; cin >> p >> q; X[p] = q;
long long P = solve();
if (P == INF) P = -1;
cout << P << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
189688 KB |
Output is correct |
2 |
Correct |
181 ms |
189532 KB |
Output is correct |
3 |
Correct |
181 ms |
189688 KB |
Output is correct |
4 |
Correct |
202 ms |
189816 KB |
Output is correct |
5 |
Correct |
172 ms |
189688 KB |
Output is correct |
6 |
Correct |
177 ms |
189620 KB |
Output is correct |
7 |
Correct |
187 ms |
189688 KB |
Output is correct |
8 |
Correct |
178 ms |
189688 KB |
Output is correct |
9 |
Correct |
185 ms |
189660 KB |
Output is correct |
10 |
Correct |
169 ms |
189660 KB |
Output is correct |
11 |
Correct |
173 ms |
189728 KB |
Output is correct |
12 |
Correct |
167 ms |
189684 KB |
Output is correct |
13 |
Correct |
207 ms |
189688 KB |
Output is correct |
14 |
Correct |
181 ms |
189688 KB |
Output is correct |
15 |
Correct |
191 ms |
189820 KB |
Output is correct |
16 |
Correct |
165 ms |
189772 KB |
Output is correct |
17 |
Correct |
179 ms |
189832 KB |
Output is correct |
18 |
Correct |
187 ms |
189688 KB |
Output is correct |
19 |
Correct |
186 ms |
189688 KB |
Output is correct |
20 |
Correct |
170 ms |
189660 KB |
Output is correct |
21 |
Correct |
175 ms |
189816 KB |
Output is correct |
22 |
Correct |
203 ms |
189764 KB |
Output is correct |
23 |
Correct |
172 ms |
189744 KB |
Output is correct |
24 |
Correct |
182 ms |
189688 KB |
Output is correct |
25 |
Correct |
190 ms |
189660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
189688 KB |
Output is correct |
2 |
Correct |
181 ms |
189532 KB |
Output is correct |
3 |
Correct |
181 ms |
189688 KB |
Output is correct |
4 |
Correct |
202 ms |
189816 KB |
Output is correct |
5 |
Correct |
172 ms |
189688 KB |
Output is correct |
6 |
Correct |
177 ms |
189620 KB |
Output is correct |
7 |
Correct |
187 ms |
189688 KB |
Output is correct |
8 |
Correct |
178 ms |
189688 KB |
Output is correct |
9 |
Correct |
185 ms |
189660 KB |
Output is correct |
10 |
Correct |
169 ms |
189660 KB |
Output is correct |
11 |
Correct |
173 ms |
189728 KB |
Output is correct |
12 |
Correct |
167 ms |
189684 KB |
Output is correct |
13 |
Correct |
207 ms |
189688 KB |
Output is correct |
14 |
Correct |
181 ms |
189688 KB |
Output is correct |
15 |
Correct |
191 ms |
189820 KB |
Output is correct |
16 |
Correct |
165 ms |
189772 KB |
Output is correct |
17 |
Correct |
179 ms |
189832 KB |
Output is correct |
18 |
Correct |
187 ms |
189688 KB |
Output is correct |
19 |
Correct |
186 ms |
189688 KB |
Output is correct |
20 |
Correct |
170 ms |
189660 KB |
Output is correct |
21 |
Correct |
175 ms |
189816 KB |
Output is correct |
22 |
Correct |
203 ms |
189764 KB |
Output is correct |
23 |
Correct |
172 ms |
189744 KB |
Output is correct |
24 |
Correct |
182 ms |
189688 KB |
Output is correct |
25 |
Correct |
190 ms |
189660 KB |
Output is correct |
26 |
Correct |
171 ms |
190136 KB |
Output is correct |
27 |
Correct |
243 ms |
192764 KB |
Output is correct |
28 |
Correct |
260 ms |
192860 KB |
Output is correct |
29 |
Correct |
786 ms |
204136 KB |
Output is correct |
30 |
Correct |
800 ms |
204280 KB |
Output is correct |
31 |
Correct |
1080 ms |
204664 KB |
Output is correct |
32 |
Correct |
1059 ms |
205176 KB |
Output is correct |
33 |
Correct |
730 ms |
204664 KB |
Output is correct |
34 |
Correct |
785 ms |
204664 KB |
Output is correct |
35 |
Correct |
1097 ms |
204664 KB |
Output is correct |
36 |
Correct |
907 ms |
204024 KB |
Output is correct |
37 |
Correct |
786 ms |
204712 KB |
Output is correct |
38 |
Correct |
687 ms |
204696 KB |
Output is correct |
39 |
Correct |
795 ms |
204552 KB |
Output is correct |
40 |
Correct |
759 ms |
204764 KB |
Output is correct |
41 |
Correct |
628 ms |
204920 KB |
Output is correct |
42 |
Correct |
982 ms |
204920 KB |
Output is correct |
43 |
Correct |
736 ms |
204664 KB |
Output is correct |
44 |
Correct |
643 ms |
204792 KB |
Output is correct |
45 |
Correct |
655 ms |
204536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
189688 KB |
Output is correct |
2 |
Correct |
181 ms |
189532 KB |
Output is correct |
3 |
Correct |
181 ms |
189688 KB |
Output is correct |
4 |
Correct |
202 ms |
189816 KB |
Output is correct |
5 |
Correct |
172 ms |
189688 KB |
Output is correct |
6 |
Correct |
177 ms |
189620 KB |
Output is correct |
7 |
Correct |
187 ms |
189688 KB |
Output is correct |
8 |
Correct |
178 ms |
189688 KB |
Output is correct |
9 |
Correct |
185 ms |
189660 KB |
Output is correct |
10 |
Correct |
169 ms |
189660 KB |
Output is correct |
11 |
Correct |
173 ms |
189728 KB |
Output is correct |
12 |
Correct |
167 ms |
189684 KB |
Output is correct |
13 |
Correct |
207 ms |
189688 KB |
Output is correct |
14 |
Correct |
181 ms |
189688 KB |
Output is correct |
15 |
Correct |
191 ms |
189820 KB |
Output is correct |
16 |
Correct |
165 ms |
189772 KB |
Output is correct |
17 |
Correct |
179 ms |
189832 KB |
Output is correct |
18 |
Correct |
187 ms |
189688 KB |
Output is correct |
19 |
Correct |
186 ms |
189688 KB |
Output is correct |
20 |
Correct |
170 ms |
189660 KB |
Output is correct |
21 |
Correct |
175 ms |
189816 KB |
Output is correct |
22 |
Correct |
203 ms |
189764 KB |
Output is correct |
23 |
Correct |
172 ms |
189744 KB |
Output is correct |
24 |
Correct |
182 ms |
189688 KB |
Output is correct |
25 |
Correct |
190 ms |
189660 KB |
Output is correct |
26 |
Correct |
171 ms |
190136 KB |
Output is correct |
27 |
Correct |
243 ms |
192764 KB |
Output is correct |
28 |
Correct |
260 ms |
192860 KB |
Output is correct |
29 |
Correct |
786 ms |
204136 KB |
Output is correct |
30 |
Correct |
800 ms |
204280 KB |
Output is correct |
31 |
Correct |
1080 ms |
204664 KB |
Output is correct |
32 |
Correct |
1059 ms |
205176 KB |
Output is correct |
33 |
Correct |
730 ms |
204664 KB |
Output is correct |
34 |
Correct |
785 ms |
204664 KB |
Output is correct |
35 |
Correct |
1097 ms |
204664 KB |
Output is correct |
36 |
Correct |
907 ms |
204024 KB |
Output is correct |
37 |
Correct |
786 ms |
204712 KB |
Output is correct |
38 |
Correct |
687 ms |
204696 KB |
Output is correct |
39 |
Correct |
795 ms |
204552 KB |
Output is correct |
40 |
Correct |
759 ms |
204764 KB |
Output is correct |
41 |
Correct |
628 ms |
204920 KB |
Output is correct |
42 |
Correct |
982 ms |
204920 KB |
Output is correct |
43 |
Correct |
736 ms |
204664 KB |
Output is correct |
44 |
Correct |
643 ms |
204792 KB |
Output is correct |
45 |
Correct |
655 ms |
204536 KB |
Output is correct |
46 |
Correct |
1508 ms |
224188 KB |
Output is correct |
47 |
Execution timed out |
18090 ms |
315384 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
189688 KB |
Output is correct |
2 |
Correct |
181 ms |
189532 KB |
Output is correct |
3 |
Correct |
181 ms |
189688 KB |
Output is correct |
4 |
Correct |
202 ms |
189816 KB |
Output is correct |
5 |
Correct |
172 ms |
189688 KB |
Output is correct |
6 |
Correct |
177 ms |
189620 KB |
Output is correct |
7 |
Correct |
187 ms |
189688 KB |
Output is correct |
8 |
Correct |
178 ms |
189688 KB |
Output is correct |
9 |
Correct |
185 ms |
189660 KB |
Output is correct |
10 |
Correct |
169 ms |
189660 KB |
Output is correct |
11 |
Correct |
173 ms |
189728 KB |
Output is correct |
12 |
Correct |
167 ms |
189684 KB |
Output is correct |
13 |
Correct |
207 ms |
189688 KB |
Output is correct |
14 |
Correct |
181 ms |
189688 KB |
Output is correct |
15 |
Correct |
191 ms |
189820 KB |
Output is correct |
16 |
Correct |
165 ms |
189772 KB |
Output is correct |
17 |
Correct |
179 ms |
189832 KB |
Output is correct |
18 |
Correct |
187 ms |
189688 KB |
Output is correct |
19 |
Correct |
186 ms |
189688 KB |
Output is correct |
20 |
Correct |
170 ms |
189660 KB |
Output is correct |
21 |
Correct |
175 ms |
189816 KB |
Output is correct |
22 |
Correct |
203 ms |
189764 KB |
Output is correct |
23 |
Correct |
172 ms |
189744 KB |
Output is correct |
24 |
Correct |
182 ms |
189688 KB |
Output is correct |
25 |
Correct |
190 ms |
189660 KB |
Output is correct |
26 |
Correct |
171 ms |
190136 KB |
Output is correct |
27 |
Correct |
243 ms |
192764 KB |
Output is correct |
28 |
Correct |
260 ms |
192860 KB |
Output is correct |
29 |
Correct |
786 ms |
204136 KB |
Output is correct |
30 |
Correct |
800 ms |
204280 KB |
Output is correct |
31 |
Correct |
1080 ms |
204664 KB |
Output is correct |
32 |
Correct |
1059 ms |
205176 KB |
Output is correct |
33 |
Correct |
730 ms |
204664 KB |
Output is correct |
34 |
Correct |
785 ms |
204664 KB |
Output is correct |
35 |
Correct |
1097 ms |
204664 KB |
Output is correct |
36 |
Correct |
907 ms |
204024 KB |
Output is correct |
37 |
Correct |
786 ms |
204712 KB |
Output is correct |
38 |
Correct |
687 ms |
204696 KB |
Output is correct |
39 |
Correct |
795 ms |
204552 KB |
Output is correct |
40 |
Correct |
759 ms |
204764 KB |
Output is correct |
41 |
Correct |
628 ms |
204920 KB |
Output is correct |
42 |
Correct |
982 ms |
204920 KB |
Output is correct |
43 |
Correct |
736 ms |
204664 KB |
Output is correct |
44 |
Correct |
643 ms |
204792 KB |
Output is correct |
45 |
Correct |
655 ms |
204536 KB |
Output is correct |
46 |
Correct |
1508 ms |
224188 KB |
Output is correct |
47 |
Execution timed out |
18090 ms |
315384 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |