#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
#define fi first
#define se second
#define all(v) begin(v), end(v)
#define sz(v) (int)(v.size())
using pli = pair<ll, int>;
template<typename T>
inline void chkmin(T& x, T y) {
x = x < y ? x : y;
}
const ll inf = 1e18;
tuple<ll,int,int> shortest_path(vector<pair<ii,ll>>& ls, int anot, int bnot) {
// best path *---a--->--...-->---b---* in ls s.t. a != anot, b != bnot
tuple<ll,int,int> opt{inf, -1, -1};
for(auto [pa, w] : ls) {
auto [s, t] = pa;
if(s != anot and t != bnot) {
opt = min(opt, make_tuple(w, s, t));
}
}
return opt;
}
struct path {
int s, t;
ll w;
};
vector<path> get_top5(vector<pair<ii,ll>>& ls) {
vector<path> ret;
auto [w1, a1, b1] = shortest_path(ls, -1, -1);
auto [w2, a2, b2] = shortest_path(ls, a1, -1);
auto [w3, a3, b3] = shortest_path(ls, a1, b2);
auto [w4, a4, b4] = shortest_path(ls, -1, b1);
auto [w5, a5, b5] = shortest_path(ls, a4, b1);
if(~a1) ret.push_back({a1, b1, w1});
if(~a2) ret.push_back({a2, b2, w2});
if(~a3) ret.push_back({a3, b3, w3});
if(~a4) ret.push_back({a4, b4, w4});
if(~a5) ret.push_back({a5, b5, w5});
return ret;
}
vector<path> merge(vector<path> L, vector<path> R) {
map<ii, ll> opt;
for(path& l : L) {
for(path& r : R) {
if(l.t != r.s) {
if(!opt.count(ii(l.s, r.t))) {
opt[ii(l.s, r.t)] = l.w + r.w;
} else {
chkmin(opt[ii(l.s, r.t)], l.w + r.w);
}
}
}
}
std::vector<path> ret;
vector<pair<ii,ll>> ls(all(opt));
return get_top5(ls);
}
int n, m, T, L;
int A[4004], B[4004], C[4004];
vi ine[2002], oute[2002];
ll cost[4004][4004]; // cost[i][j] = shortest path from i-th directed edge to j-th directed edge
int X[100005];
vector<path> top5[2002][2002];
vector<path> fn[100005];
#ifdef LOCAL
#include "../debug.h"
#else
#define debug(...)
#endif
int main(int argc, char const *argv[])
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
scanf("%d %d %d %d", &n, &m, &T, &L);
for(int i = 1; i <= m; ++i) {
scanf("%d %d %d", &A[2*i], &B[2*i], &C[2*i]);
A[2*i+1] = B[2*i];
B[2*i+1] = A[2*i];
C[2*i+1] = C[2*i];
ine[B[2*i]].emplace_back(2*i);
oute[A[2*i]].emplace_back(2*i);
ine[B[2*i+1]].emplace_back(2*i+1);
oute[A[2*i+1]].emplace_back(2*i+1);
}
for(int i = 1; i <= n; ++i) {
debug(ine[i], oute[i]);
}
for(int i = 1; i <= L; ++i) {
scanf("%d", &X[i]);
}
for(int src = 2; src <= 2*m+1; ++src) {
// for(int src : {4}) {
vector<bool> done(2*m+2);
ll *dis = cost[src];
fill(dis+2, dis+2*m+2, inf);
debug(vector<ll>(dis+2, dis+2*m+2));
dis[src] = C[src];
debug(dis[src]);
priority_queue<pli, vector<pli>, greater<pli>> Q;
Q.emplace(dis[src], src);
while(sz(Q)) {
int u = Q.top().se;
debug(u);
Q.pop();
if(done[u]) continue;
done[u] = 1;
for(int v : oute[B[u]]) if((u^v)>>1) {
debug(v);
ll nw = dis[u] + C[v];
debug(nw);
debug(dis[7]);
if(nw < dis[v]) {
dis[v] = nw;
Q.emplace(dis[v], v);
}
}
}
debug(dis[7]);
}
for(int a = 1; a <= n; ++a) {
for(int b = 1; b <= n; ++b) if(a != b) {
vector<pair<ii,ll>> ls;
for(int s : oute[a])
for(int t : ine[b])
if(cost[s][t] < inf)
ls.push_back({{s>>1, t>>1}, cost[s][t]});
top5[a][b] = get_top5(ls);
}
}
while(T--) {
int p, q;
scanf("%d %d", &p, &q);
X[p] = q;
debug(vi(X+1, X+1+L));
ll ans = inf;
vector<path> cur = top5[X[1]][X[2]];
for(int i = 3; i <= L; ++i) {
cur = merge(cur, top5[X[i-1]][X[i]]);
}
for(path& pa : cur) chkmin(ans, pa.w);
printf("%lld\n", ans == inf ? -1 : ans);
}
return 0;
}
Compilation message
wild_boar.cpp: In function 'int main(int, const char**)':
wild_boar.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | scanf("%d %d %d %d", &n, &m, &T, &L);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:89:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d %d %d", &A[2*i], &B[2*i], &C[2*i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:102:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | scanf("%d", &X[i]);
| ~~~~~^~~~~~~~~~~~~
wild_boar.cpp:145:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
145 | scanf("%d %d", &p, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
96844 KB |
Output is correct |
2 |
Correct |
65 ms |
96928 KB |
Output is correct |
3 |
Correct |
67 ms |
96844 KB |
Output is correct |
4 |
Correct |
67 ms |
96836 KB |
Output is correct |
5 |
Correct |
65 ms |
96832 KB |
Output is correct |
6 |
Correct |
66 ms |
96892 KB |
Output is correct |
7 |
Correct |
68 ms |
96956 KB |
Output is correct |
8 |
Correct |
65 ms |
96848 KB |
Output is correct |
9 |
Correct |
65 ms |
96836 KB |
Output is correct |
10 |
Correct |
64 ms |
96900 KB |
Output is correct |
11 |
Correct |
65 ms |
96908 KB |
Output is correct |
12 |
Correct |
68 ms |
96836 KB |
Output is correct |
13 |
Correct |
64 ms |
96896 KB |
Output is correct |
14 |
Correct |
68 ms |
96956 KB |
Output is correct |
15 |
Correct |
67 ms |
96836 KB |
Output is correct |
16 |
Correct |
65 ms |
96924 KB |
Output is correct |
17 |
Correct |
70 ms |
96952 KB |
Output is correct |
18 |
Correct |
66 ms |
97044 KB |
Output is correct |
19 |
Correct |
66 ms |
96836 KB |
Output is correct |
20 |
Correct |
66 ms |
96952 KB |
Output is correct |
21 |
Correct |
65 ms |
96840 KB |
Output is correct |
22 |
Correct |
65 ms |
96924 KB |
Output is correct |
23 |
Correct |
66 ms |
96948 KB |
Output is correct |
24 |
Correct |
64 ms |
96916 KB |
Output is correct |
25 |
Correct |
64 ms |
96888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
96844 KB |
Output is correct |
2 |
Correct |
65 ms |
96928 KB |
Output is correct |
3 |
Correct |
67 ms |
96844 KB |
Output is correct |
4 |
Correct |
67 ms |
96836 KB |
Output is correct |
5 |
Correct |
65 ms |
96832 KB |
Output is correct |
6 |
Correct |
66 ms |
96892 KB |
Output is correct |
7 |
Correct |
68 ms |
96956 KB |
Output is correct |
8 |
Correct |
65 ms |
96848 KB |
Output is correct |
9 |
Correct |
65 ms |
96836 KB |
Output is correct |
10 |
Correct |
64 ms |
96900 KB |
Output is correct |
11 |
Correct |
65 ms |
96908 KB |
Output is correct |
12 |
Correct |
68 ms |
96836 KB |
Output is correct |
13 |
Correct |
64 ms |
96896 KB |
Output is correct |
14 |
Correct |
68 ms |
96956 KB |
Output is correct |
15 |
Correct |
67 ms |
96836 KB |
Output is correct |
16 |
Correct |
65 ms |
96924 KB |
Output is correct |
17 |
Correct |
70 ms |
96952 KB |
Output is correct |
18 |
Correct |
66 ms |
97044 KB |
Output is correct |
19 |
Correct |
66 ms |
96836 KB |
Output is correct |
20 |
Correct |
66 ms |
96952 KB |
Output is correct |
21 |
Correct |
65 ms |
96840 KB |
Output is correct |
22 |
Correct |
65 ms |
96924 KB |
Output is correct |
23 |
Correct |
66 ms |
96948 KB |
Output is correct |
24 |
Correct |
64 ms |
96916 KB |
Output is correct |
25 |
Correct |
64 ms |
96888 KB |
Output is correct |
26 |
Correct |
65 ms |
97220 KB |
Output is correct |
27 |
Correct |
89 ms |
98696 KB |
Output is correct |
28 |
Correct |
90 ms |
99028 KB |
Output is correct |
29 |
Correct |
268 ms |
104124 KB |
Output is correct |
30 |
Correct |
282 ms |
104116 KB |
Output is correct |
31 |
Correct |
255 ms |
104100 KB |
Output is correct |
32 |
Correct |
271 ms |
104232 KB |
Output is correct |
33 |
Correct |
272 ms |
105892 KB |
Output is correct |
34 |
Correct |
268 ms |
105936 KB |
Output is correct |
35 |
Correct |
255 ms |
105920 KB |
Output is correct |
36 |
Correct |
259 ms |
105756 KB |
Output is correct |
37 |
Correct |
272 ms |
105996 KB |
Output is correct |
38 |
Correct |
260 ms |
108356 KB |
Output is correct |
39 |
Correct |
268 ms |
108324 KB |
Output is correct |
40 |
Correct |
276 ms |
108356 KB |
Output is correct |
41 |
Correct |
264 ms |
108412 KB |
Output is correct |
42 |
Correct |
255 ms |
110052 KB |
Output is correct |
43 |
Correct |
255 ms |
111544 KB |
Output is correct |
44 |
Correct |
257 ms |
111600 KB |
Output is correct |
45 |
Correct |
170 ms |
109792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
96844 KB |
Output is correct |
2 |
Correct |
65 ms |
96928 KB |
Output is correct |
3 |
Correct |
67 ms |
96844 KB |
Output is correct |
4 |
Correct |
67 ms |
96836 KB |
Output is correct |
5 |
Correct |
65 ms |
96832 KB |
Output is correct |
6 |
Correct |
66 ms |
96892 KB |
Output is correct |
7 |
Correct |
68 ms |
96956 KB |
Output is correct |
8 |
Correct |
65 ms |
96848 KB |
Output is correct |
9 |
Correct |
65 ms |
96836 KB |
Output is correct |
10 |
Correct |
64 ms |
96900 KB |
Output is correct |
11 |
Correct |
65 ms |
96908 KB |
Output is correct |
12 |
Correct |
68 ms |
96836 KB |
Output is correct |
13 |
Correct |
64 ms |
96896 KB |
Output is correct |
14 |
Correct |
68 ms |
96956 KB |
Output is correct |
15 |
Correct |
67 ms |
96836 KB |
Output is correct |
16 |
Correct |
65 ms |
96924 KB |
Output is correct |
17 |
Correct |
70 ms |
96952 KB |
Output is correct |
18 |
Correct |
66 ms |
97044 KB |
Output is correct |
19 |
Correct |
66 ms |
96836 KB |
Output is correct |
20 |
Correct |
66 ms |
96952 KB |
Output is correct |
21 |
Correct |
65 ms |
96840 KB |
Output is correct |
22 |
Correct |
65 ms |
96924 KB |
Output is correct |
23 |
Correct |
66 ms |
96948 KB |
Output is correct |
24 |
Correct |
64 ms |
96916 KB |
Output is correct |
25 |
Correct |
64 ms |
96888 KB |
Output is correct |
26 |
Correct |
65 ms |
97220 KB |
Output is correct |
27 |
Correct |
89 ms |
98696 KB |
Output is correct |
28 |
Correct |
90 ms |
99028 KB |
Output is correct |
29 |
Correct |
268 ms |
104124 KB |
Output is correct |
30 |
Correct |
282 ms |
104116 KB |
Output is correct |
31 |
Correct |
255 ms |
104100 KB |
Output is correct |
32 |
Correct |
271 ms |
104232 KB |
Output is correct |
33 |
Correct |
272 ms |
105892 KB |
Output is correct |
34 |
Correct |
268 ms |
105936 KB |
Output is correct |
35 |
Correct |
255 ms |
105920 KB |
Output is correct |
36 |
Correct |
259 ms |
105756 KB |
Output is correct |
37 |
Correct |
272 ms |
105996 KB |
Output is correct |
38 |
Correct |
260 ms |
108356 KB |
Output is correct |
39 |
Correct |
268 ms |
108324 KB |
Output is correct |
40 |
Correct |
276 ms |
108356 KB |
Output is correct |
41 |
Correct |
264 ms |
108412 KB |
Output is correct |
42 |
Correct |
255 ms |
110052 KB |
Output is correct |
43 |
Correct |
255 ms |
111544 KB |
Output is correct |
44 |
Correct |
257 ms |
111600 KB |
Output is correct |
45 |
Correct |
170 ms |
109792 KB |
Output is correct |
46 |
Correct |
273 ms |
114236 KB |
Output is correct |
47 |
Correct |
6291 ms |
258028 KB |
Output is correct |
48 |
Correct |
5636 ms |
305084 KB |
Output is correct |
49 |
Correct |
4853 ms |
363792 KB |
Output is correct |
50 |
Correct |
5095 ms |
363848 KB |
Output is correct |
51 |
Correct |
5565 ms |
363684 KB |
Output is correct |
52 |
Correct |
3820 ms |
363852 KB |
Output is correct |
53 |
Correct |
3785 ms |
363944 KB |
Output is correct |
54 |
Correct |
3806 ms |
364004 KB |
Output is correct |
55 |
Correct |
3781 ms |
363928 KB |
Output is correct |
56 |
Correct |
3815 ms |
393420 KB |
Output is correct |
57 |
Correct |
3873 ms |
426048 KB |
Output is correct |
58 |
Correct |
4010 ms |
461120 KB |
Output is correct |
59 |
Correct |
4043 ms |
499100 KB |
Output is correct |
60 |
Correct |
3998 ms |
540028 KB |
Output is correct |
61 |
Correct |
3966 ms |
583684 KB |
Output is correct |
62 |
Correct |
3898 ms |
630304 KB |
Output is correct |
63 |
Correct |
3770 ms |
679576 KB |
Output is correct |
64 |
Correct |
1725 ms |
536228 KB |
Output is correct |
65 |
Correct |
1766 ms |
536124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
96844 KB |
Output is correct |
2 |
Correct |
65 ms |
96928 KB |
Output is correct |
3 |
Correct |
67 ms |
96844 KB |
Output is correct |
4 |
Correct |
67 ms |
96836 KB |
Output is correct |
5 |
Correct |
65 ms |
96832 KB |
Output is correct |
6 |
Correct |
66 ms |
96892 KB |
Output is correct |
7 |
Correct |
68 ms |
96956 KB |
Output is correct |
8 |
Correct |
65 ms |
96848 KB |
Output is correct |
9 |
Correct |
65 ms |
96836 KB |
Output is correct |
10 |
Correct |
64 ms |
96900 KB |
Output is correct |
11 |
Correct |
65 ms |
96908 KB |
Output is correct |
12 |
Correct |
68 ms |
96836 KB |
Output is correct |
13 |
Correct |
64 ms |
96896 KB |
Output is correct |
14 |
Correct |
68 ms |
96956 KB |
Output is correct |
15 |
Correct |
67 ms |
96836 KB |
Output is correct |
16 |
Correct |
65 ms |
96924 KB |
Output is correct |
17 |
Correct |
70 ms |
96952 KB |
Output is correct |
18 |
Correct |
66 ms |
97044 KB |
Output is correct |
19 |
Correct |
66 ms |
96836 KB |
Output is correct |
20 |
Correct |
66 ms |
96952 KB |
Output is correct |
21 |
Correct |
65 ms |
96840 KB |
Output is correct |
22 |
Correct |
65 ms |
96924 KB |
Output is correct |
23 |
Correct |
66 ms |
96948 KB |
Output is correct |
24 |
Correct |
64 ms |
96916 KB |
Output is correct |
25 |
Correct |
64 ms |
96888 KB |
Output is correct |
26 |
Correct |
65 ms |
97220 KB |
Output is correct |
27 |
Correct |
89 ms |
98696 KB |
Output is correct |
28 |
Correct |
90 ms |
99028 KB |
Output is correct |
29 |
Correct |
268 ms |
104124 KB |
Output is correct |
30 |
Correct |
282 ms |
104116 KB |
Output is correct |
31 |
Correct |
255 ms |
104100 KB |
Output is correct |
32 |
Correct |
271 ms |
104232 KB |
Output is correct |
33 |
Correct |
272 ms |
105892 KB |
Output is correct |
34 |
Correct |
268 ms |
105936 KB |
Output is correct |
35 |
Correct |
255 ms |
105920 KB |
Output is correct |
36 |
Correct |
259 ms |
105756 KB |
Output is correct |
37 |
Correct |
272 ms |
105996 KB |
Output is correct |
38 |
Correct |
260 ms |
108356 KB |
Output is correct |
39 |
Correct |
268 ms |
108324 KB |
Output is correct |
40 |
Correct |
276 ms |
108356 KB |
Output is correct |
41 |
Correct |
264 ms |
108412 KB |
Output is correct |
42 |
Correct |
255 ms |
110052 KB |
Output is correct |
43 |
Correct |
255 ms |
111544 KB |
Output is correct |
44 |
Correct |
257 ms |
111600 KB |
Output is correct |
45 |
Correct |
170 ms |
109792 KB |
Output is correct |
46 |
Correct |
273 ms |
114236 KB |
Output is correct |
47 |
Correct |
6291 ms |
258028 KB |
Output is correct |
48 |
Correct |
5636 ms |
305084 KB |
Output is correct |
49 |
Correct |
4853 ms |
363792 KB |
Output is correct |
50 |
Correct |
5095 ms |
363848 KB |
Output is correct |
51 |
Correct |
5565 ms |
363684 KB |
Output is correct |
52 |
Correct |
3820 ms |
363852 KB |
Output is correct |
53 |
Correct |
3785 ms |
363944 KB |
Output is correct |
54 |
Correct |
3806 ms |
364004 KB |
Output is correct |
55 |
Correct |
3781 ms |
363928 KB |
Output is correct |
56 |
Correct |
3815 ms |
393420 KB |
Output is correct |
57 |
Correct |
3873 ms |
426048 KB |
Output is correct |
58 |
Correct |
4010 ms |
461120 KB |
Output is correct |
59 |
Correct |
4043 ms |
499100 KB |
Output is correct |
60 |
Correct |
3998 ms |
540028 KB |
Output is correct |
61 |
Correct |
3966 ms |
583684 KB |
Output is correct |
62 |
Correct |
3898 ms |
630304 KB |
Output is correct |
63 |
Correct |
3770 ms |
679576 KB |
Output is correct |
64 |
Correct |
1725 ms |
536228 KB |
Output is correct |
65 |
Correct |
1766 ms |
536124 KB |
Output is correct |
66 |
Correct |
12041 ms |
97944 KB |
Output is correct |
67 |
Correct |
1343 ms |
169876 KB |
Output is correct |
68 |
Correct |
1192 ms |
350788 KB |
Output is correct |
69 |
Correct |
2225 ms |
353708 KB |
Output is correct |
70 |
Execution timed out |
18102 ms |
352684 KB |
Time limit exceeded |
71 |
Halted |
0 ms |
0 KB |
- |