#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <set>
#include <iostream>
//#include "dreaming.h"
#define ve vector
#define all(x) x.begin(), x.end()
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 1e5+10;
ve<pii> g[MAXN];
bool vis[MAXN];
int dp[MAXN];
void calcdst(int v){
vis[v] = 1;
dp[v] = 0;
for(auto to : g[v])
if(!vis[to.fi]){
calcdst(to.fi);
dp[v] = max(dp[v], dp[to.fi] + to.se);
}
}
int dfs(int v, int p, int curmx){
ve<int> dsts = {curmx};
for(auto& to : g[v])
if(to.fi != p)
dsts.push_back(dp[to.fi] + to.se);
sort(all(dsts), greater<int>());
int res = dsts[0];
multiset<int, greater<int> > cdst;
for(auto i : dsts)
cdst.insert(i);
for(auto& to : g[v])
if(to.fi != p){
cdst.erase(cdst.find(dp[to.fi] + to.se));
res = min(res, dfs(to.fi, v, *cdst.begin() + to.se));
cdst.insert(dp[to.fi] + to.se);
}
return res;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0; i < M; i++){
int u = A[i], v = B[i], w = T[i];
g[u].push_back({v, w});
g[v].push_back({u, w});
}
ve<int> v;
for(int i = 0; i < N; i++)
if(!vis[i]){
calcdst(i);
v.push_back(dfs(i, i, 0));
}
sort(all(v), greater<int>());
if(v.size() == 1)
return v[0];
else if(v.size() == 2)
return v[0] + v[1] + L;
else
return max(v[0] + v[1] + L, v[1] + v[2] + 2*L);
}
#define fail(s, x...) do { \
fprintf(stderr, s "\n", ## x); \
exit(1); \
} while(0)
#define MAX_N 100000
static int A[MAX_N];
static int B[MAX_N];
static int T[MAX_N];
int main() {
int N, M, L, i;
int res;
FILE *f = fopen("dreaming.in", "r");
if (!f)
fail("Failed to open input file.");
res = fscanf(f, "%d%d%d", &N, &M, &L);
for (i = 0; i < M; i++)
res = fscanf(f, "%d%d%d", &A[i], &B[i], &T[i]);
fclose(f);
int answer = travelTime(N, M, L, A, B, T);
printf("%d\n", answer);
return 0;
}
Compilation message
dreaming.cpp: In function 'int main()':
dreaming.cpp:93:9: warning: variable 'res' set but not used [-Wunused-but-set-variable]
93 | int res;
| ^~~
/usr/bin/ld: /tmp/ccoJKUIl.o: in function `main':
dreaming.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cc3RPerk.o:grader.c:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/cc3RPerk.o: in function `main':
grader.c:(.text.startup+0xd1): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status