This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/// dwuy: _,\,,,_\,__,\,,_
#include "dreaming.h"
#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) int32_t(s.length())
#define MASK(k)(1LL<<(k))
#define TASK ""
using namespace std;
typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;
const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MX = 100005;
struct DSU{
int n;
vector<int> e;
DSU(int n=0){
this->n = n;
this->e.assign(n+5, -1);
}
int fp(int u){
return e[u]<0? u : e[u] = fp(e[u]);
}
void unon(int u, int v){
u = fp(u);
v = fp(v);
if(u == v) return;
if(e[u] > e[v]) swap(u, v);
e[u] += e[v];
e[v] = u;
}
};
int n, m, l;
vector<pii> G[MX];
int dp[MX];
int dpDown[MX];
int mnPath[MX];
pii pf[MX];
pii sf[MX];
DSU dsu;
void calc_dp_down(int u, int p){
dpDown[u] = 0;
for(pii &tmp: G[u]){
int v, c; tie(v, c) = tmp;
if(v == p) continue;
calc_dp_down(v, u);
dpDown[u] = max(dpDown[u], dpDown[v] + c);
}
}
void calc_dp(int u, int p, int mx){
pii best1 = {mx, p};
pii best2 = {0, 0};
for(pii &tmp: G[u]){
int v, c; tie(v, c) = tmp;
if(v == p) continue;
int dis = dpDown[v] + c;
if(dis >= best1.fi){
best2 = best1;
best1 = {dis, v};
}
else best2 = max(best2, {dis, v});
}
dp[u] = best1.fi;
for(pii &tmp: G[u]){
int v, c; tie(v, c) = tmp;
if(v == p) continue;
if(v == best1.se) calc_dp(v, u, best2.fi + c);
else calc_dp(v, u, best1.fi + c);
}
}
pii combine(pii a, pii b){
if(a.fi < b.fi) swap(a, b);
if(a.se > b.fi) return a;
return {a.fi, b.fi};
}
int travelTime(int N, int M, int L, int A[], int B[], int C[]){
tie(n, m, l) = {N, M, L};
dsu = DSU(n);
for(int i=0; i<m; i++){
int u = A[i]; u++;
int v = B[i]; v++;
int c = C[i];
G[u].push_back({v, c});
G[v].push_back({u, c});
dsu.unon(u, v);
}
memset(dpDown, -1, sizeof dpDown);
memset(dp, -1, sizeof dp);
for(int i=1; i<=n; i++) if(dpDown[i] == -1) calc_dp_down(i, 0);
for(int i=1; i<=n; i++) if(dp[i] == -1) calc_dp(i, 0, 0);
for(int i=0; i<=n+1; i++){
pf[i] = {-1, -1};
sf[i] = {-1, -1};
}
memset(mnPath, 0x3f, sizeof mnPath);
for(int i=1; i<=n; i++){
int root = dsu.fp(i);
mnPath[root] = min(mnPath[root], dp[i]);
}
for(int i=1; i<=n; i++) if(dsu.fp(i) != i) mnPath[i] = -1;
for(int i=1; i<=n; i++){
pf[i] = {mnPath[i], -1};
pf[i] = combine(pf[i-1], pf[i]);
}
for(int i=n; i>=1; i--){
if(dsu.fp(i) == i) sf[i] = {mnPath[i], -1};
else sf[i] = {-1, -1};
sf[i] = combine(sf[i+1], sf[i]);
}
int ans = INF;
if(-dsu.e[dsu.fp(1)] != n){
for(int i=1; i<=n; i++) if(dsu.fp(i) == i){
pii best = combine(pf[i-1], sf[i+1]);
if(best.fi != -1 && best.se != -1) ans = min(ans, max(best.fi + best.se + l + l, best.fi + mnPath[i] + l));
else ans = min(ans, best.fi + mnPath[i] + l);
}
}
else ans = 0;
for(int i=1; i<=n; i++) ans = max(ans, dp[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... |