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>
using namespace std;
using ll = long long;
int best_path(int n, int k, int H[][2], int L[]) {
vector<int> adj[n];
ll dis[n][n] {};
int cost[n][n] {}, D[n][n] {};
for(int i = 0; i < n - 1; i++) {
adj[H[i][0]].push_back(H[i][1]);
adj[H[i][1]].push_back(H[i][0]);
cost[H[i][0]][H[i][1]] = L[i];
cost[H[i][1]][H[i][0]] = L[i];
}
for(int i = 0; i < n; i++) {
bool vis[n] {};
queue<int> q;
q.push(i);
vis[i] = 1;
while(!q.empty()) {
int x = q.front();
q.pop();
for(int X : adj[x]) {
if(!vis[X]) {
dis[i][X] = dis[i][x] + cost[x][X];
D[i][X] = D[i][x] + 1;
vis[X] = 1;
q.push(X);
}
}
}
}
int mn = 1e9;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i == j) continue;
if(dis[i][j] == k) {
mn = min(mn, D[i][j]);
}
}
}
if(mn == 1e9) mn = -1;
return mn;
}
//int main() {
// ios::sync_with_stdio(0);
// cin.tie(0);
// int n, k;
// cin >> n >> k;
// int H[n][2], L[n];
// for(int i = 0; i < n - 1; i++) {
// cin >> H[i][0] >> H[i][1];
// }
// for(int i = 0; i < n - 1; i++) {
// cin >> L[i];
// }
//
// return 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... |