This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <dreaming.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int, int> > vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
#define INF 0x3f3f3f3f
#define MOD 1000000007LL
#define EPSILON 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define FOR(i, a, b) for (ll i=(a); i<=(signed)(b); i++)
#define F0R(i, a) for (ll i=0; i<(signed)(a); i++)
#define RFOR(i, a, b) for (ll i=(a); i >= b; i--)
#define MN 100005
vpii e[MN];
int vis[MN];
pii far(int cn, int p){ //{dist, node}
vis[cn] = true;
pii ans = {0, cn};
for(auto nn : e[cn]) if(nn.f != p){
pii res = far(nn.f, cn);
ans = max(ans, {res.f+nn.s, res.s});
}
return ans;
}
int d[2][MN];
int cmn;
void gda(int t, int cn, int p){
if(t == 1){
cmn = min(cmn, max(d[0][cn], d[1][cn]));
}
for(auto nn : e[cn]) if(nn.f != p){
d[t][nn.f] = d[t][cn]+nn.s;
gda(t, nn.f, cn);
}
}
int diam(int x){
pii res1 = far(x, 0);
pii res2 = far(res1.s, 0);
//return {res2.f, {res1.s, res2.s}};
cmn = INF;
gda(0, res1.s, 0);
gda(1, res2.s, 0);
return cmn;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]){
int n = N; int m = M; int l = L;
F0R(i, m){
e[A[i]].pb({B[i], T[i]});
e[B[i]].pb({A[i], T[i]});
}
vi hmm;
F0R(i, n){
if(!vis[i]){
hmm.pb(diam(i));
}
}
sort(hmm.begin(), hmm.end(), greater<int>() );
if(hmm.size() == 1){
return hmm[0];
} else if(hmm.size() == 2){
return hmm[0]+hmm[1]+l;
} else{
return max(hmm[0]+hmm[1]+l, hmm[1]+hmm[2]+l+l);
}
}
/*int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, M, L;
cin >> N >> M >> L;
int A[M], B[M], T[M];
F0R(i, M) cin >> A[i] >> B[i] >> T[i];
cout << travelTime(N, M, L, A, B, T) << "\n";
return 0;
}*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |