#include "race.h"
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
void root_tree (vector<int> &parents, vector<vector<pair<int, int> > > &adjacent, int current){
for (int i = 0; i < adjacent[current].size(); i++){
long long child = adjacent[current][i].first;
if (parents[child] == -1){
parents[child] = current;
root_tree(parents, adjacent, child);
}
}
}
void process_node(int N, vector<int> &parents, vector<vector<pair<int, int> > > &adjacent, vector<vector<int> > &least_highways, int current){
least_highways[current][0] = 0;
for (int i = 0; i < adjacent[current].size(); i++){
int child = adjacent[current][i].first;
int d = adjacent[current][i].second;
if (parents[child] == current){
if (least_highways[child][0] == N + 1){
process_node(N, parents, adjacent, least_highways, child);
}
for (int r = d; r < least_highways[0].size(); r++){
//cout << r << ' ' << d << ' ' << 'c' << child << ' ' << current << ' ' << least_highways[child][r - d] << endl;
if (r - d >= 0){
least_highways[current][r] = min(least_highways[current][r], least_highways[child][r - d] + 1);
}
}
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
vector<vector<pair<int, int> > > adjacent (N, vector<pair<int, int> >());
for (int i = 0; i < N - 1; i++){
adjacent[H[i][0]].push_back(make_pair(H[i][1], L[i]));
adjacent[H[i][1]].push_back(make_pair(H[i][0], L[i]));
}
if (N <= 1000){
int best = N + 1;
for (int i = 0; i < N; i++){
//cout << i << ' ' << endl;
vector<pair<int, int> > distance (N, make_pair(-1, -1));
queue<int> to_visit;
to_visit.push(i);
distance[i].first = 0;
distance[i].second = 0;
while (!to_visit.empty()){
int current = to_visit.front();
to_visit.pop();
if (distance[current].second == K){
best = min(best, distance[current].first);
}
for (int r = 0; r < adjacent[current].size(); r++){
int next = adjacent[current][r].first;
if (distance[next].first == -1){
distance[next].first = distance[current].first + 1;
distance[next].second = distance[current].second + adjacent[current][r].second;
to_visit.push(next);
}
}
}
}
if (best == N + 1){
return -1;
}
return best;
} else {
vector<int> parents (N, -1);
parents[0] = -2;
root_tree(parents, adjacent, 0);
vector<vector<int> > least_highways (N, vector<int> (K + 1, N + 1));
process_node(N, parents, adjacent, least_highways, 0);
int best = N + 1;
// for (int i = 0; i < N; i ++){
//
// cout << endl;
// cout << i << ":" << endl;
// for (int j = 0; j <= K; j++){
// cout << least_highways[i][j] << ' ';
// }
// cout << endl;
//
// }
for (int n = 0; n < N; n++){
for (int i = 0; i < adjacent[n].size(); i++){
int child1 = adjacent[n][i].first;
if (parents[child1] == n){
for (int j = i + 1; j < adjacent[n].size(); j++){
int child2 = adjacent[n][j].first;
if (parents[child2] == n){
int added_distance = adjacent[n][i].second + adjacent[n][j].second;
for (int r = 0; r <= K - added_distance; r++){
best = min(best, 2 + least_highways[child1][r] + least_highways[child2][K - added_distance - r]);
}
}
}
}
best = min (best, least_highways[n][K]);
//cout << n << ' ' << best << endl;
}
}
if (best > N){
return -1;
}
return best;
return N;
}
}
Compilation message
race.cpp: In function 'void root_tree(std::vector<int>&, std::vector<std::vector<std::pair<int, int> > >&, int)':
race.cpp:14:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adjacent[current].size(); i++){
~~^~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'void process_node(int, std::vector<int>&, std::vector<std::vector<std::pair<int, int> > >&, std::vector<std::vector<int> >&, int)':
race.cpp:30:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adjacent[current].size(); i++){
~~^~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:40:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int r = d; r < least_highways[0].size(); r++){
~~^~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:91:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int r = 0; r < adjacent[current].size(); r++){
~~^~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:135:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adjacent[n].size(); i++){
~~^~~~~~~~~~~~~~~~~~~~
race.cpp:138:43: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = i + 1; j < adjacent[n].size(); j++){
~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
26 ms |
384 KB |
Output is correct |
22 |
Correct |
27 ms |
384 KB |
Output is correct |
23 |
Correct |
27 ms |
384 KB |
Output is correct |
24 |
Correct |
26 ms |
384 KB |
Output is correct |
25 |
Correct |
26 ms |
384 KB |
Output is correct |
26 |
Correct |
26 ms |
384 KB |
Output is correct |
27 |
Correct |
26 ms |
512 KB |
Output is correct |
28 |
Correct |
26 ms |
504 KB |
Output is correct |
29 |
Correct |
26 ms |
384 KB |
Output is correct |
30 |
Correct |
26 ms |
384 KB |
Output is correct |
31 |
Correct |
26 ms |
384 KB |
Output is correct |
32 |
Correct |
26 ms |
384 KB |
Output is correct |
33 |
Correct |
27 ms |
384 KB |
Output is correct |
34 |
Correct |
20 ms |
384 KB |
Output is correct |
35 |
Correct |
19 ms |
384 KB |
Output is correct |
36 |
Correct |
16 ms |
384 KB |
Output is correct |
37 |
Correct |
17 ms |
384 KB |
Output is correct |
38 |
Correct |
21 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
167 ms |
50936 KB |
Output is correct |
20 |
Correct |
159 ms |
52344 KB |
Output is correct |
21 |
Correct |
175 ms |
52344 KB |
Output is correct |
22 |
Correct |
172 ms |
52472 KB |
Output is correct |
23 |
Correct |
132 ms |
52600 KB |
Output is correct |
24 |
Correct |
153 ms |
52600 KB |
Output is correct |
25 |
Correct |
151 ms |
56788 KB |
Output is correct |
26 |
Correct |
106 ms |
61176 KB |
Output is correct |
27 |
Correct |
1897 ms |
94864 KB |
Output is correct |
28 |
Correct |
318 ms |
122360 KB |
Output is correct |
29 |
Correct |
322 ms |
117936 KB |
Output is correct |
30 |
Correct |
1879 ms |
94884 KB |
Output is correct |
31 |
Correct |
1899 ms |
94840 KB |
Output is correct |
32 |
Correct |
2206 ms |
104312 KB |
Output is correct |
33 |
Correct |
281 ms |
103416 KB |
Output is correct |
34 |
Correct |
229 ms |
103800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
26 ms |
384 KB |
Output is correct |
22 |
Correct |
27 ms |
384 KB |
Output is correct |
23 |
Correct |
27 ms |
384 KB |
Output is correct |
24 |
Correct |
26 ms |
384 KB |
Output is correct |
25 |
Correct |
26 ms |
384 KB |
Output is correct |
26 |
Correct |
26 ms |
384 KB |
Output is correct |
27 |
Correct |
26 ms |
512 KB |
Output is correct |
28 |
Correct |
26 ms |
504 KB |
Output is correct |
29 |
Correct |
26 ms |
384 KB |
Output is correct |
30 |
Correct |
26 ms |
384 KB |
Output is correct |
31 |
Correct |
26 ms |
384 KB |
Output is correct |
32 |
Correct |
26 ms |
384 KB |
Output is correct |
33 |
Correct |
27 ms |
384 KB |
Output is correct |
34 |
Correct |
20 ms |
384 KB |
Output is correct |
35 |
Correct |
19 ms |
384 KB |
Output is correct |
36 |
Correct |
16 ms |
384 KB |
Output is correct |
37 |
Correct |
17 ms |
384 KB |
Output is correct |
38 |
Correct |
21 ms |
384 KB |
Output is correct |
39 |
Correct |
167 ms |
50936 KB |
Output is correct |
40 |
Correct |
159 ms |
52344 KB |
Output is correct |
41 |
Correct |
175 ms |
52344 KB |
Output is correct |
42 |
Correct |
172 ms |
52472 KB |
Output is correct |
43 |
Correct |
132 ms |
52600 KB |
Output is correct |
44 |
Correct |
153 ms |
52600 KB |
Output is correct |
45 |
Correct |
151 ms |
56788 KB |
Output is correct |
46 |
Correct |
106 ms |
61176 KB |
Output is correct |
47 |
Correct |
1897 ms |
94864 KB |
Output is correct |
48 |
Correct |
318 ms |
122360 KB |
Output is correct |
49 |
Correct |
322 ms |
117936 KB |
Output is correct |
50 |
Correct |
1879 ms |
94884 KB |
Output is correct |
51 |
Correct |
1899 ms |
94840 KB |
Output is correct |
52 |
Correct |
2206 ms |
104312 KB |
Output is correct |
53 |
Correct |
281 ms |
103416 KB |
Output is correct |
54 |
Correct |
229 ms |
103800 KB |
Output is correct |
55 |
Correct |
199 ms |
190328 KB |
Output is correct |
56 |
Correct |
17 ms |
6400 KB |
Output is correct |
57 |
Correct |
83 ms |
33784 KB |
Output is correct |
58 |
Execution timed out |
3091 ms |
55532 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |