Submission #142637

#TimeUsernameProblemLanguageResultExecution timeMemory
142637dantoh000Dreaming (IOI13_dreaming)C++14
100 / 100
110 ms10924 KiB
#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
typedef pair<int,int> ii;
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
    vector<ii> adjlist[N+1];
    for (int i = 0; i < M; i++){
        adjlist[A[i]].push_back(ii(B[i],T[i]));
        adjlist[B[i]].push_back(ii(A[i],T[i]));
    }
    int p[N+1]; memset(p,-1,sizeof(p));
    int d[N+1]; memset(d,0,sizeof(d));
    int p1[N+1];memset(p1,-1,sizeof(p1));
    int d1[N+1];memset(d1,0,sizeof(d1));
    int best1, best2, best3;
    best1 = best2 = best3 = -L;
    int ans = 0;
    for (int i = 0; i < N; i++){
        if (p[i] == -1){
            //printf("visiting %d comp\n",i);
            p[i] = i;
            d[i] = 0;
            int s = i;
            queue<int> q;
            q.push(i);
            while (q.size()){
                int u = q.front(); q.pop();
                for (auto v : adjlist[u]){
                    if (v.first == p[u]) continue;
                    p[v.first] = u;
                    d[v.first] = d[u]+v.second;
                    if (d[v.first] > d[s]){
                        s = v.first;
                    }
                    q.push(v.first);
                }
            }
            p1[s] = s;
            d1[s] = 0 ;
            q.push(s);
            int e = s;
            while (q.size()){
                int u = q.front(); q.pop();
                for (auto v : adjlist[u]){
                    if (v.first == p1[u]) continue;
                    p1[v.first] = u;
                    d1[v.first] = d1[u]+v.second;
                    if (d1[v.first] > d1[e]){
                        e = v.first;
                    }
                    q.push(v.first);
                }
            }
            int diameter = d1[e];
            ans = max(ans,diameter);
            int minimax = d1[e];
            int cur = e;
            while (cur != s){
                minimax = min(minimax,max(diameter-d1[cur],d1[cur]));
                cur = p1[cur];
            }
            //printf("%d %d %d\n",i,minimax,diameter);
            if (minimax >= best1){
                best3 = best2;
                best2 = best1;
                best1 = minimax;
            }
            else if (minimax >= best2){
                best3 = best2;
                best2 = minimax;
            }
            else if (minimax > best3){
                best3 = minimax;
            }
        }
    }
    return max(ans,max(best1+best2+L,best2+best3+2*L));
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...