// 人外有人,天外有天
// author: Ausp3x
#pragma GCC optimize("O1, O2, O3, Ofast, unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include "dreaming.h"
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define pb push_back
// #define DEBUG
typedef long long lng;
int const INF32 = 0x3f3f3f3f;
lng const INF64 = 0x3f3f3f3f3f3f3f3f;
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
vector<vector<pair<int, int>>> adjl(N);
for (int i = 0; i < M; i++) {
adjl[A[i]].pb({B[i], T[i]});
adjl[B[i]].pb({A[i], T[i]});
}
vector<int> vst(N);
vector<lng> W;
for (int i = 0; i < N; i++) {
if (vst[i])
continue;
if (adjl[i].empty()) {
W.pb(0);
continue;
}
int end1 = i;
lng max_dis = 0;
queue<pair<int, lng>> bfs;
bfs.push({i, 0});
while (!bfs.empty()) {
auto [cur, cur_d] = bfs.front();
bfs.pop();
if (vst[cur] > 0)
continue;
vst[cur]++;
if (cur_d > max_dis) {
end1 = cur;
max_dis = cur_d;
}
for (auto [nxt, d] : adjl[cur]) {
if (vst[nxt] == 0)
bfs.push({nxt, cur_d + d});
}
}
map<int, pair<lng, lng>> dis_from_diameter_ends;
int end2 = end1;
max_dis = 0;
bfs.push({end1, 0});
while (!bfs.empty()) {
auto [cur, cur_d] = bfs.front();
bfs.pop();
if (vst[cur] > 1)
continue;
vst[cur]++;
if (cur_d > max_dis) {
end2 = cur;
max_dis = cur_d;
}
dis_from_diameter_ends[cur].fi = cur_d;
for (auto [nxt, d] : adjl[cur]) {
if (vst[nxt] == 1)
bfs.push({nxt, cur_d + d});
}
}
// cout << end1 << ' ' << end2 << endl;
bfs.push({end2, 0});
while (!bfs.empty()) {
auto [cur, cur_d] = bfs.front();
bfs.pop();
if (vst[cur] > 2)
continue;
vst[cur]++;
dis_from_diameter_ends[cur].se = cur_d;
for (auto [nxt, d] : adjl[cur]) {
if (vst[nxt] == 2)
bfs.push({nxt, cur_d + d});
}
}
lng w = INF64;
for (auto [k, v] : dis_from_diameter_ends) {
// cout << v.fi << ' ' << v.se << endl;
w = min(w, max(v.fi, v.se));
}
// cout << w << endl;
W.pb(w);
}
sort(W.begin(), W.end(), greater<lng>());
// for (int x : W)
// cout << x << ' ';
// cout << endl;
if (W.size() == 1) {
return W[0];
}
if (W.size() == 2) {
return W[0] + W[1] + L;
}
return max(W[0] + W[1] + L, W[1] + W[2] + 2 * L);
}
#ifdef DEBUG
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
int N, M, L;
cin >> N >> M >> L;
int A[M], B[M], T[M];
for (int i = 0; i < M; i++)
cin >> A[i] >> B[i] >> T[i];
cout << travelTime(N, M, L, A, B, T) << endl;
}
return 0;
}
#endif
Compilation message
dreaming.cpp:4:55: warning: bad option '-f O2' to pragma 'optimize' [-Wpragmas]
4 | #pragma GCC optimize("O1, O2, O3, Ofast, unroll-loops")
| ^
dreaming.cpp:4:55: warning: bad option '-f O3' to pragma 'optimize' [-Wpragmas]
dreaming.cpp:4:55: warning: bad option '-f Ofast' to pragma 'optimize' [-Wpragmas]
dreaming.cpp:4:55: warning: bad option '-f unroll-loops' to pragma 'optimize' [-Wpragmas]
In file included from dreaming.cpp:7:
dreaming.h:8:62: warning: bad option '-f O2' to attribute 'optimize' [-Wattributes]
8 | int travelTime(int N, int M, int L, int A[], int B[], int T[]);
| ^
dreaming.h:8:62: warning: bad option '-f O3' to attribute 'optimize' [-Wattributes]
dreaming.h:8:62: warning: bad option '-f Ofast' to attribute 'optimize' [-Wattributes]
dreaming.h:8:62: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
dreaming.h:8:62: warning: bad option '-f O2' to attribute 'optimize' [-Wattributes]
dreaming.h:8:62: warning: bad option '-f O3' to attribute 'optimize' [-Wattributes]
dreaming.h:8:62: warning: bad option '-f Ofast' to attribute 'optimize' [-Wattributes]
dreaming.h:8:62: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
dreaming.cpp:20:62: warning: bad option '-f O2' to attribute 'optimize' [-Wattributes]
20 | int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
| ^
dreaming.cpp:20:62: warning: bad option '-f O3' to attribute 'optimize' [-Wattributes]
dreaming.cpp:20:62: warning: bad option '-f Ofast' to attribute 'optimize' [-Wattributes]
dreaming.cpp:20:62: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
77 ms |
12512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
77 ms |
12512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
5948 KB |
Output is correct |
2 |
Correct |
19 ms |
6432 KB |
Output is correct |
3 |
Correct |
19 ms |
6164 KB |
Output is correct |
4 |
Correct |
20 ms |
6364 KB |
Output is correct |
5 |
Correct |
19 ms |
6364 KB |
Output is correct |
6 |
Correct |
20 ms |
7100 KB |
Output is correct |
7 |
Correct |
20 ms |
6608 KB |
Output is correct |
8 |
Correct |
19 ms |
6108 KB |
Output is correct |
9 |
Correct |
19 ms |
6104 KB |
Output is correct |
10 |
Correct |
20 ms |
6484 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
3 ms |
4332 KB |
Output is correct |
13 |
Correct |
3 ms |
4308 KB |
Output is correct |
14 |
Correct |
3 ms |
4308 KB |
Output is correct |
15 |
Correct |
3 ms |
4304 KB |
Output is correct |
16 |
Correct |
3 ms |
4308 KB |
Output is correct |
17 |
Correct |
3 ms |
4308 KB |
Output is correct |
18 |
Correct |
3 ms |
4308 KB |
Output is correct |
19 |
Correct |
3 ms |
4308 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
4308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
77 ms |
12512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |