#include "dreaming.h"
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
vector<pair<int, int> > adj[100005];
int go[100005];
int cost[1000005] = {0};
int vis[1000005] = {0};
int n;
int id = 0, max_path = 0;
// grap bolgonii median max
vector<int> median;
void dfs(int u, int x, int path){
if(max_path <= path) {
id = u;
max_path = path;
}
vis[u] = 1;
for(int i = 0; i < adj[u].size(); i++){
int v, e;
v = adj[u][i].first;
e = adj[u][i].second;
if(v != x) {
dfs(v, u, path + e);
}
}
}
void dfss(int u, int x){
if(max_path <= cost[u]) {
id = u;
max_path = cost[u];
}
vis[u] = 1;
go[u] = x;
for(int i = 0; i < adj[u].size(); i++){
int v, e;
v = adj[u][i].first;
e = adj[u][i].second;
if(v != x) {
cost[v] = cost[u] + e;
dfss(v, u);
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n = N;
memset(go, -1, sizeof(go));
for(int i = 0; i < M; i++){
adj[A[i]].pb(mp(B[i], T[i]));
adj[B[i]].pb(mp(A[i], T[i]));
}
for(int i = 0; i < n; i++)
if(vis[i] == 0){
id = 0;
max_path = 0;
dfs(i, -1, 0);
max_path = 0;
dfss(id, -1);
int diameter = max_path;
int u = id;
int path = 1e9;
while(u != -1){
path = min(path, max(diameter - cost[u], cost[u]));
u = go[u];
}
median.pb(path);
}
sort(median.begin(), median.end());
int sz = median.size();
int res = 0;
res = max(res, median[sz - 1] + median[sz - 2] + L);
if(sz > 2) res = max(res, median[sz - 2] + median[sz - 3] + 2 * L);
return res;
}
#define MAX_N 100000
int A[MAX_N];
int B[MAX_N];
int T[MAX_N];
int main() {
int N, M, L, i;
cin >> N >> M >> L;
for (i = 0; i < M; i++)
cin >> A[i] >> B[i] >> T[i];
int answer = travelTime(N, M, L, A, B, T);
cout << answer;
return 0;
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int, int)':
dreaming.cpp:21:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for(int i = 0; i < adj[u].size(); i++){
| ~~^~~~~~~~~~~~~~~
dreaming.cpp: In function 'void dfss(int, int)':
dreaming.cpp:37:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i = 0; i < adj[u].size(); i++){
| ~~^~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:54:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
54 | for(int i = 0; i < n; i++)
| ^~~
dreaming.cpp:73:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
73 | sort(median.begin(), median.end());
| ^~~~
/tmp/cce1UA6i.o: In function `main':
dreaming.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccM3OBjj.o:grader.c:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status