Submission #639182

#TimeUsernameProblemLanguageResultExecution timeMemory
639182finn__Race (IOI11_race)C++17
31 / 100
456 ms262144 KiB
#include <bits/stdc++.h>
using namespace std;

#include "race.h"

vector<map<size_t, size_t>> g;

// dp[i][j] holds the minimum size of a path ending at i and having total cost j.
vector<vector<size_t>> dp;
size_t min_size = SIZE_MAX;

void dfs(size_t u, size_t p, size_t k)
{
    dp[u][0] = 0;

    for (auto [v, w] : g[u])
    {
        if (v != p)
        {
            dfs(v, u, k);

            for (size_t j = w; j <= k; j++)
                if (dp[v][j - w] < SIZE_MAX && dp[u][k - j] < SIZE_MAX)
                    min_size = min(min_size, dp[v][j - w] + dp[u][k - j] + 1);

            for (size_t j = w; j <= k; j++)
                if (dp[v][j - w] < SIZE_MAX)
                    dp[u][j] = min(dp[u][j], dp[v][j - w] + 1);
        }
    }

    min_size = min(min_size, dp[u].back());
}

int best_path(int n, int k, int edges[][2], int length[])
{
    g = vector<map<size_t, size_t>>(n);
    for (size_t i = 0; i < n - 1; i++)
    {
        size_t u = edges[i][0], v = edges[i][1], w = length[i];
        g[u][v] = w;
        g[v][u] = w;
    };

    dp = vector<vector<size_t>>(n, vector<size_t>(k + 1, SIZE_MAX));
    dfs(0, 0, k);

    return min_size == SIZE_MAX ? -1 : min_size;
}

Compilation message (stderr)

race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:38:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   38 |     for (size_t i = 0; i < n - 1; i++)
      |                        ~~^~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...