제출 #1276681

#제출 시각아이디문제언어결과실행 시간메모리
1276681iustinmircea10Museum (CEOI17_museum)C++17
20 / 100
80 ms6084 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

vector<pair<int, int>> dp[10005]; //dp[x][k].fi = raspuns pentru subtree x, k noduri, timp minim, .second = iar pentru acel timp, distanta minima fata de x
vector<pair<int, int>> adj[10005];

void dfs(int u, int p = 0) {
    dp[u].push_back({0, 0});
    dp[u].push_back({0, 0});
    for (auto [v, cost] : adj[u]) {
        if (v == p) continue;
        dfs(v, u);
        vector<pair<int, int>> newdp(dp[u].size() + dp[v].size() - 1, {1e18, 1e18});
        //evitam edge caseuri cu j = 0, adica doar in subarb lui u
        for (int i = 0; i < dp[u].size(); i++) {
            newdp[i] = dp[u][i];
        }
        for (int i = 1; i < dp[u].size(); i++) {
            for (int j = 1; j < dp[v].size(); j++) {
                int newk = i + j;
                //daca ma duc mai intai in u si apoi in v
                if (dp[u][i].first + dp[u][i].second + cost + dp[v][j].first < newdp[newk].first) {
                    newdp[newk].first = dp[u][i].first + dp[u][i].second + cost + dp[v][j].first;
                    newdp[newk].second = dp[v][j].second + cost;
                }
                else if (dp[u][i].first + dp[u][i].second + cost + dp[v][j].first == newdp[newk].first) {
                    newdp[newk].second = min(newdp[newk].second, dp[v][j].second + cost);
                }
                //acum daca ma duc mai intai in v si apoi in u
                if (cost + dp[v][j].first + dp[v][j].second + cost + dp[u][i].first < newdp[newk].first) {
                    newdp[newk].first = cost + dp[v][j].first + dp[v][j].second + cost + dp[u][i].first;
                    newdp[newk].second = dp[u][i].second;
                }
                else if (cost + dp[v][j].first + dp[v][j].second + cost + dp[u][i].first == newdp[newk].first) {
                    newdp[newk].second = min(newdp[newk].second, dp[u][i].second);
                }
            }
        }
        dp[u] = newdp;
    }
}

signed main() {

    int n, k, start;
    cin >> n >> k >> start;
    for (int i = 1; i < n; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
    dfs(start);
    cout << dp[start][k].first << '\n';

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...