#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#include "dreaming.h"
const int maxn = 1e4 + 5;
vector<pair<int,int>> G[maxn];
vector<int> vis(maxn, 0);
vector<int> dp(maxn, 0);
int getDiameter(int n, int st) {
queue<int> q;
vector<int> dis(maxn, -1);
q.push(st);
dis[st] = 0;
while (q.size()) {
int u = q.front();
q.pop();
for (auto e : G[u]) {
int v = e.first;
if (dis[v] == -1) {
dis[v] = dis[u] + e.second;
q.push(v);
}
}
}
int now = 0, mx = 0;
for (int i = 1; i <= n; i++) {
if (mx < dis[i]) {
mx = dis[i];
now = i;
}
dis[i] = -1;
}
q.push(now);
dis[now] = 0;
while (q.size()) {
int u = q.front();
q.pop();
for (auto e : G[u]) {
int v = e.first;
if (dis[v] == -1) {
dis[v] = dis[u] + e.second;
q.push(v);
}
}
}
return *max_element(dis.begin(), dis.end());
}
int dfs(int now, int fa) {
int mx = 0;
for (auto e : G[now]) {
if (e.first != fa) {
mx = max(mx, dfs(e.first, now) + e.second);
}
}
return mx;
}
int dfs1(int now, int fa) {
int mx = dp[now];
vis[now] = true;
for (auto e : G[now]) {
if (e.first != fa) {
mx = min(dfs1(e.first, now), mx);
}
}
return mx;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for (int i = 0; i < M; i++) {
// cout << A[i] << " " << B[i];
A[i]++;
B[i]++;
G[A[i]].emplace_back(B[i], T[i]);
G[B[i]].emplace_back(A[i], T[i]);
}
if (N == 1) {
return 0;
}
if (N == M + 1) {
return getDiameter(N, 1);
}
if (N == 2) {
return L;
}
if (M == 0) {
return L * 2;
}
for (int i = 1; i <= N; i++) {
dp[i] = dfs(i, 0);
}
int ans = 0, mx = 0;
for (int i = 1; i <= N; i++) {
if (!vis[i]) {
// cout << i - 1 << " " << dfs1(i, 0) << "\n";
ans = ans + dfs1(i, 0);
mx = max(mx, getDiameter(N, i));
}
}
return max(ans + L, mx);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
17 ms |
3160 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
616 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
17 ms |
3160 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
7 ms |
1884 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
616 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
600 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
5 ms |
860 KB |
Output is correct |
28 |
Correct |
7 ms |
812 KB |
Output is correct |
29 |
Incorrect |
3 ms |
860 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
17 ms |
3160 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |