#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
int k = 0;
void DFS_sum(int node, int p, vector<int> &sum, vector<vector<int>> &vec, vector<vector<int>> &cost, vector<int> &col)
{
col[node] = k;
for (int i = 0; i < vec[node].size(); i++)
{
if (vec[node][i] == p)
continue;
DFS_sum(vec[node][i], node, sum, vec, cost, col);
sum[node] = max(sum[node], sum[vec[node][i]] + cost[node][i]);
}
}
void DFS_main(int node, int p, int main, int sec, vector<int> &sum, int &ans, vector<vector<int>> &vec, vector<vector<int>> &cost)
{
ans = min(ans, main);
int path = -1;
for (int i = 0; i < vec[node].size(); i++)
{
int temp = sum[vec[node][i]] + cost[node][i];
if (vec[node][i] == p)
continue;
if (temp > sec && (temp != main || path != -1))
sec = temp;
if (temp == main)
path = i;
}
if (path == -1)
return;
main = sum[vec[node][path]];
sec += cost[node][path];
if (sec > main)
{
ans = min(ans, sec);
return;
}
DFS_main(vec[node][path], node, main, sec, sum, ans, vec, cost);
}
int graph_hub(int node, vector<vector<int>> &vec, vector<vector<int>> &cost, vector<int> &col)
{
vector<int> sum(vec.size(), 0);
DFS_sum(node, -1, sum, vec, cost, col);
int MAX = 0, sec = 0;
for (int i = 0; i < vec[node].size(); i++)
{
int temp = sum[vec[node][i]] + cost[node][i];
if (temp > MAX)
{
sec = MAX;
MAX = temp;
}
else
sec = max(sec, temp);
}
int ans = 1000 * 1000 * 1000;
DFS_main(node, -1, MAX, sec, sum, ans, vec, cost);
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int C[])
{
vector<int> col(N, -1);
vector<vector<int>> vec(N), cost(N);
for (int i = 0; i < M; i++)
{
vec[A[i]].push_back(B[i]);
vec[B[i]].push_back(A[i]);
cost[A[i]].push_back(C[i]);
cost[B[i]].push_back(C[i]);
}
if (M == N - 1)
{
vector<int> sum(N, 0);
DFS_sum(0, -1, sum, vec, cost, col);
int unos = 0, dos = 0;
for (int i = 0; i < vec[0].size(); i++)
{
int temp = sum[vec[0][i]] + cost[0][i];
if (temp > unos)
{
dos = unos;
unos = temp;
}
else
dos = max(dos, temp);
}
return unos + dos;
}
vector<int> hub_l;
for (int i = 0; i < N; i++)
if (col[i] == -1)
{
hub_l.push_back(graph_hub(i, vec, cost, col));
k++;
}
int unos = 0, dos = 0, tres = 0;
for (int i = 0; i < hub_l.size(); i++)
{
int p = hub_l[i];
if (p > unos)
{
tres = dos;
dos = unos;
unos = p;
}
else
if (p > dos)
{
tres = dos;
dos = p;
}
else
tres = max(tres, p);
}
if (M == N - 2)
return unos + L + dos;
return max(unos + L + dos, dos + 2 * L + tres);
}
Compilation message
dreaming.cpp: In function 'void DFS_sum(int, int, std::vector<int>&, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&, std::vector<int>&)':
dreaming.cpp:10:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for (int i = 0; i < vec[node].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'void DFS_main(int, int, int, int, std::vector<int>&, int&, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
dreaming.cpp:24:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int i = 0; i < vec[node].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int graph_hub(int, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&, std::vector<int>&)':
dreaming.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = 0; i < vec[node].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:93:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int i = 0; i < vec[0].size(); i++)
| ~~^~~~~~~~~~~~~~~
dreaming.cpp:117:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for (int i = 0; i < hub_l.size(); i++)
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
16880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
16880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
290 ms |
10360 KB |
Output is correct |
2 |
Correct |
299 ms |
10832 KB |
Output is correct |
3 |
Correct |
282 ms |
10584 KB |
Output is correct |
4 |
Correct |
290 ms |
10716 KB |
Output is correct |
5 |
Correct |
299 ms |
10556 KB |
Output is correct |
6 |
Correct |
352 ms |
11468 KB |
Output is correct |
7 |
Correct |
320 ms |
11436 KB |
Output is correct |
8 |
Correct |
292 ms |
10736 KB |
Output is correct |
9 |
Correct |
271 ms |
10416 KB |
Output is correct |
10 |
Correct |
321 ms |
10916 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
473 ms |
6476 KB |
Output is correct |
13 |
Correct |
504 ms |
6640 KB |
Output is correct |
14 |
Correct |
487 ms |
6336 KB |
Output is correct |
15 |
Correct |
469 ms |
6560 KB |
Output is correct |
16 |
Correct |
450 ms |
6344 KB |
Output is correct |
17 |
Correct |
474 ms |
6444 KB |
Output is correct |
18 |
Correct |
495 ms |
6544 KB |
Output is correct |
19 |
Correct |
476 ms |
6596 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
467 ms |
6240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
16880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |