#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
int n,m;
int mem[maxn];
int head[maxn];
long long len;
long long mx[5];
pair<int,int> rec[maxn];
vector<pair<int,int> > from[maxn];
vector<int> a[maxn];
int findhead(int x) {
if(head[x]==x) return x;
return head[x] = findhead(head[x]);
}
void dfs(int x,int last) {
for(int i=0;i<from[x].size();i++) {
if(from[x][i].first!=last) {
mem[from[x][i].first] = mem[x] + from[x][i].second;
rec[from[x][i].first] = {x,from[x][i].second};
dfs(from[x][i].first,x);
}
}
}
pair<long long,long long> get(int x) {
int now;
long long path,dist;
if(a[x].size()==0) return {0,0};
now = a[x][0];
memset(mem,0,sizeof(mem));
dfs(now,0);
for(int i=0;i<a[x].size();i++) if(mem[a[x][i]] > mem[now]) now = a[x][i];
memset(mem,0,sizeof(mem));
dfs(now,0);
for(int i=0;i<a[x].size();i++) if(mem[a[x][i]] > mem[now]) now = a[x][i];
path = mem[now];
dist = 0;
while(1) {
if(dist+rec[now].second >= path/2) break;
dist += rec[now].second;
now = rec[now].first;
}
// printf("%d : %lld %lld => {%lld, %lld}\n",x,path,dist,path,min(max(dist,path-dist),max(dist+rec[now].second,path-dist-rec[now].second)));
return {path,min(max(dist,path-dist),max(dist+rec[now].second,path-dist-rec[now].second))};
}
int travelTime(int N,int M,int L,int A[],int B[],int T[]) {
int i,j,x,y,val;
long long ans,sum;
pair<long long,long long> t;
n = N; m = M; len = L;
for(i=1;i<=n;i++) head[i] = i;
for(i=0;i<m;i++) {
x = A[i]; y = B[i]; val = T[i];
x++; y++;
head[findhead(x)] = findhead(y);
from[x].push_back({y,val}); from[y].push_back({x,val});
}
for(i=1;i<=n;i++) a[findhead(i)].push_back(i);
ans = 0;
for(j=0;j<3;j++) mx[j] = -(long long)1e18;
for(i=1;i<=n;i++) {
t = get(i);
printf("%d",1/0);
ans = max(ans,t.first);
for(j=0;j<3;j++) if(mx[j]<=t.second) swap(mx[j],t.second);
}
printf("%d",max(ans,max(mx[0]+mx[1]+len,mx[1]+mx[2]+len*2)));
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:17:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<from[x].size();i++) {
~^~~~~~~~~~~~~~~
dreaming.cpp: In function 'std::pair<long long int, long long int> get(int)':
dreaming.cpp:32:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[x].size();i++) if(mem[a[x][i]] > mem[now]) now = a[x][i];
~^~~~~~~~~~~~
dreaming.cpp:35:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[x].size();i++) if(mem[a[x][i]] > mem[now]) now = a[x][i];
~^~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:63:22: warning: division by zero [-Wdiv-by-zero]
printf("%d",1/0);
~^~
dreaming.cpp:67:64: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
printf("%d",max(ans,max(mx[0]+mx[1]+len,mx[1]+mx[2]+len*2)));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
dreaming.cpp:48:19: warning: unused variable 'sum' [-Wunused-variable]
long long ans,sum;
^~~
dreaming.cpp:68:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
/tmp/cc5jWs4M.o: In function `main':
grader.c:(.text.startup+0xa2): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status