이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "dreaming.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define nmax 100008
const int inf = 1e9 + 7;
using namespace std;
typedef pair<int, int> pii;
pii operator+(pii x, int y){
return (x.fi <= y) ? make_pair(y, x.fi) : ((x.se < y) ? make_pair(x.fi, y) : x);
}
pii operator+(pii x, pii y){
return (x + y.fi) + y.se;
}
vector<pii>vt, l[nmax];
bool vis[nmax];
int n, d = 0, ans = inf, diameter = 0, dis[2][nmax];
stack<int>s;
int a[nmax];
pii pf[nmax], sf[nmax];
vector<int>his;
void bfs(int root, int id, int k){
for (int tmp : his)
dis[id][tmp] = -1;
s.push(root);
dis[id][root] = 0;
while (!s.empty()){
int u = s.top();
s.pop();
if (k)
his.push_back(u);
vis[u] = true;
for (pii it : l[u])
if (dis[id][it.fi] == -1){
dis[id][it.fi] = dis[id][u] + it.se;
s.push(it.fi);
}
}
}
int calc(int rt){
for (int tmp : his)
dis[0][tmp] = dis[1][tmp] = -1;
his.clear();
bfs(rt, 0, 1);
int u = 0, v = 0, res = inf;
for (int i : his)
if (dis[0][u] < dis[0][i])
u = i;
bfs(u, 1, 0);
for (int i : his)
if (dis[1][v] < dis[1][i])
v = i;
bfs(v, 0, 0);
for (int i : his)
res = min(res, max(dis[0][i], dis[1][i]));
diameter = max(diameter, dis[0][u]);
return res;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
memset(dis, -1, sizeof(dis));
n = N;
for (int i = 0; i < M; i++){
int u = A[i], v = B[i], w = T[i];
l[u].push_back({v, w});
l[v].push_back({u, w});
}
// assert(M == N - 2);
fill(vis, vis + n + 1, false);
pf[0] = {-inf, -inf};
for (int i = 0; i < N; i++)
if (!vis[i]){
a[++d] = calc(i);
pf[d] = pf[d - 1] + a[d];
}
if (M == N - 2)
return max(diameter, a[2] + a[1] + L);
sf[d + 1] = {-inf, -inf};
for (int i = d; i > 0; i--)
sf[i] = sf[i + 1] + a[i];
for (int i = 1; i <= d; i++){
pii cur = pf[i - 1] + sf[i + 1];
ans = min(ans, max({diameter, cur.fi + cur.se + L * 2, cur.fi + a[i] + L}));
}
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... |