답안 #565508

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
565508 2022-05-21T02:52:58 Z maomao90 Wild Boar (JOI18_wild_boar) C++17
100 / 100
5710 ms 903964 KB
// 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];
int dpfi[MAXL * 4][BAG], dpse[MAXL * 4][BAG];
void upd(int p, int u = 1, int lo = 1, int hi = l) {
    if (hi - lo == 1) {
        REP (i, 0, BAG) {
            dp[u][i] = rd[x[lo]][x[hi]][i];
            dpfi[u][i] = fi[x[lo]][x[hi]][i];
            dpse[u][i] = se[x[lo]][x[hi]][i];
        }
        return;
    }
    int mid = lo + hi >> 1;
    int lc = u << 1, rc = u << 1 ^ 1;
    if (p <= mid) {
        upd(p, lc, lo, mid);
    }
    if (p >= mid) {
        upd(p, rc, mid, hi);
    }
    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];
    }
    REP (i, 0, BAG) {
        dp[u][i] = LINF;
    }
    REP (l, 0, BAG) {
        REP (r, 0, BAG) {
            if ((dpse[lc][l] ^ 1) == dpfi[rc][r]) {
                continue;
            }
            if (mnto(dp[u][0], dp[lc][l] + dp[rc][r])) {
                dpfi[u][0] = dpfi[lc][l];
                dpse[u][0] = dpse[rc][r];
            }
        }
    }
    REP (l, 0, BAG) {
        REP (r, 0, BAG) {
            if ((dpse[lc][l] ^ 1) == dpfi[rc][r]) {
                continue;
            }
            if (dpfi[lc][l] == dpfi[u][0]) {
                continue;
            }
            if (mnto(dp[u][1], dp[lc][l] + dp[rc][r])) {
                dpfi[u][1] = dpfi[lc][l];
                dpse[u][1] = dpse[rc][r];
            }
        }
    }
    REP (l, 0, BAG) {
        REP (r, 0, BAG) {
            if ((dpse[lc][l] ^ 1) == dpfi[rc][r]) {
                continue;
            }
            if (dpfi[lc][l] == dpfi[u][0] || dpse[rc][r] == dpse[u][1]) {
                continue;
            }
            if (mnto(dp[u][2], dp[lc][l] + dp[rc][r])) {
                dpfi[u][2] = dpfi[lc][l];
                dpse[u][2] = dpse[rc][r];
            }
        }
    }
    REP (l, 0, BAG) {
        REP (r, 0, BAG) {
            if ((dpse[lc][l] ^ 1) == dpfi[rc][r]) {
                continue;
            }
            if (dpse[rc][r] == dpse[u][0]) {
                continue;
            }
            if (mnto(dp[u][3], dp[lc][l] + dp[rc][r])) {
                dpfi[u][3] = dpfi[lc][l];
                dpse[u][3] = dpse[rc][r];
            }
        }
    }
    REP (l, 0, BAG) {
        REP (r, 0, BAG) {
            if ((dpse[lc][l] ^ 1) == dpfi[rc][r]) {
                continue;
            }
            if (dpse[rc][r] == dpse[u][0] || dpfi[lc][l] == dpfi[u][3]) {
                continue;
            }
            if (mnto(dp[u][4], dp[lc][l] + dp[rc][r])) {
                dpfi[u][4] = dpfi[lc][l];
                dpse[u][4] = dpse[rc][r];
            }
        }
    }
}

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) {
            mnto(ans, dp[1][i]);
        }
        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:102:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  102 |     int mid = lo + hi >> 1;
      |               ~~~^~~~
wild_boar.cpp:110:9: warning: variable 'a' set but not used [-Wunused-but-set-variable]
  110 |     int a = -1, b = x[mid], c = x[mid + 1], d = -1;
      |         ^
wild_boar.cpp:110:17: warning: unused variable 'b' [-Wunused-variable]
  110 |     int a = -1, b = x[mid], c = x[mid + 1], d = -1;
      |                 ^
wild_boar.cpp:110:29: warning: unused variable 'c' [-Wunused-variable]
  110 |     int a = -1, b = x[mid], c = x[mid + 1], d = -1;
      |                             ^
wild_boar.cpp:110:45: warning: variable 'd' set but not used [-Wunused-but-set-variable]
  110 |     int a = -1, b = x[mid], c = x[mid + 1], d = -1;
      |                                             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 740 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 740 KB Output is correct
21 Correct 1 ms 608 KB Output is correct
22 Correct 1 ms 608 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 740 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 740 KB Output is correct
21 Correct 1 ms 608 KB Output is correct
22 Correct 1 ms 608 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 2 ms 1376 KB Output is correct
27 Correct 381 ms 26308 KB Output is correct
28 Correct 369 ms 26356 KB Output is correct
29 Correct 540 ms 34232 KB Output is correct
30 Correct 504 ms 34188 KB Output is correct
31 Correct 489 ms 34124 KB Output is correct
32 Correct 498 ms 34192 KB Output is correct
33 Correct 553 ms 37240 KB Output is correct
34 Correct 546 ms 37324 KB Output is correct
35 Correct 452 ms 37364 KB Output is correct
36 Correct 483 ms 37300 KB Output is correct
37 Correct 559 ms 37356 KB Output is correct
38 Correct 578 ms 40768 KB Output is correct
39 Correct 515 ms 40484 KB Output is correct
40 Correct 550 ms 40704 KB Output is correct
41 Correct 545 ms 40876 KB Output is correct
42 Correct 459 ms 42876 KB Output is correct
43 Correct 556 ms 44552 KB Output is correct
44 Correct 540 ms 44660 KB Output is correct
45 Correct 450 ms 48976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 740 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 740 KB Output is correct
21 Correct 1 ms 608 KB Output is correct
22 Correct 1 ms 608 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 2 ms 1376 KB Output is correct
27 Correct 381 ms 26308 KB Output is correct
28 Correct 369 ms 26356 KB Output is correct
29 Correct 540 ms 34232 KB Output is correct
30 Correct 504 ms 34188 KB Output is correct
31 Correct 489 ms 34124 KB Output is correct
32 Correct 498 ms 34192 KB Output is correct
33 Correct 553 ms 37240 KB Output is correct
34 Correct 546 ms 37324 KB Output is correct
35 Correct 452 ms 37364 KB Output is correct
36 Correct 483 ms 37300 KB Output is correct
37 Correct 559 ms 37356 KB Output is correct
38 Correct 578 ms 40768 KB Output is correct
39 Correct 515 ms 40484 KB Output is correct
40 Correct 550 ms 40704 KB Output is correct
41 Correct 545 ms 40876 KB Output is correct
42 Correct 459 ms 42876 KB Output is correct
43 Correct 556 ms 44552 KB Output is correct
44 Correct 540 ms 44660 KB Output is correct
45 Correct 450 ms 48976 KB Output is correct
46 Correct 247 ms 33720 KB Output is correct
47 Correct 3442 ms 383136 KB Output is correct
48 Correct 3840 ms 462012 KB Output is correct
49 Correct 4248 ms 523996 KB Output is correct
50 Correct 4179 ms 524136 KB Output is correct
51 Correct 4177 ms 524140 KB Output is correct
52 Correct 4540 ms 524356 KB Output is correct
53 Correct 4511 ms 524248 KB Output is correct
54 Correct 4512 ms 524316 KB Output is correct
55 Correct 4491 ms 524384 KB Output is correct
56 Correct 4646 ms 558400 KB Output is correct
57 Correct 4831 ms 593840 KB Output is correct
58 Correct 4874 ms 631364 KB Output is correct
59 Correct 4936 ms 670952 KB Output is correct
60 Correct 4950 ms 712376 KB Output is correct
61 Correct 4985 ms 755584 KB Output is correct
62 Correct 4918 ms 800480 KB Output is correct
63 Correct 4851 ms 843668 KB Output is correct
64 Correct 1889 ms 901100 KB Output is correct
65 Correct 1839 ms 901188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 740 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 740 KB Output is correct
21 Correct 1 ms 608 KB Output is correct
22 Correct 1 ms 608 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 2 ms 1376 KB Output is correct
27 Correct 381 ms 26308 KB Output is correct
28 Correct 369 ms 26356 KB Output is correct
29 Correct 540 ms 34232 KB Output is correct
30 Correct 504 ms 34188 KB Output is correct
31 Correct 489 ms 34124 KB Output is correct
32 Correct 498 ms 34192 KB Output is correct
33 Correct 553 ms 37240 KB Output is correct
34 Correct 546 ms 37324 KB Output is correct
35 Correct 452 ms 37364 KB Output is correct
36 Correct 483 ms 37300 KB Output is correct
37 Correct 559 ms 37356 KB Output is correct
38 Correct 578 ms 40768 KB Output is correct
39 Correct 515 ms 40484 KB Output is correct
40 Correct 550 ms 40704 KB Output is correct
41 Correct 545 ms 40876 KB Output is correct
42 Correct 459 ms 42876 KB Output is correct
43 Correct 556 ms 44552 KB Output is correct
44 Correct 540 ms 44660 KB Output is correct
45 Correct 450 ms 48976 KB Output is correct
46 Correct 247 ms 33720 KB Output is correct
47 Correct 3442 ms 383136 KB Output is correct
48 Correct 3840 ms 462012 KB Output is correct
49 Correct 4248 ms 523996 KB Output is correct
50 Correct 4179 ms 524136 KB Output is correct
51 Correct 4177 ms 524140 KB Output is correct
52 Correct 4540 ms 524356 KB Output is correct
53 Correct 4511 ms 524248 KB Output is correct
54 Correct 4512 ms 524316 KB Output is correct
55 Correct 4491 ms 524384 KB Output is correct
56 Correct 4646 ms 558400 KB Output is correct
57 Correct 4831 ms 593840 KB Output is correct
58 Correct 4874 ms 631364 KB Output is correct
59 Correct 4936 ms 670952 KB Output is correct
60 Correct 4950 ms 712376 KB Output is correct
61 Correct 4985 ms 755584 KB Output is correct
62 Correct 4918 ms 800480 KB Output is correct
63 Correct 4851 ms 843668 KB Output is correct
64 Correct 1889 ms 901100 KB Output is correct
65 Correct 1839 ms 901188 KB Output is correct
66 Correct 486 ms 22448 KB Output is correct
67 Correct 625 ms 245196 KB Output is correct
68 Correct 1680 ms 879928 KB Output is correct
69 Correct 1916 ms 881052 KB Output is correct
70 Correct 2550 ms 902588 KB Output is correct
71 Correct 4053 ms 385280 KB Output is correct
72 Correct 4341 ms 464152 KB Output is correct
73 Correct 5119 ms 526848 KB Output is correct
74 Correct 5210 ms 526800 KB Output is correct
75 Correct 5284 ms 527024 KB Output is correct
76 Correct 4864 ms 526456 KB Output is correct
77 Correct 4683 ms 526332 KB Output is correct
78 Correct 4549 ms 526348 KB Output is correct
79 Correct 5417 ms 596500 KB Output is correct
80 Correct 5638 ms 634044 KB Output is correct
81 Correct 5356 ms 672960 KB Output is correct
82 Correct 5710 ms 714928 KB Output is correct
83 Correct 5298 ms 757528 KB Output is correct
84 Correct 5453 ms 846200 KB Output is correct
85 Correct 2361 ms 903964 KB Output is correct