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 "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
const int mxN = 1e5 + 1;
vector<pii> adj[mxN];
bool vis[mxN];
int dist[mxN], dist2[mxN];
pii dfs(int u, int par = -1) {
vis[u] = true;
pii res = {u, 0};
for (pii &v : adj[u]) {
if (v.ff != par) {
pii ret = dfs(v.ff, u);
ret.ss += v.ss;
if (ret.ss > res.ss) {
res = ret;
}
}
}
return res;
}
int FindRadii(int lhs, int rhs) {
priority_queue<pii, vector<pii>, greater<pii>> pq;
vector<int> nodes;
// from lhs
pq.push({lhs, 0});
dist[lhs] = 0;
nodes.pb(lhs);
while (!pq.empty()) {
pii cur = pq.top();
pq.pop();
if (cur.ss > dist[cur.ff]) continue;
nodes.pb(cur.ff);
for (pii &v : adj[cur.ff]) {
if (dist[v.ff] > cur.ss + v.ss) {
dist[v.ff] = cur.ss + v.ss;
pq.push({v.ff, dist[v.ff]});
}
}
}
// from rhs
pq.push({rhs, 0});
dist2[rhs] = 0;
while (!pq.empty()) {
pii cur = pq.top();
pq.pop();
if (cur.ss > dist2[cur.ff]) continue;
for (pii &v : adj[cur.ff]) {
if (dist2[v.ff] > cur.ss + v.ss) {
dist2[v.ff] = cur.ss + v.ss;
pq.push({v.ff, dist2[v.ff]});
}
}
}
int ans = 2e9;
for (int &x : nodes) {
ans = min(ans, max(dist[x], dist2[x]));
}
return ans;
}
int travelTime(int n, int m, int L, int A[], int B[], int T[]) {
for (int i = 0; i < m; i++) {
A[i]++; B[i]++;
adj[A[i]].pb({B[i], T[i]});
adj[B[i]].pb({A[i], T[i]});
}
for (int i = 1; i <= n; i++) {
dist[i] = dist2[i] = 2e9;
}
vector<int> fin;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
pii lhs = dfs(i);
pii rhs = dfs(lhs.ff);
fin.pb(FindRadii(lhs.ff, rhs.ff));
}
}
sort(fin.begin(), fin.end(), greater<int>());
if (fin.size() == 1) return fin[0];
else if (fin.size() == 2) return fin[0] + fin[1] + L;
else return max(fin[0] + fin[1] + L, fin[1] + fin[2] + L * 2);
}
# | 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... |