#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vc vector
typedef vc<ll> vcll;
#define pr pair
typedef pr<ll, ll> prll;
#define f0r(i,a,n) for (i=a; i<n; i++)
#define f1r(i,a,n) for (i=a; i<=n; i++)
#define r0f(i,n,a) for (i=n; i>a; i--)
#define r1f(i,n,a) for (i=n; i>=a; i--)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define in(s, x) ((s).find((x)) != (s).end())
#define all(x) (x).begin(), (x).end()
#define allrev(x) (x).rbegin(), (x).rend()
inline void maxa(ll &a, ll b) { if (a<b) a=b; }
inline void mina(ll &a, ll b) { if (a>b) a=b; }
#define INF (LLONG_MAX>>4ll)
#define mxN 100'010ll
ll n, m, l;
int *uar, *var, *war;
vc<vc<prll>> gr(mxN); // fi is w, se is v
vcll ar;
inline void build_gr()
{
ll i;
f0r(i,0,m)
{
ll u, v, w;
u=uar[i];
v=var[i];
w=war[i];
gr[u].pb({w, v});
gr[v].pb({w, u});
}
}
vc<bool> seen(mxN, 0);
map<prll, ll> from_to; // the "to" gives the value
inline void init_umap()
{
ll u;
f0r(u,0,n)
{
from_to[mp(-1,u)]=-1;
for (prll edge : gr[u])
{
ll v = edge.se;
from_to[mp(u,v)]=-1;
from_to[mp(v,u)]=-1;
}
}
}
vcll comp; // comp for component
void dfs(ll u)
{
if (seen[u]) return;
// printf("real, u: %lli\n", u);
seen[u]=1;
comp.pb(u);
for (prll edge : gr[u])
{
ll v=edge.se;
dfs(v);
}
}
ll calc_from_to(ll p, ll u)
{
ll &t = from_to[mp(p,u)];
// if (t!=-1) printf("p: %lli, u: %lli, t: %lli\n", p, u, t);
if (t!=-1) return t;
ll m=0; // max
for (prll edge : gr[u])
{
ll v=edge.se;
if (v==p) continue;
ll w=edge.fi;
maxa(m, calc_from_to(u,v)+w);
}
// printf("res: %lli\n", m);
return t=m;
}
inline ll minmaxdist(ll u)
{
comp.clear();
dfs(u);
// printf("comp.size: %lli\n", (ll)comp.size());
ll m=INF; // min
for (ll u : comp)
{
mina(m, calc_from_to(-1, u));
// printf("min: %lli\n", m);
}
// printf("true\n");
return m;
}
inline void build_ar()
{
init_umap();
ll u;
f0r(u,0,n)
{
if (seen[u]) continue;
ar.pb(minmaxdist(u));
// printf("ar.back(): %lli\n", ar.back());
}
}
inline ll value()
{
ll z=ar.size();
// printf("z: %lli\n", z);
if (z==1) return ar[0];
if (z==2) return ar[0]+ar[1]+l;
return max(ar[0]+ar[1]+l, ar[1]+ar[2]+(2*l));
}
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
n=N;
m=M;
l=L;
uar=A;
var=B;
war=T;
build_gr();
build_ar();
sort(allrev(ar));
return value();
}
/*
int main()
{
freopen("dreaming.in", "r", stdin);
int N, M, L, *A, *B, *T;
A=new int[mxN];
B=new int[mxN];
T=new int[mxN];
cin >> N >> M >> L;
ll i;
f0r(i,0,M) cin >> A[i] >> B[i] >> T[i];
int v = travelTime(N, M, L, A, B, T);
printf("ans: %i\n", v);
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
308 ms |
32196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
308 ms |
32196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
15348 KB |
Output is correct |
2 |
Correct |
82 ms |
15528 KB |
Output is correct |
3 |
Correct |
95 ms |
15484 KB |
Output is correct |
4 |
Correct |
97 ms |
15508 KB |
Output is correct |
5 |
Correct |
81 ms |
15316 KB |
Output is correct |
6 |
Correct |
88 ms |
16576 KB |
Output is correct |
7 |
Correct |
87 ms |
15928 KB |
Output is correct |
8 |
Correct |
90 ms |
15204 KB |
Output is correct |
9 |
Correct |
93 ms |
15180 KB |
Output is correct |
10 |
Correct |
81 ms |
15888 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
28 ms |
10196 KB |
Output is correct |
13 |
Correct |
23 ms |
10192 KB |
Output is correct |
14 |
Correct |
27 ms |
10204 KB |
Output is correct |
15 |
Correct |
24 ms |
10192 KB |
Output is correct |
16 |
Correct |
22 ms |
10204 KB |
Output is correct |
17 |
Correct |
27 ms |
10204 KB |
Output is correct |
18 |
Correct |
24 ms |
10196 KB |
Output is correct |
19 |
Correct |
23 ms |
10160 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
24 ms |
10292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
308 ms |
32196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |