Submission #759277

# Submission time Handle Problem Language Result Execution time Memory
759277 2023-06-16T03:01:30 Z hazzle Museum (CEOI17_museum) C++17
100 / 100
248 ms 182156 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("avx2")

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define all(m) (m).begin(), (m).end()
#define rall(m) (m).rbegin(), (m).rend()
#define vec vector
#define sz(a) (int) (a).size()
#define mpp make_pair
#define mtt make_tuple

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef tuple <int, int, int> tui;

template <typename T>
using prq = priority_queue <T>;

template <typename T>
using pgq = priority_queue <T, vec <T>, greater <T>>;

template <typename T> bool umin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> bool umax(T &a, T b) { return a < b ? a = b, 1 : 0; }

const int inf = 1e9 + 42;

inline int solve(){
      int n, k, x;
      cin >> n >> k >> x, --x;
      vec <vec <pii>> g(n);
      for (int i = 0; i < n - 1; ++i){
            int u, v, w;
            cin >> u >> v >> w, --u, --v;
            g[u].push_back({v, w});
            g[v].push_back({u, w});
      }
      vec <vec <array <int, 2>>> dp(n);
      auto mrg = [&](vec <array <int, 2>> a, vec <array <int, 2>> b, int len){
            vec <array <int, 2>> c(sz(a) + sz(b) - 1, {inf, inf});
            for (int i = 0; i < sz(a); ++i){
                  umin(c[i][0], a[i][0]);
                  umin(c[i][1], a[i][1]);
                  for (int j = 0; j < sz(b); ++j){
                        umin(c[i + j][0], a[i][0] + b[j][0] + 2 * len);
                        umin(c[i + j][1], min(a[i][1] + b[j][0] + 2 * len, a[i][0] + b[j][1] + len));
                  }
            }
            return c;
      };
      auto dfs = [&](auto &&dfs, int u, int p) -> void{
            dp[u] = {{0, 0}, {0, 0}};
            for (auto &[v, w]: g[u]) if (v != p){
                  dfs(dfs, v, u);
                  dp[u] = mrg(dp[u], dp[v], w);
            }
      }; dfs(dfs, x, x);
      cout << dp[x][k][1] << "\n";
      return 0;
}

inline void precalc(){}

signed main(){
      ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
      int tst = 1; //cin >> tst;
      precalc();
      while(tst--) solve();
      return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 3796 KB Output is correct
2 Correct 81 ms 4196 KB Output is correct
3 Correct 196 ms 182156 KB Output is correct
4 Correct 114 ms 56128 KB Output is correct
5 Correct 90 ms 13388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 3796 KB Output is correct
2 Correct 81 ms 4196 KB Output is correct
3 Correct 196 ms 182156 KB Output is correct
4 Correct 114 ms 56128 KB Output is correct
5 Correct 90 ms 13388 KB Output is correct
6 Correct 79 ms 2996 KB Output is correct
7 Correct 148 ms 106628 KB Output is correct
8 Correct 248 ms 2136 KB Output is correct
9 Correct 180 ms 2128 KB Output is correct
10 Correct 95 ms 3096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 80 ms 3796 KB Output is correct
7 Correct 81 ms 4196 KB Output is correct
8 Correct 196 ms 182156 KB Output is correct
9 Correct 114 ms 56128 KB Output is correct
10 Correct 90 ms 13388 KB Output is correct
11 Correct 79 ms 2996 KB Output is correct
12 Correct 148 ms 106628 KB Output is correct
13 Correct 248 ms 2136 KB Output is correct
14 Correct 180 ms 2128 KB Output is correct
15 Correct 95 ms 3096 KB Output is correct
16 Correct 81 ms 4444 KB Output is correct
17 Correct 88 ms 4424 KB Output is correct
18 Correct 122 ms 68104 KB Output is correct
19 Correct 195 ms 2088 KB Output is correct
20 Correct 95 ms 3372 KB Output is correct
21 Correct 158 ms 98516 KB Output is correct
22 Correct 80 ms 4076 KB Output is correct
23 Correct 195 ms 2080 KB Output is correct
24 Correct 81 ms 3224 KB Output is correct
25 Correct 193 ms 176504 KB Output is correct