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 <bits/stdc++.h>
#include "race.h"
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Node = array<int, 4>;
int best_path(int N, int K, int H[][2], int L[]) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int ans = 1e9;
vector<vector<pii> > graph(N);
for(int i=0; i<N-1; i++) {
graph[H[i][0]].push_back({ H[i][1], L[i] });
graph[H[i][1]].push_back({ H[i][0], L[i] });
}
for(int i=0; i<N; i++) {
stack<Node> st;
st.push({ i, 0, 0, 0 });
while(!st.empty()) {
Node nd = st.top();
st.pop();
if(nd[3] > K) continue;
if(nd[3] == K) ans = min(ans, nd[2]);
if(nd[2] + 1 >= ans) continue;
for(auto &[v, w] : graph[nd[0]]) {
if(v == nd[1]) continue;
st.push({ v, nd[0], nd[2] + 1, nd[3] + w });
}
}
}
if(ans == 1e9) return -1;
return ans;
}
// int32_t main() {
// int n, k;
// cin >> n >> k;
// int g[n-1][2];
// for(int i=0; i<n-1; i++) {
// int a, b;
// cin >> a >> b;
// g[i][0] = a;
// g[i][1] = b;
// }
// int len[n-1];
// for(int i=0; i<n-1; i++) cin >> len[i];
// cout << best_path(n, k, g, len) << '\n';
// return 0;
// }
Compilation message (stderr)
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:37:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
37 | for(auto &[v, w] : graph[nd[0]]) {
| ^
# | 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... |