Submission #1118780

# Submission time Handle Problem Language Result Execution time Memory
1118780 2024-11-26T06:27:52 Z Cadoc Museum (CEOI17_museum) C++14
100 / 100
432 ms 170572 KB
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define el cout << '\n'
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define N 10005

int n, k, r;
vector<pii> g[N];
vector<int> dp[N][2];

// dp(u, k, ok = 0/1) la tong do dai duong di ngan nhat di qua k dinh trong cay con goc u,
// ok = 0 la khong quay lai u va nguoc lai

inline void combineSubtree(vector<int> &Ans, vector<int> &a, vector<int> &b, int cost){
    while(sz(Ans) < sz(a) + sz(b) - 1) Ans.emplace_back(INF);

    for(int i=0; i<sz(a); ++i){
        for(int j=0; j<sz(b); ++j){
            Ans[i + j] = min(Ans[i + j], a[i] + b[j] + cost);
        }
    }
}

void dfs(int u, int p = -1){
    dp[u][0] = dp[u][1] = {0, 0};
    for(pii T:g[u]){
        int v = T.fi, w = T.se;
        if(v != p){
            dfs(v, u);

            vector<int> f[2];
            for(int i=0; i<2; ++i) f[i] = dp[u][i];

            combineSubtree(dp[u][0], f[0], dp[v][1], 2ll * w);
            combineSubtree(dp[u][0], f[1], dp[v][0], w);
            combineSubtree(dp[u][1], f[1], dp[v][1], 2ll * w);
        }
    }
}

void Solve(){
    cin >> n >> k >> r;
    for(int i=1; i<n; ++i){
        int u, v, w; cin >> u >> v >> w;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }

    dfs(r);
    cout << dp[r][0][k];
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #define NAME "TASK"
    if(fopen(NAME".inp", "r")){
        freopen(NAME".inp", "r", stdin);
        freopen(NAME".out", "w", stdout);
    }

    Solve();
    return 0;
}

Compilation message

museum.cpp: In function 'int main()':
museum.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         freopen(NAME".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
museum.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         freopen(NAME".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1120 KB Output is correct
2 Correct 2 ms 1120 KB Output is correct
3 Correct 1 ms 1120 KB Output is correct
4 Correct 2 ms 1120 KB Output is correct
5 Correct 2 ms 1120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 131 ms 4512 KB Output is correct
2 Correct 160 ms 4696 KB Output is correct
3 Correct 432 ms 164172 KB Output is correct
4 Correct 269 ms 56664 KB Output is correct
5 Correct 155 ms 15180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 131 ms 4512 KB Output is correct
2 Correct 160 ms 4696 KB Output is correct
3 Correct 432 ms 164172 KB Output is correct
4 Correct 269 ms 56664 KB Output is correct
5 Correct 155 ms 15180 KB Output is correct
6 Correct 132 ms 3492 KB Output is correct
7 Correct 301 ms 101728 KB Output is correct
8 Correct 412 ms 2420 KB Output is correct
9 Correct 320 ms 2640 KB Output is correct
10 Correct 177 ms 3756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1120 KB Output is correct
2 Correct 2 ms 1120 KB Output is correct
3 Correct 1 ms 1120 KB Output is correct
4 Correct 2 ms 1120 KB Output is correct
5 Correct 2 ms 1120 KB Output is correct
6 Correct 131 ms 4512 KB Output is correct
7 Correct 160 ms 4696 KB Output is correct
8 Correct 432 ms 164172 KB Output is correct
9 Correct 269 ms 56664 KB Output is correct
10 Correct 155 ms 15180 KB Output is correct
11 Correct 132 ms 3492 KB Output is correct
12 Correct 301 ms 101728 KB Output is correct
13 Correct 412 ms 2420 KB Output is correct
14 Correct 320 ms 2640 KB Output is correct
15 Correct 177 ms 3756 KB Output is correct
16 Correct 162 ms 5208 KB Output is correct
17 Correct 148 ms 4860 KB Output is correct
18 Correct 236 ms 65576 KB Output is correct
19 Correct 319 ms 2476 KB Output is correct
20 Correct 136 ms 3664 KB Output is correct
21 Correct 291 ms 98732 KB Output is correct
22 Correct 135 ms 4628 KB Output is correct
23 Correct 331 ms 2640 KB Output is correct
24 Correct 137 ms 3516 KB Output is correct
25 Correct 410 ms 170572 KB Output is correct