#pragma GCC optimize("O3")
#include "dreaming.h"
#include <bits/stdc++.h>
const int INF = 2e9;
using namespace std;
struct arco
{
int to, p;
};
int N;
vector<vector<arco>> g;
vector<int> dist;
vector<pair<int, int>> centers;
vector<int> temp;
vector<int> padre;
queue<int> changed;
int bfs(int node, bool precalcolo){
queue<int> q;
dist[node] = 0;
q.push(node);
int maxiNode = node;
int maxi = 0;
while(!q.empty()){
auto curr = q.front();
q.pop();
if(maxi < dist[curr])
maxi = dist[curr], maxiNode = curr;
for(auto i : g[curr]){
if(dist[i.to] > dist[curr] + i.p){
padre[i.to] = curr;
changed.push(i.to);
q.push(i.to);
dist[i.to] = dist[curr] + i.p;
}
}
}
// for(auto i : padre)cout << i << " ";
// cout << "\n\n";
if(precalcolo)
temp.push_back(maxiNode);
else{
int diameter = maxi;
int center = maxiNode;
int curr = maxiNode;
maxi = maxi;
// cout << curr <<"\n";
// cout << "\t" << max(diameter-dist[curr], dist[curr]) << " " << maxi << "\n";
while(padre[curr] != -1){
//cout << curr << " " << padre[curr] << "\n";
curr = padre[curr];
//cout << curr << "\n";
if(max(diameter-dist[curr], dist[curr]) < maxi){
//cout << "\t" << max(diameter-dist[curr], dist[curr]) << " " << maxi << "\n";
maxi = max(diameter-dist[curr], dist[curr]);
center = curr;
}
}
centers.push_back({maxi, center});
}
while(!changed.empty()){
padre[changed.front()] = -1;
changed.pop();
}
return *max_element(dist.begin(), dist.end());
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
::N = N;
g.resize(N);
dist.resize(N);
centers.reserve(N);
temp.reserve(N);
padre.resize(N, -1);
for(int i=0; i<M; i++){
g[A[i]].push_back({B[i], T[i]});
g[B[i]].push_back({A[i], T[i]});
}
fill(dist.begin(), dist.end(), INF);
for(int i=0; i<N; i++) if(dist[i] == INF)
bfs(i, true);
fill(dist.begin(), dist.end(), INF);
for(auto i : temp)
bfs(i, false);
nth_element(centers.begin(), centers.begin()+centers.size()-2, centers.end());
return L + centers.back().first + centers[centers.size()-2].first;
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:93:28: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
for(int i=0; i<N; i++) if(dist[i] == INF)
^~
dreaming.cpp:96:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
fill(dist.begin(), dist.end(), INF);
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
64 ms |
7544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
64 ms |
7544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
64 ms |
7544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1064 ms |
5956 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
64 ms |
7544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
64 ms |
7544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |