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;
typedef pair<int,int> ii;
typedef vector<ii> vii;
#define F first
#define S second
int best_path(int n, int k, int h[][2], int L[]){
vector<pair<int,int>> roads;
vector<int> w;
for (int i = 0; i < n - 1; i++){
///cout << L[i] << " " << h[i][0] << " " << h[i][1] << "\n";
if (L[i] == k) return 1;
//if(L[i] > k) continue;
roads.push_back({h[i][0], h[i][1]});
w.push_back(L[i]);
}
vector<vii> graph(n);
for (int i = 0; i < roads.size(); i++){
int u = roads[i].F, v = roads[i].S;
graph[u].push_back({v,w[i]});
graph[v].push_back({u,w[i]});
}
int ans = INT_MAX;
vector<bool> temp(n + 5,0);
vector<vector<bool>> vis(n + 5, temp);
for (int s = 0; s < n; s++){
// Definir que punto iniciara el algoritmo
// Guaradar info como v,w
vector<long long int> peso(n + 5, INT_MAX);
vector<int> dist(n + 5, 0);
// Aqui dist[s], s es el inicio
peso[s] = 0;
vis[s][s] = 1;
priority_queue<ii,vii,greater<ii>> pq;
pq.push(ii(0,s));
while(!pq.empty()){
int d = pq.top().F;
int u = pq.top().S;
pq.pop();
if (d > peso[u]) continue;
if (peso[u] > k) continue;
for (auto x : graph[u]){
if(vis[x.F][u]) continue;
if(peso[u] + x.S < peso[x.F]){
peso[x.F] = peso[u] + x.S;
dist[x.F] = dist[u] + 1;
// Se invierte de v,w a w,v
pq.push(ii(peso[x.F],x.F));
vis[u][x.F] = 1;
}
}
}
auto f = find(peso.begin(), peso.end(), k);
if (f != peso.end()){
int p = f - peso.begin();
ans = min(ans, dist[p]);
}
//cout << s << " : ";
//for (int i = 0; i < n; i++) cout << peso[i] << "," << dist[i] << " ";
cout << "\n";
}
if (ans == INT_MAX) return -1;
return ans;
}
Compilation message (stderr)
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:19:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for (int i = 0; i < roads.size(); i++){
| ~~^~~~~~~~~~~~~~
# | 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... |