#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
struct ura{
int x, c;
};
vector <ura> g[N];
int maxi = 2e9, dp[N], dp2[N], dp_up[N];
int lover[N];
int viz[N];
vector <int> A;
void dfs(int nod, int dad = -1)
{
viz[nod] = 1;
for (auto son : g[nod])
{
if (son.x == dad)
continue;
dfs(son.x, nod);
if (dp[nod] < dp[son.x] + son.c)
{
dp2[nod] = dp[nod];
dp[nod] = dp[son.x] + son.c;
}
else
dp2[nod] = max(dp2[nod], dp[son.x] + son.c);
}
}
void dfs2(int nod, int dad = -1)
{
for (auto son : g[nod])
{
if (son.x == dad)
continue;
int c = dp[nod];
if (c == dp[son.x] + son.c)
c = dp2[nod];
dp_up[son.x] = max(dp_up[nod], c) + son.c;
dfs2(son.x, nod);
}
lover[nod] = max(dp_up[nod], dp[nod]);
maxi = min(lover[nod], maxi);
}
int dist[N], bebe = -1, maxim;
void dfs_diametru(int nod, int dad = -1)
{
for (auto son : g[nod])
{
if (son.x == dad)
continue;
dist[son.x] = dist[nod] + son.c;
if (dist[son.x] > maxim)
{
maxim = dist[son.x];
bebe = son.x;
}
dfs_diametru(son.x, nod);
}
}
void dfs_clear(int nod, int dad = -1)
{
dist[nod] = 0;
for (auto son : g[nod])
{
if (son.x == dad)
continue;
dfs_clear(son.x, nod);
}
}
int travelTime(int n, int m, int l, int a[], int b[], int t[])
{
int i, j;
for (i = 0; i < m; i++)
{
g[a[i]].push_back({b[i], t[i]});
g[b[i]].push_back({a[i], t[i]});
}
int c = 0;
for (i = 0; i < n; i++)
{
if (viz[i])
continue;
maxi = 2e9;
dfs(i);
dfs2(i);
maxim = 0;
A.push_back(maxi);
bebe = -1;
dfs_diametru(i);
dfs_clear(i);
if (bebe == -1)
continue;
dfs_diametru(bebe);
c = max(c, dist[bebe]);
}
if (A.size() == 1)
return max(c, A[0]);
sort(A.begin(), A.end());
reverse(A.begin(), A.end());
c = max(c, A[0] + A[1] + l);
if (A.size() == 2)
return c;
else
return max(c, A[1] + A[2] + 2 * l);
}
int a[N];
int b[N];
int t[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];
cout << travelTime(n, m, l, a, b, t);
return 0;
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:73:12: warning: unused variable 'j' [-Wunused-variable]
73 | int i, j;
| ^
/usr/bin/ld: /tmp/ccSt6vs0.o: in function `main':
dreaming.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccjZrOR0.o:grader.c:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccjZrOR0.o: in function `main':
grader.c:(.text.startup+0xd1): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status