Submission #780507

# Submission time Handle Problem Language Result Execution time Memory
780507 2023-07-12T09:40:32 Z thefless Museum (CEOI17_museum) C++17
100 / 100
306 ms 278616 KB
#include<bits/stdc++.h>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define Times (1.0 * clock() / CLOCKS_PER_SEC)
#define fi first
#define base 31
#define sz(x) (int)(x).size()
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()

#define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); freopen(name".bug", "w", stderr); }
#define task "name" 
#define tsk "" 

const int oo = 1e9 + 7;
const ll loo = (ll)1e18 + 7;
const int MOD = 1e9 + 7;
const int MOD2 = 1e9 + 696969;
const int N = 1e4 + 3;
const int BASE = 10;

template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}

int n , k , x;
vector<ll> dp[N][2];
vector<pii> adj[N];
int fin[N];
void cal_fin(int u , int par){
    fin[u] = 1;
    for(auto x : adj[u]){
        int v = x.se;
        if(v == par) continue;
        cal_fin(v , u);
        fin[u] += fin[v];
    }
}
 void dfs(int u , int par){
    dp[u][0].assign(min(k , fin[u]) + 2 , loo);
    dp[u][1].assign(min(k , fin[u]) + 2 , loo);
    dp[u][0][0] = dp[u][0][1] = dp[u][1][0] = dp[u][1][1] = 0;
    int total = 1;
    for(auto x : adj[u]){
        int v = x.se;
        int w = x.fi;
        if(v == par) continue;
        dfs(v , u);
        total += fin[v];
        for(int i = total ; i >= 2 ; i--){
            if(i > k) continue;
            for(int j = max(0 , i - total + fin[v]) ; j <= min(i , fin[v]) ; j++){
                minimize(dp[u][0][i] , dp[u][0][i - j] + dp[v][1][j] + 2 * w);
                minimize(dp[u][0][i] , dp[u][1][i - j] + dp[v][0][j] + w);
                minimize(dp[u][1][i] , dp[u][1][i - j] + dp[v][1][j] + 2 * w);
            }
        }
    }
 } 
void Solve(){
    cin >> n >> k >> x;
    for(int i = 1 ; i < n ; i++){
        int u , v , w;
        cin >> u >> v >> w;
        adj[u].pb({w , v});
        adj[v].pb({w , u});
    }
    cal_fin(x , -1);
    dfs(x , -1);
    cout << dp[x][0][k];
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int T = 1;
    // cin >> T;
    while(T--)
    {
        Solve();
    }
    cerr << "Time elapsed: " << Times << " s.\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3096 KB Output is correct
2 Correct 10 ms 3176 KB Output is correct
3 Correct 26 ms 8148 KB Output is correct
4 Correct 15 ms 5844 KB Output is correct
5 Correct 12 ms 4820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3096 KB Output is correct
2 Correct 10 ms 3176 KB Output is correct
3 Correct 26 ms 8148 KB Output is correct
4 Correct 15 ms 5844 KB Output is correct
5 Correct 12 ms 4820 KB Output is correct
6 Correct 10 ms 2664 KB Output is correct
7 Correct 25 ms 7684 KB Output is correct
8 Correct 53 ms 2056 KB Output is correct
9 Correct 38 ms 1996 KB Output is correct
10 Correct 18 ms 2228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 10 ms 3096 KB Output is correct
7 Correct 10 ms 3176 KB Output is correct
8 Correct 26 ms 8148 KB Output is correct
9 Correct 15 ms 5844 KB Output is correct
10 Correct 12 ms 4820 KB Output is correct
11 Correct 10 ms 2664 KB Output is correct
12 Correct 25 ms 7684 KB Output is correct
13 Correct 53 ms 2056 KB Output is correct
14 Correct 38 ms 1996 KB Output is correct
15 Correct 18 ms 2228 KB Output is correct
16 Correct 33 ms 3992 KB Output is correct
17 Correct 81 ms 5068 KB Output is correct
18 Correct 56 ms 30580 KB Output is correct
19 Correct 67 ms 2020 KB Output is correct
20 Correct 27 ms 2832 KB Output is correct
21 Correct 209 ms 152728 KB Output is correct
22 Correct 101 ms 5704 KB Output is correct
23 Correct 221 ms 2368 KB Output is correct
24 Correct 99 ms 3800 KB Output is correct
25 Correct 306 ms 278616 KB Output is correct