71522023-12-31 18:21:01MagyarKendeSZLGTársaság (50)cpp17Wrong answer 7/507ms6160 KiB
#include <bits/stdc++.h>

#define speed cin.tie(0); ios::sync_with_stdio(0)
#define cinv(v) for (auto& e : v) cin >> e;
#define all(v) v.begin(), v.end()
#define has(s, e) s.count(e)

using namespace std;
using ll = long long;
using point = array<int, 2>;

struct Route {
public:
    int node;
    int weight;
    bool operator()(Route a, Route b) {
        return a.weight > b.weight;
    }
};

int main() {
    speed;

    vector<vector<point>> g;

    int N, K;
    cin >> N >> K;
    
    g.resize(N + 1);
    for (int i = 2; i <= N; i++) {
        int V, W;
        cin >> V >> W;
        g[V].push_back({i, -W});
    }

    priority_queue<Route, vector<Route>, Route> pq;
    vector<ll> distS(N + 1, INT_MAX);
    vector<int> prevS(N + 1);
    vector<bool> vis(N + 1);

    pq.push({1, 0});
    distS[1] = 0;

    while (!pq.empty()) {
        auto [node, weight] = pq.top();
        pq.pop();

        if (distS[node] < weight) continue;

        for (auto [next_node, next_weight] : g[node]) {
            if (vis[next_node]) continue;
            vis[next_node] = 1;

            int dist = weight + next_weight;

            if (dist < distS[next_node]) {
                distS[next_node] = dist;
                prevS[next_node] = node;
                pq.push({next_node, dist});
            }
        }
    }

    int longest = min_element(all(distS)) - distS.begin(),
        sum = 0, next = longest;
    
    vector<int> path;

    while (prevS[next] != 0) {
        for (point p : g[prevS[next]]) {
            if (p[0] == next) {
                sum += -p[1];
                path.push_back(-p[1]);
                break;
            }
        }
        next = prevS[next];
    }
    
    sort(all(path));

    if (K >= path.size()) {
        cout << 0;
        exit(0);
    }

    for (int i = path.size() - 1; i >= path.size() - K; i--) {
        sum -= path[i];
    }

    cout << sum;
}
SubtaskSumTestVerdictTimeMemory
base7/50
1Accepted0/03ms1824 KiB
2Wrong answer0/04ms2688 KiB
3Wrong answer0/33ms2352 KiB
4Wrong answer0/33ms2580 KiB
5Accepted3/33ms2800 KiB
6Wrong answer0/33ms2788 KiB
7Wrong answer0/33ms3120 KiB
8Wrong answer0/33ms3352 KiB
9Wrong answer0/33ms3512 KiB
10Wrong answer0/34ms3864 KiB
11Wrong answer0/34ms4176 KiB
12Wrong answer0/34ms4380 KiB
13Wrong answer0/44ms4644 KiB
14Wrong answer0/46ms5036 KiB
15Wrong answer0/46ms5564 KiB
16Accepted4/47ms6160 KiB
17Wrong answer0/47ms6028 KiB