# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
904725 | StefanL2005 | Race (IOI11_race) | C++14 | 0 ms | 0 KiB |
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;
ifstream in("file.in");
ofstream out("file.out");
#define inf 1000*1000*1000
void DFS(int node, int parent, vector<vector<int>> &vecin, vector<vector<int>> &cost, vector<int> &dist, vector<int> &path)
{
for (int i = 0; i < vecin[node].size(); i++)
{
auto V = vecin[node][i];
if (V == parent)
continue;
path[V] = path[node] + 1;
dist[V] = dist[node] + cost[node][i];
DFS(V, node, vecin, cost, dist, path);
}
}
int best_path(int n, int k, vector<vector<int>> H, vector<int> L)
{
int best_ans = inf;
vector<int> dist;
vector<int> path;
vector<vector<int>> vecin(n), cost(n);
for (int i = 0; i < H.size(); i++)
{
vecin[H[i][0]].push_back(H[i][1]);
vecin[H[i][1]].push_back(H[i][0]);
cost[H[i][0]].push_back(L[i]);
cost[H[i][1]].push_back(L[i]);
}
for (int i = 0; i < n; i++)
{
dist = vector<int> (n);
path = vector<int> (n);
dist[i] = path[i] = 0;
DFS(i, -1, vecin, cost, dist, path);
for (int i = 0; i < n; i++)
if (dist[i] == k)
best_ans = min(best_ans, path[i]);
}
if (best_ans == inf)
return -1;
return best_ans;
}