Submission #978215

# Submission time Handle Problem Language Result Execution time Memory
978215 2024-05-09T03:47:52 Z model_code Board Game (JOI24_boardgame) C++17
100 / 100
529 ms 52772 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

template <class T>
using vc = vector<T>;

#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload3(__VA_ARGS__, FOR3, FOR2, FOR1)(__VA_ARGS__)

#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if

template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}

template <class T, class S>
inline bool chmax(T &a, const S &b) {
  return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
  return (a > b ? a = b, 1 : 0);
}

// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
  vector<int> ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
  vc<T> B(len(I));
  FOR(i, len(I)) B[i] = A[I[i]];
  return B;
}

constexpr ll INF = 1'000'000'000'000'000'000;

// 点集合 V からの距離
// 距離が x1 または x2 であり, f(u,v) で与える.
template <typename F>
vc<ll> bfs(int N, vc<vc<int>> &G, vc<int> &V, F f) {
  assert(!V.empty());
  vc<ll> dist(N, INF);
  deque<int> que;
  for (auto &v: V) { dist[v] = 0, que.push_back(v); }
  while (len(que)) {
    int v = POP(que);
    for (int w: G[v]) {
      ll x = f(v, w);
      assert(x == 0 || x == 1);
      if (chmin(dist[w], dist[v] + x)) {
        if (x == 0)
          que.push_front(w);
        else
          que.push_back(w);
      }
    }
  }
  return dist;
}

void out(vc<ll> ANS) {
  for (ll x: ANS) cout << x << "\n";
}

void solve() {
  // 入力を読み込む
  int N, M, K;
  cin >> N >> M >> K;
  vc<vc<int>> G(N);
  FOR(M) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    G[u].emplace_back(v), G[v].emplace_back(u);
  }
  string S;
  cin >> S;
  vc<int> X;
  FOR(K) {
    int x;
    cin >> x;
    X.emplace_back(--x);
  }

  // 駒 0 の初期位置:X0
  // この実装では「他の駒」の個数を K としている
  int X0 = X[0];
  X.erase(X.begin());
  --K;

  // 入力ここまで
  // åœæ­¢ãƒžã‚¹ãŒå­˜åœ¨ã—ãªã„å ´åˆ
  if (count(all(S), '1') == 0) {
    vc<int> V = {X0};
    vc<ll> ANS = bfs(N, G, V, [&](int u, int v) -> int { return 1; });
    return out(ANS);
  }

  // 駒 0 が目的達成までに他の駒がターンを消化する回数 n
  // n>=1 のときに 2n+a が利用できるような a の最小値
  vc<ll> A = [&]() -> vc<ll> {
    vc<int> V;
    FOR(v, N) {
      if (S[v] != '1') continue;
      for (int w: G[v]) { V.emplace_back(w); }
    }
    vc<ll> dist = bfs(N, G, V, [&](int u, int v) -> int { return 1; });
    vc<ll> A(K);
    FOR(k, K) {
      ll x = dist[X[k]];
      A[k] = x - 1;
    }
    return A;
  }();

  // n>=1 のときに n+b が利用できるような b の最小値
  vc<ll> B = [&]() -> vc<ll> {
    vc<int> V;
    FOR(v, N) {
      if (S[v] != '1') continue;
      for (int w: G[v]) {
        if (S[w] == '1') {
          V.emplace_back(v);
          break;
        }
      }
    }
    if (V.empty()) return vc<ll>(K, INF);
    vc<ll> dist = bfs(
        N, G, V, [&](int u, int v) -> int { return (S[u] == '1' ? 0 : 1); });
    vc<ll> B(K);
    FOR(k, K) { B[k] = dist[X[k]]; }
    return B;
  }();

  // 他の駒すべてを n 回ずつターン消費するためにかかるコスト
  vc<ll> COST(N);
  {
    vc<ll> C0(N + 1), C1(N + 1);
    FOR(k, K) {
      ll a = A[k], b = B[k];
      assert(a <= b);
      ll n = b - a;
      chmin(n, N);
      // FOR(x, n) COST[x] += 2 * x + a;
      // FOR(x, n, N) COST[x] += x + b;
      // c1x +c0
      C1[0] += 2, C1[n] -= 2, C0[0] += a, C0[n] -= a;
      C1[n] += 1, C1[N] -= 1, C0[n] += b, C0[N] -= b;
    }
    FOR(n, N) C1[n + 1] += C1[n], C0[n + 1] += C0[n];
    FOR(x, 1, N) COST[x] = C1[x] * x + C0[x];
  }

  auto solution_1 = [&]() -> vc<ll> {
    // K が大きい
    // 再行動回数を枝狩りする
    vc<ll> ANS(N, INF);
    vc<int> V = {X0};

    // min_cnt: v に到達するまでに他のプレイヤがターン消化する最小回数
    vc<ll> min_cnt = bfs(N, G, V, [&](int u, int v) -> int {
      return (S[u] == '1' && u != X0 ? 1 : 0);
    });
    ll LIM = N / K + 1;
    chmin(LIM, count(all(S), '1'));

    // dp[i][v]: v に到達するまでに他のプレイヤが
    // min_cnt[v] + i 回ずつターン消化
    vc<vc<int>> dp(LIM, vc<int>(N, 1e9));
    vc<int> min_cnt_now(N, N);
    deque<pair<int, int>> que;
    dp[0][X0] = 0, que.emplace_back(0, X0);
    while (len(que)) {
      auto [k, v] = POP(que);
      for (int w: G[v]) {
        int k1 = k + (S[v] == '1' && v != X0 ? 1 : 0);
        int x = dp[k - min_cnt[v]][v];
        // 最適になりえないパターンを枝狩りする
        if (!chmin(min_cnt_now[w], k1)) continue;
        if (k1 - min_cnt[w] >= LIM) continue;
        if (N <= x + 1 + ll(K) * (k1 - min_cnt[w])) continue;
        if (chmin(dp[k1 - min_cnt[w]][w], x + 1)) { que.emplace_back(k1, w); }
      }
    }
    FOR(k, LIM) {
      FOR(v, N) {
        ll x = dp[k][v];
        ll n = min_cnt[v] + k;
        if (n < N) chmin(ANS[v], x + COST[n]);
      }
    }
    return ANS;
  };

  auto solution_2 = [&]() -> vc<ll> {
    // 直線の種類ごとに解く
    vc<ll> ANS(N, INF);
    vc<vc<ll>> dp(2, vc<ll>(N, INF));
    vc<pair<int, int>> que1(N + N), que2(N + N);
    auto solve_ab = [&](ll a, ll b) -> void {
      // 他のプレイヤが n 回ターン消化するときに an+b のコストがかかるとして解く
      // v!=X0 かつ S[v]=='1' から出るときにコスト発生
      // dp[v] := プレイヤ1の回数 + an
      fill(all(dp[0]), INF), fill(all(dp[1]), INF);
      int l1 = 0, r1 = 0, l2 = 0, r2 = 0;
      dp[0][X0] = 0, que1[r1++] = {0, X0};
      while (l1 < r1 || l2 < r2) {
        auto [t, v] = [&]() -> pair<int, int> {
          if (l1 == r1) return que2[l2++];
          if (l2 == r2) return que1[l1++];
          auto [t1, v1] = que1[l1];
          auto [t2, v2] = que2[l2];
          return (dp[t1][v1] < dp[t2][v2] ? que1[l1++] : que2[l2++]);
        }();
        for (auto w: G[v]) {
          if (v == X0 || S[v] == '0') {
            if (chmin(dp[t][w], dp[t][v] + 1)) que1[r1++] = {t, w};
          } else {
            if (chmin(dp[1][w], dp[t][v] + a + 1)) que2[r2++] = {1, w};
          }
        }
      }
      FOR(v, N) {
        chmin(ANS[v], dp[0][v]);
        chmin(ANS[v], dp[1][v] + b);
      }
    };

    int L = 1;
    while (L < N) {
      // COST[L], ..., COST[R-1] 部分をまとめて計算する
      int R = L + 2;
      while (R < N && 2 * COST[R - 1] == COST[R - 2] + COST[R]) { ++R; }
      chmin(R, N);
      if (R == L + 1) {
        solve_ab(0, COST[L]);
      } else {
        ll a = COST[L + 1] - COST[L];
        ll b = COST[L] - a * L;
        solve_ab(a, b);
      }
      L = R;
    }
    return ANS;
  };

  vc<ll> ANS = (K >= 200 ? solution_1() : solution_2());
  out(ANS);
}

signed main() {
  solve();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 604 KB Output is correct
2 Correct 26 ms 3296 KB Output is correct
3 Correct 44 ms 5484 KB Output is correct
4 Correct 33 ms 4692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 600 KB Output is correct
2 Correct 33 ms 4008 KB Output is correct
3 Correct 40 ms 7224 KB Output is correct
4 Correct 37 ms 7224 KB Output is correct
5 Correct 49 ms 6368 KB Output is correct
6 Correct 37 ms 7380 KB Output is correct
7 Correct 36 ms 5932 KB Output is correct
8 Correct 37 ms 6988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 600 KB Output is correct
2 Correct 29 ms 4124 KB Output is correct
3 Correct 38 ms 5564 KB Output is correct
4 Correct 41 ms 6204 KB Output is correct
5 Correct 47 ms 6332 KB Output is correct
6 Correct 37 ms 7244 KB Output is correct
7 Correct 37 ms 7228 KB Output is correct
8 Correct 49 ms 6324 KB Output is correct
9 Correct 26 ms 2600 KB Output is correct
10 Correct 25 ms 2624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 3 ms 704 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 3 ms 860 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 580 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 3 ms 856 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 600 KB Output is correct
14 Correct 2 ms 824 KB Output is correct
15 Correct 7 ms 820 KB Output is correct
16 Correct 11 ms 844 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 2 ms 704 KB Output is correct
19 Correct 3 ms 820 KB Output is correct
20 Correct 3 ms 824 KB Output is correct
21 Correct 3 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 7232 KB Output is correct
2 Correct 40 ms 7232 KB Output is correct
3 Correct 36 ms 7228 KB Output is correct
4 Correct 39 ms 7240 KB Output is correct
5 Correct 45 ms 7404 KB Output is correct
6 Correct 34 ms 5920 KB Output is correct
7 Correct 26 ms 2664 KB Output is correct
8 Correct 20 ms 3632 KB Output is correct
9 Correct 21 ms 3648 KB Output is correct
10 Correct 34 ms 5712 KB Output is correct
11 Correct 34 ms 5712 KB Output is correct
12 Correct 38 ms 7480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 7232 KB Output is correct
2 Correct 40 ms 7232 KB Output is correct
3 Correct 36 ms 7228 KB Output is correct
4 Correct 39 ms 7240 KB Output is correct
5 Correct 45 ms 7404 KB Output is correct
6 Correct 34 ms 5920 KB Output is correct
7 Correct 26 ms 2664 KB Output is correct
8 Correct 20 ms 3632 KB Output is correct
9 Correct 21 ms 3648 KB Output is correct
10 Correct 34 ms 5712 KB Output is correct
11 Correct 34 ms 5712 KB Output is correct
12 Correct 38 ms 7480 KB Output is correct
13 Correct 46 ms 7436 KB Output is correct
14 Correct 41 ms 7452 KB Output is correct
15 Correct 65 ms 7344 KB Output is correct
16 Correct 68 ms 7428 KB Output is correct
17 Correct 36 ms 6176 KB Output is correct
18 Correct 53 ms 6188 KB Output is correct
19 Correct 50 ms 5556 KB Output is correct
20 Correct 43 ms 7168 KB Output is correct
21 Correct 20 ms 3668 KB Output is correct
22 Correct 22 ms 4388 KB Output is correct
23 Correct 21 ms 4388 KB Output is correct
24 Correct 39 ms 6404 KB Output is correct
25 Correct 36 ms 7196 KB Output is correct
26 Correct 36 ms 7240 KB Output is correct
27 Correct 26 ms 4376 KB Output is correct
28 Correct 100 ms 4380 KB Output is correct
29 Correct 156 ms 4604 KB Output is correct
30 Correct 41 ms 7224 KB Output is correct
31 Correct 154 ms 7412 KB Output is correct
32 Correct 287 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 3 ms 704 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 3 ms 860 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 580 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 3 ms 856 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 600 KB Output is correct
14 Correct 2 ms 824 KB Output is correct
15 Correct 7 ms 820 KB Output is correct
16 Correct 11 ms 844 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 2 ms 704 KB Output is correct
19 Correct 3 ms 820 KB Output is correct
20 Correct 3 ms 824 KB Output is correct
21 Correct 3 ms 600 KB Output is correct
22 Correct 31 ms 4404 KB Output is correct
23 Correct 28 ms 11060 KB Output is correct
24 Correct 24 ms 4532 KB Output is correct
25 Correct 34 ms 19740 KB Output is correct
26 Correct 30 ms 12476 KB Output is correct
27 Correct 27 ms 3860 KB Output is correct
28 Correct 30 ms 10652 KB Output is correct
29 Correct 28 ms 4116 KB Output is correct
30 Correct 26 ms 4372 KB Output is correct
31 Correct 30 ms 4036 KB Output is correct
32 Correct 21 ms 2816 KB Output is correct
33 Correct 21 ms 3268 KB Output is correct
34 Correct 24 ms 4384 KB Output is correct
35 Correct 21 ms 4344 KB Output is correct
36 Correct 62 ms 14772 KB Output is correct
37 Correct 46 ms 11800 KB Output is correct
38 Correct 41 ms 8032 KB Output is correct
39 Correct 240 ms 4604 KB Output is correct
40 Correct 319 ms 4608 KB Output is correct
41 Correct 31 ms 14956 KB Output is correct
42 Correct 29 ms 10256 KB Output is correct
43 Correct 37 ms 8468 KB Output is correct
44 Correct 25 ms 6932 KB Output is correct
45 Correct 25 ms 6160 KB Output is correct
46 Correct 25 ms 5660 KB Output is correct
47 Correct 25 ms 5148 KB Output is correct
48 Correct 26 ms 4636 KB Output is correct
49 Correct 25 ms 4132 KB Output is correct
50 Correct 29 ms 4104 KB Output is correct
51 Correct 23 ms 4372 KB Output is correct
52 Correct 28 ms 4588 KB Output is correct
53 Correct 36 ms 4468 KB Output is correct
54 Correct 37 ms 4304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 604 KB Output is correct
2 Correct 26 ms 3296 KB Output is correct
3 Correct 44 ms 5484 KB Output is correct
4 Correct 33 ms 4692 KB Output is correct
5 Correct 3 ms 600 KB Output is correct
6 Correct 33 ms 4008 KB Output is correct
7 Correct 40 ms 7224 KB Output is correct
8 Correct 37 ms 7224 KB Output is correct
9 Correct 49 ms 6368 KB Output is correct
10 Correct 37 ms 7380 KB Output is correct
11 Correct 36 ms 5932 KB Output is correct
12 Correct 37 ms 6988 KB Output is correct
13 Correct 3 ms 600 KB Output is correct
14 Correct 29 ms 4124 KB Output is correct
15 Correct 38 ms 5564 KB Output is correct
16 Correct 41 ms 6204 KB Output is correct
17 Correct 47 ms 6332 KB Output is correct
18 Correct 37 ms 7244 KB Output is correct
19 Correct 37 ms 7228 KB Output is correct
20 Correct 49 ms 6324 KB Output is correct
21 Correct 26 ms 2600 KB Output is correct
22 Correct 25 ms 2624 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 3 ms 704 KB Output is correct
25 Correct 2 ms 604 KB Output is correct
26 Correct 3 ms 860 KB Output is correct
27 Correct 2 ms 604 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 2 ms 580 KB Output is correct
30 Correct 4 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 3 ms 856 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 600 KB Output is correct
36 Correct 2 ms 824 KB Output is correct
37 Correct 7 ms 820 KB Output is correct
38 Correct 11 ms 844 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 2 ms 704 KB Output is correct
41 Correct 3 ms 820 KB Output is correct
42 Correct 3 ms 824 KB Output is correct
43 Correct 3 ms 600 KB Output is correct
44 Correct 37 ms 7232 KB Output is correct
45 Correct 40 ms 7232 KB Output is correct
46 Correct 36 ms 7228 KB Output is correct
47 Correct 39 ms 7240 KB Output is correct
48 Correct 45 ms 7404 KB Output is correct
49 Correct 34 ms 5920 KB Output is correct
50 Correct 26 ms 2664 KB Output is correct
51 Correct 20 ms 3632 KB Output is correct
52 Correct 21 ms 3648 KB Output is correct
53 Correct 34 ms 5712 KB Output is correct
54 Correct 34 ms 5712 KB Output is correct
55 Correct 38 ms 7480 KB Output is correct
56 Correct 46 ms 7436 KB Output is correct
57 Correct 41 ms 7452 KB Output is correct
58 Correct 65 ms 7344 KB Output is correct
59 Correct 68 ms 7428 KB Output is correct
60 Correct 36 ms 6176 KB Output is correct
61 Correct 53 ms 6188 KB Output is correct
62 Correct 50 ms 5556 KB Output is correct
63 Correct 43 ms 7168 KB Output is correct
64 Correct 20 ms 3668 KB Output is correct
65 Correct 22 ms 4388 KB Output is correct
66 Correct 21 ms 4388 KB Output is correct
67 Correct 39 ms 6404 KB Output is correct
68 Correct 36 ms 7196 KB Output is correct
69 Correct 36 ms 7240 KB Output is correct
70 Correct 26 ms 4376 KB Output is correct
71 Correct 100 ms 4380 KB Output is correct
72 Correct 156 ms 4604 KB Output is correct
73 Correct 41 ms 7224 KB Output is correct
74 Correct 154 ms 7412 KB Output is correct
75 Correct 287 ms 7416 KB Output is correct
76 Correct 31 ms 4404 KB Output is correct
77 Correct 28 ms 11060 KB Output is correct
78 Correct 24 ms 4532 KB Output is correct
79 Correct 34 ms 19740 KB Output is correct
80 Correct 30 ms 12476 KB Output is correct
81 Correct 27 ms 3860 KB Output is correct
82 Correct 30 ms 10652 KB Output is correct
83 Correct 28 ms 4116 KB Output is correct
84 Correct 26 ms 4372 KB Output is correct
85 Correct 30 ms 4036 KB Output is correct
86 Correct 21 ms 2816 KB Output is correct
87 Correct 21 ms 3268 KB Output is correct
88 Correct 24 ms 4384 KB Output is correct
89 Correct 21 ms 4344 KB Output is correct
90 Correct 62 ms 14772 KB Output is correct
91 Correct 46 ms 11800 KB Output is correct
92 Correct 41 ms 8032 KB Output is correct
93 Correct 240 ms 4604 KB Output is correct
94 Correct 319 ms 4608 KB Output is correct
95 Correct 31 ms 14956 KB Output is correct
96 Correct 29 ms 10256 KB Output is correct
97 Correct 37 ms 8468 KB Output is correct
98 Correct 25 ms 6932 KB Output is correct
99 Correct 25 ms 6160 KB Output is correct
100 Correct 25 ms 5660 KB Output is correct
101 Correct 25 ms 5148 KB Output is correct
102 Correct 26 ms 4636 KB Output is correct
103 Correct 25 ms 4132 KB Output is correct
104 Correct 29 ms 4104 KB Output is correct
105 Correct 23 ms 4372 KB Output is correct
106 Correct 28 ms 4588 KB Output is correct
107 Correct 36 ms 4468 KB Output is correct
108 Correct 37 ms 4304 KB Output is correct
109 Correct 52 ms 11496 KB Output is correct
110 Correct 61 ms 37668 KB Output is correct
111 Correct 37 ms 7240 KB Output is correct
112 Correct 214 ms 7224 KB Output is correct
113 Correct 72 ms 34112 KB Output is correct
114 Correct 63 ms 39672 KB Output is correct
115 Correct 37 ms 7228 KB Output is correct
116 Correct 76 ms 52772 KB Output is correct
117 Correct 56 ms 23408 KB Output is correct
118 Correct 30 ms 2952 KB Output is correct
119 Correct 26 ms 3332 KB Output is correct
120 Correct 38 ms 8536 KB Output is correct
121 Correct 51 ms 6752 KB Output is correct
122 Correct 46 ms 7328 KB Output is correct
123 Correct 117 ms 29508 KB Output is correct
124 Correct 95 ms 24644 KB Output is correct
125 Correct 105 ms 19268 KB Output is correct
126 Correct 397 ms 7416 KB Output is correct
127 Correct 529 ms 7416 KB Output is correct
128 Correct 66 ms 37932 KB Output is correct
129 Correct 53 ms 24972 KB Output is correct
130 Correct 49 ms 19264 KB Output is correct
131 Correct 45 ms 15204 KB Output is correct
132 Correct 50 ms 15160 KB Output is correct
133 Correct 45 ms 15224 KB Output is correct
134 Correct 43 ms 11712 KB Output is correct
135 Correct 42 ms 10296 KB Output is correct
136 Correct 42 ms 8504 KB Output is correct
137 Correct 41 ms 7748 KB Output is correct
138 Correct 41 ms 6728 KB Output is correct
139 Correct 46 ms 6608 KB Output is correct
140 Correct 41 ms 7252 KB Output is correct
141 Correct 48 ms 7424 KB Output is correct
142 Correct 52 ms 6716 KB Output is correct
143 Correct 52 ms 6824 KB Output is correct