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 <iostream>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>
#include <algorithm>
#include "dreaming.h"
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
pii calcDist(int i, vvpii & G, vector<bool> & visited, vi & dist1, vi & dist2, vi & dist3) {
queue<int> Q;
Q.push(i);
int u, v, t, maxNode = i;
vector<int> vertices = {};
dist1[i] = 0;
while(!Q.empty()) {
u = Q.front();
Q.pop();
visited[u] = true;
vertices.push_back(u);
if (dist1[u] > dist1[maxNode])
maxNode = u;
for (auto connection: G[u]) {
v = connection.first;
t = connection.second;
if (dist1[v] > dist1[u]+t) {
dist1[v] = dist1[u]+t;
Q.push(v);
}
}
}
dist2[maxNode] = 0;
Q.push(maxNode);
while(!Q.empty()) {
u = Q.front();
Q.pop();
if (dist2[u] > dist2[maxNode])
maxNode = u;
for (auto connection: G[u]) {
v = connection.first;
t = connection.second;
if (dist2[v] > dist2[u]+t) {
dist2[v] = dist2[u]+t;
Q.push(v);
}
}
}
int diam = dist2[maxNode];
dist3[maxNode] = 0;
Q.push(maxNode);
while(!Q.empty()) {
u = Q.front();
Q.pop();
for (auto connection: G[u]) {
v = connection.first;
t = connection.second;
if (dist3[v] > dist3[u]+t) {
dist3[v] = dist3[u]+t;
Q.push(v);
}
}
}
int ecc = 1e9;
for (auto vert: vertices) {
ecc = min(ecc, max(dist2[vert], dist3[vert]));
}
return make_pair(ecc, diam);
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
vvpii G(N, vpii());
vector<bool> visited(N, false);
for (int i = 0; i < M; i++) {
G[A[i]].push_back(make_pair(B[i], T[i]));
G[B[i]].push_back(make_pair(A[i], T[i]));
}
int CC = 0;
vi diam = {}, ecc = {}, dist1, dist2, dist3;
dist1 = dist2 = dist3 = vi(N, 1e9);
pii curCC;
for (int i = 0; i < N; i++) {
if (!visited[i]) {
curCC = calcDist(i, G, visited, dist1, dist2, dist3);
ecc.push_back(curCC.first);
diam.push_back(curCC.second);
CC++;
}
}
for (int i = 1; i < CC; i++) {
if (ecc[i] > ecc[0])
swap(ecc[0], ecc[i]);
}
if (CC > 1) {
for (int i = 2; i < CC; i++) {
if (ecc[i] > ecc[1])
swap(ecc[i], ecc[1]);
}
}
if (CC > 2) {
for (int i = 3; i < CC; i++) {
if (ecc[i] > ecc[2])
swap(ecc[i], ecc[2]);
}
}
int ans = 0;
if (CC > 1) {
ans = ecc[0]+ecc[1]+L;
if (CC > 2)
ans = max(ans, ecc[1]+ecc[2]+2*L);
}
for (int i = 0; i < CC; i++) {
ans = max(ans, diam[i]);
}
return ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |