답안 #681214

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
681214 2023-01-12T14:32:12 Z sysia Museum (CEOI17_museum) C++17
80 / 100
872 ms 1048576 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18;

void solve(){
    int n, k, st; cin >> n >> k >> st;
    vector<vector<T>>g(n+1);
    for (int i = 1; i<n; i++){
        int a, b, c; cin >> a >> b >> c;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    vector<int>sub(n+1);
    vector<vector<int>>dp(n+1, vector<int>(k+1, oo));
    vector<vector<int>>dp2(n+1, vector<int>(k+1, oo));
    function<void(int, int)>dfs = [&](int v, int pa){
        sub[v] = 1;
        dp[v][0] = 0;
        dp[v][1] = 0;
        dp2[v][0] = 0;
        dp2[v][1] = 0;
        for (auto [x, c]: g[v]){
            if (x == pa) continue;
            dfs(x, v);
            for (int i = sub[v]; i>=0; i--){
                for (int j = sub[x]; j>=1; j--){
                    //od tylu, by nie robic overcounting
                    if (i + j > k) continue;
                    dp[v][i+j] =  min(dp[v][i+j],  dp[v][i] +  dp[x][j] + 2 * c); 
                    dp2[v][i+j] = min(dp2[v][i+j], dp[v][i] +  dp2[x][j]+ c);
                    dp2[v][i+j] = min(dp2[v][i+j], dp2[v][i]+  dp[x][j] + 2 * c);
                    //1. wchodzimy do jakichs poddrzew dzieci v, zawracamy i wchodzimy do jakiegos poddrzewa x znowu
                    //2. wchodzimy do jakichs poddrzew dzieci x, wchodzimy do v, pozniej do x i nie wracamy juz z v
                    //3. wchodzimy do x, wracamy i to v z idziemy tyyylko w dol
                    //mmm kocham syf na podzbiorach
                }
            }
            sub[v] += sub[x];
        }
    };
    dfs(st, st);
    cout << dp2[st][k] << "\n";
}

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

    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 17812 KB Output is correct
2 Correct 72 ms 17860 KB Output is correct
3 Correct 84 ms 18256 KB Output is correct
4 Correct 78 ms 17932 KB Output is correct
5 Correct 68 ms 17824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 17812 KB Output is correct
2 Correct 72 ms 17860 KB Output is correct
3 Correct 84 ms 18256 KB Output is correct
4 Correct 78 ms 17932 KB Output is correct
5 Correct 68 ms 17824 KB Output is correct
6 Correct 59 ms 17736 KB Output is correct
7 Correct 83 ms 18120 KB Output is correct
8 Correct 112 ms 17692 KB Output is correct
9 Correct 87 ms 17700 KB Output is correct
10 Correct 63 ms 17712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 64 ms 17812 KB Output is correct
7 Correct 72 ms 17860 KB Output is correct
8 Correct 84 ms 18256 KB Output is correct
9 Correct 78 ms 17932 KB Output is correct
10 Correct 68 ms 17824 KB Output is correct
11 Correct 59 ms 17736 KB Output is correct
12 Correct 83 ms 18120 KB Output is correct
13 Correct 112 ms 17692 KB Output is correct
14 Correct 87 ms 17700 KB Output is correct
15 Correct 63 ms 17712 KB Output is correct
16 Correct 202 ms 158672 KB Output is correct
17 Correct 872 ms 785088 KB Output is correct
18 Correct 237 ms 158928 KB Output is correct
19 Correct 239 ms 158696 KB Output is correct
20 Correct 215 ms 158620 KB Output is correct
21 Runtime error 451 ms 1048576 KB Execution killed with signal 9
22 Halted 0 ms 0 KB -