Submission #7046

#TimeUsernameProblemLanguageResultExecution timeMemory
7046myungwooDreaming (IOI13_dreaming)C++98
100 / 100
223 ms14460 KiB
#include "dreaming.h"
#include <vector>
#include <queue>
#include <functional>
#include <algorithm>
using namespace std;

#define MAXN 100005

static int N,M,L,ans;
static int mom[MAXN],momv[MAXN],clongest[MAXN];
static vector<int> con[MAXN],conv[MAXN],R;
static queue<int> que;

void bfs(int n)
{
    int i,j,k,v,q;
    que.push(n);
    mom[n] = n;
    vector <int> order;
    while (!que.empty()){
        q = que.front(); que.pop();
        order.push_back(q);
        for (i=con[q].size();i--;){
            k = con[q][i], v = conv[q][i];
            if (mom[k]) continue;
            mom[k] = q, momv[k] = v;
            que.push(k);
        }
    }
    for (i=order.size();i--;){
        n = order[i];
        int max2=0;
        for (j=con[n].size();j--;){
            k = con[n][j], v = conv[n][j];
            if (mom[n] == k) continue;
            if (clongest[n] < clongest[k]+v)
                max2 = clongest[n], clongest[n] = clongest[k]+v;
            else if (max2 < clongest[k]+v)
                max2 = clongest[k]+v;
        }
        if (ans < clongest[n]+max2)
            ans = clongest[n]+max2;
    }
}

void dfs(int n,int longest,int &minlen)
{
    int i,k,v;
    int max1=0,max2=0,maxp=0;
    for (i=con[n].size();i--;){
        k = con[n][i], v = conv[n][i];
        if (mom[n] == k) continue;
        if (max1 < clongest[k]+v)
            max2 = max1, max1 = clongest[k]+v, maxp = k;
        else if (max2 < clongest[k]+v)
            max2 = clongest[k]+v;
    }
    if (minlen > max(longest,max1))
        minlen = max(longest,max1);
    if (maxp){
        dfs(maxp,max(longest,max2)+momv[maxp],minlen);
    }
}

void proc(int n)
{
    int minlen=1e9;
    bfs(n);
    dfs(n,0,minlen);
    R.push_back(minlen);
}

int travelTime(int n,int m,int l,int arr_a[],int arr_b[],int arr_t[])
{
    int i,a,b,c;
    N = n, M = m, L = l;
    for (i=0;i<M;i++){
        a = arr_a[i], b = arr_b[i], c = arr_t[i];
        ++a, ++b;
        con[a].push_back(b), conv[a].push_back(c);
        con[b].push_back(a), conv[b].push_back(c);
    }
    for (i=1;i<=N;i++) if (!mom[i]) proc(i);
    sort(R.begin(),R.end(),greater<int>());
    if (R.size() > 1){
        if (ans < R[0]+R[1]+L)
            ans = R[0]+R[1]+L;
    }
    if (R.size() > 2){
        if (ans < R[1]+R[2]+L+L)
            ans = R[1]+R[2]+L+L;
    }
    return ans;
}
#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...