#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std ;
#define pii pair <int , int>
#define ff first
#define ss second
const int inf = 1e9 ;
const int N = 1e5 + 5 ;
template <class T>
bool chmax( T& x , const T& y ){
bool f = 0 ;
if ( x < y ) x = y , f = 1 ;
return f ;
}
template <class T>
bool chmin( T &x , const T &y ){
bool f = 0 ;
if ( x > y ) x = y , f = 1 ;
return f ;
}
bool vis[N] ;
int d[N] , f[N] , sc[N] ;
int mx , m , ind , r ;
pii ans[N] ;
vector <pii> g[N] ;
vector <pii> vec ;
void dfs ( int v , int s , int& m ){
vis[v] = 1 ; d[v] = s ;
if ( mx <= d[v] ){
m = v ; mx = d[v] ;
}
for ( auto to : g[v] ) if ( !vis[to.ff] ) dfs(to.ff,s+to.ss,m) ;
}
void fir ( int v , int d , int& m ){
vis[v] = 0 ; f[v] = d ;
if ( chmax ( mx , f[v] ) ) m = v ;
for ( auto to : g[v] ) if ( vis[to.ff] ) fir(to.ff,d+to.ss,m) ;
}
void sec ( int v , int d , int& m ){
vis[v] = 1 ; sc[v] = d ;
if ( max( f[v] , sc[v] ) < mx ){
mx = max ( f[v] , sc[v] ) ; m = v ;
}
for ( auto to : g[v] ) if ( !vis[to.ff] ) sec(to.ff,d+to.ss,m) ;
}
int travelTime(int n, int m, int l, int a[], int b[], int c[]) {
for ( int i = 0 ; i < m ; i ++ ){
g[a[i]].push_back({b[i],c[i]}) ;
g[b[i]].push_back({a[i],c[i]}) ;
}
for ( int i = 1 ; i <= n ; i ++ ){
if ( !vis[i] ){
mx = 0 ;
dfs(i,0,m) ;
mx = 0 ;
fir(m,0,m) ;
chmax ( r , f[m] ) ;
mx = inf ;
sec(m,0,m) ;
vec.push_back({max(f[m],sc[m]),m}) ;
}
} vec.push_back({0,0}) ; vec.push_back({0,0}) ; vec.push_back({0,0}) ;
sort ( vec.rbegin() , vec.rend() ) ;
return max({ r, vec[0].ff + vec[1].ff + l , vec[1].ff + vec[2].ff + 2*l }) ;
}
/*
signed main(){
int n , m , l , a[N] , b[N] , t[N] ;
cin >> n >> m >> l ;
for ( int i = 0 ; i < m ; i ++ ) cin >> a[i] >> b[i] >> t[i] ;
cout << travelTime( n , m , l , a , b , t ) ;
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
12892 KB |
Output is correct |
2 |
Correct |
31 ms |
12888 KB |
Output is correct |
3 |
Correct |
20 ms |
10072 KB |
Output is correct |
4 |
Correct |
5 ms |
5720 KB |
Output is correct |
5 |
Correct |
4 ms |
5212 KB |
Output is correct |
6 |
Correct |
9 ms |
6236 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
12892 KB |
Output is correct |
2 |
Correct |
31 ms |
12888 KB |
Output is correct |
3 |
Correct |
20 ms |
10072 KB |
Output is correct |
4 |
Correct |
5 ms |
5720 KB |
Output is correct |
5 |
Correct |
4 ms |
5212 KB |
Output is correct |
6 |
Correct |
9 ms |
6236 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
8412 KB |
Output is correct |
2 |
Correct |
19 ms |
8408 KB |
Output is correct |
3 |
Correct |
22 ms |
8416 KB |
Output is correct |
4 |
Correct |
24 ms |
8480 KB |
Output is correct |
5 |
Correct |
16 ms |
8412 KB |
Output is correct |
6 |
Correct |
18 ms |
9180 KB |
Output is correct |
7 |
Correct |
18 ms |
8412 KB |
Output is correct |
8 |
Correct |
16 ms |
8416 KB |
Output is correct |
9 |
Correct |
16 ms |
8288 KB |
Output is correct |
10 |
Correct |
18 ms |
8408 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
16 ms |
6868 KB |
Output is correct |
13 |
Correct |
13 ms |
6868 KB |
Output is correct |
14 |
Correct |
14 ms |
6868 KB |
Output is correct |
15 |
Correct |
14 ms |
6868 KB |
Output is correct |
16 |
Correct |
17 ms |
6868 KB |
Output is correct |
17 |
Correct |
17 ms |
6620 KB |
Output is correct |
18 |
Correct |
11 ms |
6864 KB |
Output is correct |
19 |
Correct |
14 ms |
6868 KB |
Output is correct |
20 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
12892 KB |
Output is correct |
2 |
Correct |
31 ms |
12888 KB |
Output is correct |
3 |
Correct |
20 ms |
10072 KB |
Output is correct |
4 |
Correct |
5 ms |
5720 KB |
Output is correct |
5 |
Correct |
4 ms |
5212 KB |
Output is correct |
6 |
Correct |
9 ms |
6236 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |