제출 #348738

#제출 시각아이디문제언어결과실행 시간메모리
348738idk321경주 (Race) (IOI11_race)C++11
9 / 100
137 ms49564 KiB
#include "race.h"
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 200001;
const int K = 101;

vector<array<int, 2>> adj[N];
int dp[N][K];
//int par[N];
int n, k;


int dfs1(int node, int parent)
{
    dp[node][0] = 0;
    //par[node] = parent;
    int res = N;
    for (auto next : adj[node])
    {
        if (next[0] == parent) continue;

        vector<int> bestAt(k + 1, N);
        bestAt[0] = 0;
        res = min(res, dfs1(next[0], node));
        for (int i = 0; i + next[1] <= k; i++)
        {
            //cout << node << " " << next[0] << " " << i << " " << dp[next[0]][i] << endl;
            dp[node][i + next[1]] = min(dp[node][i + next[1]], dp[next[0]][i] + 1);
            res = min(res, dp[next[0]][i] + 1 + bestAt[k - i - next[1]]);
        }
        for (int i = 0; i + next[1] <= k; i++)
        {
            bestAt[i + next[1]] = min(bestAt[i + next[1]], dp[next[0]][i] + 1);
        }
    }

    return res;
}

int best_path(int n1, int k1, int H[][2], int L[])
{
    n = n1;
    k = k1;
    for (int i = 0; i < n; i++) for (int j = 0; j <= k; j++) dp[i][j] = N;
    for (int i = 0; i < n - 1; i++)
    {
        adj[H[i][0]].push_back({H[i][1], L[i]});
        adj[H[i][1]].push_back({H[i][0], L[i]});
    }


    int res = dfs1(0, -1);
    if (res == N) return -1;
    return res;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...