답안 #541264

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
541264 2022-03-22T20:48:04 Z rainliofficial Museum (CEOI17_museum) C++17
80 / 100
199 ms 17392 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define f first
#define s second

/**
 * The challenge of the problem is to know which edges we want to "backtrack" on (meaning we pass an edge twice). 
 * It is key that we realize the given graph is a tree, which means we can use tree DP. 
 * 
 * Let dp[i][j][0/1] = the min cost to visit j nodes in subtree of i, such that 0 = we don't return back to i, 1 = we return back to i
 */

struct edge{
    int to, c;
};

const int MAXN = 10001, MAXK = 101;
int n, k, x, ss[MAXN], dp[MAXN][MAXK][2], dp2[MAXN][MAXK][2];
vector<edge> arr[MAXN];

void subtreeSize(int at, int par){
    ss[at] = 1;
    for (edge i : arr[at]){
        if (i.to != par){
            subtreeSize(i.to, at);
            ss[at] += ss[i.to];
        }
    }
}

void dfs(int at, int par){
    // try to "tack on" the answer of the children of at
    int currSize = 1; 
    for (edge i : arr[at]){
        if (i.to != par){
            dfs(i.to, at);
            // combine the new subtree with the existing subtree of at
            for (int atSize=1; atSize<=currSize; atSize++){
                for (int newSize=1; newSize<=ss[i.to]; newSize++){
                    if (atSize + newSize > k){
                        continue;
                    }
                    dp2[at][atSize + newSize][0] = min({dp2[at][atSize + newSize][0], dp[at][atSize][1] + dp[i.to][newSize][0] + i.c, dp[at][atSize][0] + dp[i.to][newSize][1] + 2*i.c});
                    dp2[at][atSize + newSize][1] = min(dp2[at][atSize + newSize][1], dp[at][atSize][1] + dp[i.to][newSize][1] + 2*i.c);
                }
            }
            // copy over data
            for (int atSize=1; atSize<=currSize; atSize++){
                for (int newSize=1; newSize<=ss[i.to]; newSize++){
                    if (atSize + newSize > k){
                        continue;
                    }
                    dp[at][atSize + newSize][0] = min(dp[at][atSize + newSize][0], dp2[at][atSize + newSize][0]);
                    dp[at][atSize + newSize][1] = min(dp[at][atSize + newSize][1], dp2[at][atSize + newSize][1]);
                }
            }
            for (int atSize=1; atSize<=currSize; atSize++){
                for (int newSize=1; newSize<=ss[i.to]; newSize++){
                    if (atSize + newSize > k){
                        continue;
                    }
                    dp2[at][atSize + newSize][0] = dp2[at][atSize + newSize][1] = INT_MAX;
                    
                }
            }
            currSize += ss[i.to];
        }
    }
}
int main(){
    cin.tie(0); ios_base::sync_with_stdio(0);
    // freopen("file.in", "r", stdin);
    // freopen("file.out", "w", stdout);
    cin >> n >> k >> x;
    x--;
    for (int i=0; i<n-1; i++){
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        arr[a].push_back({b, c});
        arr[b].push_back({a, c});
    }
    for (int i=0; i<MAXN; i++){
        for (int j=0; j<MAXK; j++){
            if (j > 1){
                dp[i][j][0] = dp[i][j][1] = INT_MAX;
            }
            dp2[i][j][0] = dp2[i][j][1] = INT_MAX;
        }
    }
    subtreeSize(x, -1);
    dfs(x, -1);
    cout << dp[x][k][0] << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 16340 KB Output is correct
2 Correct 9 ms 16340 KB Output is correct
3 Correct 10 ms 16324 KB Output is correct
4 Correct 8 ms 16340 KB Output is correct
5 Correct 8 ms 16340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 16796 KB Output is correct
2 Correct 130 ms 16936 KB Output is correct
3 Correct 150 ms 17392 KB Output is correct
4 Correct 133 ms 17084 KB Output is correct
5 Correct 136 ms 16952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 16796 KB Output is correct
2 Correct 130 ms 16936 KB Output is correct
3 Correct 150 ms 17392 KB Output is correct
4 Correct 133 ms 17084 KB Output is correct
5 Correct 136 ms 16952 KB Output is correct
6 Correct 173 ms 16908 KB Output is correct
7 Correct 140 ms 17232 KB Output is correct
8 Correct 199 ms 16872 KB Output is correct
9 Correct 170 ms 16940 KB Output is correct
10 Correct 129 ms 16960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 16340 KB Output is correct
2 Correct 9 ms 16340 KB Output is correct
3 Correct 10 ms 16324 KB Output is correct
4 Correct 8 ms 16340 KB Output is correct
5 Correct 8 ms 16340 KB Output is correct
6 Correct 121 ms 16796 KB Output is correct
7 Correct 130 ms 16936 KB Output is correct
8 Correct 150 ms 17392 KB Output is correct
9 Correct 133 ms 17084 KB Output is correct
10 Correct 136 ms 16952 KB Output is correct
11 Correct 173 ms 16908 KB Output is correct
12 Correct 140 ms 17232 KB Output is correct
13 Correct 199 ms 16872 KB Output is correct
14 Correct 170 ms 16940 KB Output is correct
15 Correct 129 ms 16960 KB Output is correct
16 Incorrect 151 ms 16936 KB Output isn't correct
17 Halted 0 ms 0 KB -