답안 #300132

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
300132 2020-09-16T19:27:45 Z Hehehe 꿈 (IOI13_dreaming) C++14
0 / 100
1000 ms 22392 KB
#include<bits/stdc++.h> //:3
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define sz(x) (int)((x).size())
//#define int long long
#include "dreaming.h"

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 2e9;
const ll mod = 1e9 + 7;
const int N = 2e5 + 11;
const int X = 1e6;
const ll INF64 = 3e18 + 1;
const double eps = 1e-14;
const double PI = acos(-1);

//ifstream in(".in");
//ofstream out(".out");

int path[N], viz[N], s[N], dp_up[N], dp_down[N], nr;

vector<pi> v[N];
vector<int> c[N];

void dfs(int nod, int P){

    c[nr].pb(nod);
    viz[nod] = 1;

    for(auto it : v[nod]){
        if(it.ff == P)continue;
        s[nod] = max(s[nod], s[it.ff] + it.ss);
    }

    int mx1 = 0, mx2 = 0;
    for(auto it : v[nod]){
        if(it.ff == P)continue;

        if(s[it.ff] + it.ss > mx1){
            mx2 = mx1;
            mx1 = s[it.ff] + it.ss;
            continue;
        }
        if(s[it.ff] + it.ss > mx2){
            mx2 = s[it.ff] + it.ss;
            continue;
        }
    }

    dp_down[nod] = mx1 + mx2;
}

void dfs2(int nod, int P, int cost){

    dp_up[nod] = max(dp_up[nod], dp_up[P] + cost);

    int mx1 = 0, mx2 = 0, F = 0;
    for(auto it : v[nod]){
        if(it.ff == P)continue;

        if(s[it.ff] + it.ss > mx1){
            mx2 = mx1;
            mx1 = s[it.ff] + it.ss;
            F = it.ff;
            continue;
        }
        if(s[it.ff] + it.ss > mx2){
            mx2 = s[it.ff] + it.ss;
            continue;
        }
    }

    for(auto it : v[nod]){
        if(it.ff == P)continue;

        if(it.ff == F){
            dp_up[it.ff] = max(dp_up[it.ff], mx2 + it.ss);
            continue;
        }

        dp_up[it.ff] = max(dp_up[it.ff], mx1 + it.ss);
    }

    for(auto it : v[nod]){
        if(it.ff == P)continue;
        dfs2(it.ff, nod, it.ss);
    }
}

int travelTime (int n, int m, int l, int a[], int b[], int cost[]){

    for(int i = 0; i <= n; i++){
        v[i].clear();
    }

    for(int i = 0; i < m; i++){
        int x = a[i], y = b[i], z = cost[i];
        x++, y++;
        v[x].push_back({y, z});
        v[y].push_back({x, z});
    }

    memset(viz,0,sizeof(viz));
    memset(dp_up,0,sizeof(dp_up));
    memset(dp_down,0,sizeof(dp_down));
    memset(s,0,sizeof(s));

    int nr_comp = 0;
    for(int i = 1; i <= n; i++){
        if(viz[i])continue;

        nr_comp++;
        dfs(i, 0);
        dfs2(i, 0, 0);
    }

    int ans = 0;
    for(int i = 1;i <= nr; i++){
        int mn = inf;
        for(auto nod : c[i]){
            int dist_max = max(dp_up[nod] + s[nod], dp_down[nod]);
            ans = max(ans, dist_max);
            int dist = max(s[nod], dp_up[nod]);
            mn = min(mn,dist);
        }
        path[i] = mn;
    }

    sort(path + 1, path + nr + 1);

    if(nr >= 2)ans = max(ans, path[nr] + path[nr - 1] + l);
    if(nr >= 3)ans = max(ans, path[nr - 1] + path[nr - 2] + 2*l);

    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1046 ms 22392 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1046 ms 22392 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1046 ms 22392 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 16248 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1046 ms 22392 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1046 ms 22392 KB Time limit exceeded
2 Halted 0 ms 0 KB -