#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;
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){
if(!precalcolo)padre[i.to] = curr;
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});
}
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);
padre.resize(N, -1);
temp.reserve(N);
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);
sort(centers.begin(), centers.end());
for(int i=0; i<centers.size()-1; i++){
// cout << "Aggiungo gli archi " << centers[i].second << " " << centers[centers.size()-1].second << "\n";
g[centers[i].second].push_back({centers[centers.size()-1].second, L});
g[centers[centers.size()-1].second].push_back({centers[i].second, L});
}
fill(dist.begin(), dist.end(), INF);
bfs(1, true);
int node = max_element(dist.begin(), dist.end()) - dist.begin();
fill(dist.begin(), dist.end(), INF);
return bfs(node, false);
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:85:28: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
for(int i=0; i<N; i++) if(dist[i] == INF)
^~
dreaming.cpp:88:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
fill(dist.begin(), dist.end(), INF);
^~~~
dreaming.cpp:94:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<centers.size()-1; i++){
~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1057 ms |
7160 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1057 ms |
7160 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1057 ms |
7160 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1064 ms |
5908 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1057 ms |
7160 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1057 ms |
7160 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |