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>
#define s second
#define f first
using namespace std;
using ll = long long;
vector<int> adj[200000];
vector<pair<int, int>> adj1[200000];
ll dis[1000][1000] {};
int cost[1000][1000] {}, D[1000][1000] {};
int dp[200000][101], K;
int S = 1e9;
void dfs(int X, int Y) {
for(int i = 0; i <= K; i++) {
dp[X][i] = 1e9;
}
dp[X][0] = 0;
for(pair<int, int> P : adj1[X]) {
int x = P.f;
int y = P.s;
if(x != Y) {
dfs(x, X);
for(int i = 0; i + y <= K; i++) {
int j = K - i - x;
S = min(S, dp[X][i] + dp[x][j] + 1);
}
for(int i = 0; i + y <= K; i++) {
dp[X][i + y] = min(dp[X][i + y], dp[x][i] + 1);
}
}
}
}
int best_path(int n, int k, int H[][2], int L[]) {
for(int i = 0; i < n; i++) {
adj[i].clear();
adj1[i].clear();
}
for(int i = 0; i < min(n, 1000); i++) {
for(int j = 0; j < min(n, 1000); j++) {
dis[i][j] = 0;
cost[i][j] = 0;
D[i][j] = 0;
}
}
for(int i = 0; i < min(999, n - 1); i++) {
adj[H[i][0]].push_back(H[i][1]);
adj[H[i][1]].push_back(H[i][0]);
adj1[H[i][0]].push_back({H[i][1], L[i]});
adj1[H[i][1]].push_back({H[i][0], L[i]});
cost[H[i][0]][H[i][1]] = L[i];
cost[H[i][1]][H[i][0]] = L[i];
}
if(k <= 100) {
S = 1e9;
K = k;
dfs(0, -1);
if(S == 1e9) {
S = -1;
}
return S;
} else {
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... |