#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 kt[nmax], 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);
kt[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)
kt[tmp] = false;
his.clear();
bfs(rt, 0, 1);
int u = 0, v = 0, res = inf;
for (int i = 1; i < n; i++)
if (dis[0][u] < dis[0][i])
u = i;
bfs(u, 1, 0);
for (int i = 1; i < n; i++)
if (dis[1][v] < dis[1][i])
v = i;
bfs(v, 0, 0);
for (int i = 0; i < n; i++)
if (kt[i])
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);
return 0;
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 |
1 |
Correct |
38 ms |
8140 KB |
Output is correct |
2 |
Correct |
47 ms |
8128 KB |
Output is correct |
3 |
Correct |
33 ms |
6472 KB |
Output is correct |
4 |
Correct |
7 ms |
4184 KB |
Output is correct |
5 |
Correct |
8 ms |
3900 KB |
Output is correct |
6 |
Correct |
15 ms |
4484 KB |
Output is correct |
7 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
8140 KB |
Output is correct |
2 |
Correct |
47 ms |
8128 KB |
Output is correct |
3 |
Correct |
33 ms |
6472 KB |
Output is correct |
4 |
Correct |
7 ms |
4184 KB |
Output is correct |
5 |
Correct |
8 ms |
3900 KB |
Output is correct |
6 |
Correct |
15 ms |
4484 KB |
Output is correct |
7 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1065 ms |
5964 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
8140 KB |
Output is correct |
2 |
Correct |
47 ms |
8128 KB |
Output is correct |
3 |
Correct |
33 ms |
6472 KB |
Output is correct |
4 |
Correct |
7 ms |
4184 KB |
Output is correct |
5 |
Correct |
8 ms |
3900 KB |
Output is correct |
6 |
Correct |
15 ms |
4484 KB |
Output is correct |
7 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |