Submission #888987

# Submission time Handle Problem Language Result Execution time Memory
888987 2023-12-18T14:31:59 Z dwuy Dreaming (IOI13_dreaming) C++14
0 / 100
35 ms 14928 KB
/// dwuy: _,\,,,_\,__,\,,_
#include "dreaming.h"
#include <bits/stdc++.h>

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) int32_t(s.length())
#define MASK(k)(1LL<<(k))
#define TASK ""

using namespace std;

typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;

const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MX = 100005;

struct DSU{
    int n;
    vector<int> e;

    DSU(int n=0){
        this->n = n;
        this->e.assign(n+5, -1);
    }

    int fp(int u){
        return e[u]<0? u : e[u] = fp(e[u]);
    }

    void unon(int u, int v){
        u = fp(u);
        v = fp(v);
        if(u == v) return;
        if(e[u] > e[v]) swap(u, v);
        e[u] += e[v];
        e[v] = u;
    }
};

int n, m, l;
vector<pii> G[MX];
int dp[MX];
int dpDown[MX];
int mnPath[MX];
pii pf[MX];
pii sf[MX];
DSU dsu;

void calc_dp_down(int u, int p){
    dpDown[u] = 0;
    for(pii &tmp: G[u]){
        int v, c; tie(v, c) = tmp;
        if(v == p) continue;
        calc_dp_down(v, u);
        dpDown[u] = max(dpDown[u], dpDown[v] + c);
    }
}

void calc_dp(int u, int p, int mx){
    pii best1 = {mx, p};
    pii best2 = {0, 0};
    for(pii &tmp: G[u]){
        int v, c; tie(v, c) = tmp;
        if(v == p) continue;
        int dis = dpDown[v] + c;
        if(dis >= best1.fi){
            best2 = best1;
            best1 = {dis, v};
        }
        else best2 = max(best2, {dis, v});
    }
    dp[u] = best1.fi;
    for(pii &tmp: G[u]){
        int v, c; tie(v, c) = tmp;
        if(v == p) continue;
        if(v == best1.se) calc_dp(v, u, best2.fi + c);
        else calc_dp(v, u, best1.fi + c);
    }
}

pii combine(pii a, pii b){
    if(a.fi < b.fi) swap(a, b);
    if(a.se > b.fi) return a;
    return {a.fi, b.fi};
}

int travelTime(int N, int M, int L, int A[], int B[], int C[]){
    tie(n, m, l) = {N, M, L};
    dsu = DSU(n);
    for(int i=0; i<m; i++){
        int u = A[i]; u++;
        int v = B[i]; v++;
        int c = C[i];
        G[u].push_back({v, c});
        G[v].push_back({u, c});
        dsu.unon(u, v);
    }
    memset(dpDown, -1, sizeof dpDown);
    memset(dp, -1, sizeof dp);
    for(int i=1; i<=n; i++) if(dpDown[i] == -1) calc_dp_down(i, 0);
    for(int i=1; i<=n; i++) if(dp[i] == -1) calc_dp(i, 0, 0);

    for(int i=0; i<=n+1; i++){
        pf[i] = {-1, -1};
        sf[i] = {-1, -1};
    }

    memset(mnPath, 0x3f, sizeof mnPath);
    for(int i=1; i<=n; i++){
        int root = dsu.fp(i);
        mnPath[root] = min(mnPath[root], dp[i]);
    }
    for(int i=1;  i<=n; i++) if(dsu.fp(i) != i) mnPath[i] = -1;

    for(int i=1; i<=n; i++){
            pf[i] = {mnPath[i], -1};
        pf[i] = combine(pf[i-1], pf[i]);
    }
    for(int i=n; i>=1; i--){
        if(dsu.fp(i) == i) sf[i] = {mnPath[i], -1};
        else sf[i] = {-1, -1};
        sf[i] = combine(sf[i+1], sf[i]);
    }
    int ans = INF;
    for(int i=1; i<=n; i++) if(dsu.fp(i) == i){
        pii best = combine(pf[i-1], sf[i+1]);
        ans = min(ans, max(best.fi + best.se + l + l, best.fi + mnPath[i] + l));
    }
    for(int i=1; i<=n; i++) ans = max(ans, dp[i]);
    return ans;
}

# Verdict Execution time Memory Grader output
1 Correct 32 ms 14928 KB Output is correct
2 Correct 35 ms 14748 KB Output is correct
3 Correct 23 ms 13660 KB Output is correct
4 Correct 6 ms 7796 KB Output is correct
5 Correct 5 ms 7260 KB Output is correct
6 Correct 9 ms 8284 KB Output is correct
7 Incorrect 2 ms 6492 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Incorrect 2 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 32 ms 14928 KB Output is correct
2 Correct 35 ms 14748 KB Output is correct
3 Correct 23 ms 13660 KB Output is correct
4 Correct 6 ms 7796 KB Output is correct
5 Correct 5 ms 7260 KB Output is correct
6 Correct 9 ms 8284 KB Output is correct
7 Incorrect 2 ms 6492 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 9304 KB Output is correct
2 Correct 17 ms 9196 KB Output is correct
3 Correct 15 ms 9308 KB Output is correct
4 Correct 15 ms 9308 KB Output is correct
5 Correct 20 ms 9332 KB Output is correct
6 Correct 16 ms 9384 KB Output is correct
7 Correct 17 ms 9320 KB Output is correct
8 Correct 14 ms 9304 KB Output is correct
9 Correct 14 ms 9308 KB Output is correct
10 Correct 15 ms 9456 KB Output is correct
11 Correct 2 ms 6488 KB Output is correct
12 Correct 4 ms 6748 KB Output is correct
13 Correct 4 ms 6748 KB Output is correct
14 Correct 4 ms 6948 KB Output is correct
15 Correct 4 ms 6748 KB Output is correct
16 Correct 4 ms 6748 KB Output is correct
17 Correct 4 ms 6748 KB Output is correct
18 Correct 4 ms 6748 KB Output is correct
19 Correct 4 ms 6748 KB Output is correct
20 Incorrect 2 ms 6528 KB Output isn't correct
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Incorrect 2 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 32 ms 14928 KB Output is correct
2 Correct 35 ms 14748 KB Output is correct
3 Correct 23 ms 13660 KB Output is correct
4 Correct 6 ms 7796 KB Output is correct
5 Correct 5 ms 7260 KB Output is correct
6 Correct 9 ms 8284 KB Output is correct
7 Incorrect 2 ms 6492 KB Output isn't correct
8 Halted 0 ms 0 KB -