This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "race.h"
#include <bits/stdc++.h>
#define range(it, a, b) for (ll it = a; it < b; it++)
#define all(x) begin(x), end(x)
#define ll long long
#define ull unsigned long long
#define INF64 ((ll) 1 << 60)
#define INF32 (1 << 30)
#define mset multiset
#define uset unordered_set
#define umap unordered_map
#define pqueue priority_queue
#define ptr(A) shared_ptr<A>
using namespace std;
struct Path {
ll i, w;
};
ll n, k;
vector<vector<Path>> adj;
// DISTANCE, NO. OF ROADS
vector<map<ll, ll>> paths;
ll ans = INF64;
void explore(ll i, ll from) {
paths[i][0] = 0;
for (Path child : adj[i]) {
if (child.i != from) {
explore(child.i, i);
if (paths[i].size() < paths[child.i].size())
swap(paths[child.i], paths[i]);
for (auto child_path : paths[child.i]) {
ll dist = child.w + child_path.first;
if (paths[i].count(k - dist))
ans = min(ans, paths[i][k - dist] + 1 + child_path.second);
}
for (auto child_path : paths[child.i]) {
ll dist = child.w + child_path.first;
if (!paths[i].count(dist))
paths[i][dist] = 1 + child_path.second;
else
paths[i][dist] = min(paths[i][dist], 1 + child_path.second);
}
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N; k = K;
adj.resize(n);
paths.resize(n);
range(i, 0, n) {
adj[H[i][0]].push_back({H[i][1], L[i]});
adj[H[i][1]].push_back({H[i][0], L[i]});
}
explore(0, 0);
return (ans == INF64 ? -1 : ans);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |