답안 #962147

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
962147 2024-04-13T07:40:36 Z NValchanov 꿈 (IOI13_dreaming) C++17
47 / 100
101 ms 21448 KB
#include <bits/stdc++.h>
#include "dreaming.h"

using namespace std;

typedef long long ll;

const ll MAXN = 1e5 + 10;
const ll INF = 4e18 + 10;

struct edge
{
    ll u,v,cost;
    edge(){};
    edge(ll _u, ll _v, ll _cost)
    {
        u = _u;
        v = _v;
        cost = _cost;
    }
    edge(ll _v, ll _cost)
    {
        u = v = _v;
        cost = _cost;
    }

    bool operator<(const edge&e)const
    {
        return cost < e.cost;
    }
};

ll n,m,l;
vector < edge > adj[MAXN];
bool visited[MAXN];
ll len[MAXN];
ll opt1[MAXN];
ll opt2[MAXN];
ll real_distance;
ll border;
vector < ll > topo;

void dfs(ll u, ll par)
{
    topo.push_back(u);
    visited[u] = true;
    for(edge e : adj[u])
        if(e.v != par)
            dfs(e.v, u);
}

void find_opt(ll u, ll par)
{
    opt1[u] = 0;

    for(edge e : adj[u])
    {
        ll v = e.v;
        ll cost = e.cost;

        if(v == par)
            continue;

        find_opt(v, u);

        ll cur = opt1[v] + cost;

        if(cur > opt1[u])
        {
            opt2[u] = opt1[u];
            opt1[u] = cur;
        }
        else
        {
            opt2[u] = max(opt2[u], cur);
        }
    }
}

void find_dist(ll u, ll par, ll carry)
{
    ll cur = max(carry, opt1[u]);

    real_distance = min(real_distance, cur);

    if(carry >= opt1[u])
        return;

    for(edge e : adj[u])
    {
        ll v = e.v;
        ll cost = e.cost;

        if(v == par)
            continue;

        if(opt1[u] == opt1[v] + e.cost)
        {
            ll new_carry = max(carry, opt2[u]) + cost;
            find_dist(v, u, new_carry);
            return; /// prawa rekursiq !!!
        }
    }
}

void find_path(ll src)
{
    for(ll u : topo)
    {
        len[u] = INF;
    }

    len[src] = 0;

    queue < ll > q;
    q.push(src);

    while(!q.empty())
    {
        ll u = q.front();
        q.pop();

        for(edge e : adj[u])
        {
            ll v = e.v;
            ll cost = e.cost;

            if(len[v] == INF) /// unvisited
            {
                len[v] = len[u] + cost;
                q.push(v);
            }
        }
    }
}

void init(ll u)
{
    topo.clear();
    dfs(u, u);
    real_distance = INF;
    find_opt(u, u);
    find_dist(u, u, 0);
    find_path(u);
}

ll solve(ll u)
{
    init(u);

    ll opt_ver = u;

    for(ll v : topo)
        if(len[v] > len[opt_ver])
            opt_ver = v;

    find_path(opt_ver);

    for(ll v : topo)
        if(len[v] > border)
            border = len[v];

    return real_distance;
}

ll solution()
{
    vector < ll > dists;
    for(int i = 0; i < n; i++)
    {
        if(!visited[i])
        {
            ll cur_dist = solve(i);
            dists.push_back(cur_dist);
        }
    }
    sort(dists.begin(), dists.end());

    ll comp_cnt = dists.size();

    ll ans = dists[comp_cnt - 1] + dists[comp_cnt - 2] + l;

    if(comp_cnt >= 3)
    {
        ans = max(ans, dists[comp_cnt - 2] + dists[comp_cnt - 3] + 2 * l);
    }
    ans = max(ans, border);

    return ans;
}

int travelTime(int N, int M, int L, int a[], int b[], int w[])
{
    n = N;
    m = M;
    l = L;

    for(int i = 0; i < m; i++)
    {
        adj[a[i]].push_back(edge(a[i], b[i], w[i]));
        adj[b[i]].push_back(edge(b[i], a[i], w[i]));
    }

    return solution();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 16828 KB Output is correct
2 Correct 43 ms 16468 KB Output is correct
3 Correct 31 ms 14696 KB Output is correct
4 Correct 7 ms 7772 KB Output is correct
5 Correct 6 ms 7260 KB Output is correct
6 Correct 17 ms 8664 KB Output is correct
7 Correct 2 ms 6236 KB Output is correct
8 Correct 22 ms 10768 KB Output is correct
9 Correct 29 ms 13168 KB Output is correct
10 Correct 2 ms 6236 KB Output is correct
11 Correct 38 ms 14040 KB Output is correct
12 Correct 56 ms 15448 KB Output is correct
13 Correct 2 ms 6236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6236 KB Output is correct
2 Correct 1 ms 6236 KB Output is correct
3 Correct 2 ms 6236 KB Output is correct
4 Correct 2 ms 6236 KB Output is correct
5 Correct 2 ms 6236 KB Output is correct
6 Correct 2 ms 6236 KB Output is correct
7 Correct 3 ms 6236 KB Output is correct
8 Correct 2 ms 6236 KB Output is correct
9 Correct 2 ms 6236 KB Output is correct
10 Correct 2 ms 6236 KB Output is correct
11 Correct 2 ms 6236 KB Output is correct
12 Correct 2 ms 6232 KB Output is correct
13 Correct 1 ms 6236 KB Output is correct
14 Correct 2 ms 6236 KB Output is correct
15 Correct 2 ms 6236 KB Output is correct
16 Correct 1 ms 6068 KB Output is correct
17 Correct 2 ms 6236 KB Output is correct
18 Correct 2 ms 6232 KB Output is correct
19 Correct 2 ms 6236 KB Output is correct
20 Correct 2 ms 6236 KB Output is correct
21 Correct 2 ms 6236 KB Output is correct
22 Correct 3 ms 6232 KB Output is correct
23 Correct 1 ms 6232 KB Output is correct
24 Correct 2 ms 6232 KB Output is correct
25 Correct 1 ms 6236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 16828 KB Output is correct
2 Correct 43 ms 16468 KB Output is correct
3 Correct 31 ms 14696 KB Output is correct
4 Correct 7 ms 7772 KB Output is correct
5 Correct 6 ms 7260 KB Output is correct
6 Correct 17 ms 8664 KB Output is correct
7 Correct 2 ms 6236 KB Output is correct
8 Correct 22 ms 10768 KB Output is correct
9 Correct 29 ms 13168 KB Output is correct
10 Correct 2 ms 6236 KB Output is correct
11 Correct 38 ms 14040 KB Output is correct
12 Correct 56 ms 15448 KB Output is correct
13 Correct 2 ms 6236 KB Output is correct
14 Correct 2 ms 6236 KB Output is correct
15 Correct 1 ms 6236 KB Output is correct
16 Correct 2 ms 6236 KB Output is correct
17 Correct 2 ms 6236 KB Output is correct
18 Correct 2 ms 6236 KB Output is correct
19 Correct 2 ms 6236 KB Output is correct
20 Correct 3 ms 6236 KB Output is correct
21 Correct 2 ms 6236 KB Output is correct
22 Correct 2 ms 6236 KB Output is correct
23 Correct 2 ms 6236 KB Output is correct
24 Correct 2 ms 6236 KB Output is correct
25 Correct 2 ms 6232 KB Output is correct
26 Correct 1 ms 6236 KB Output is correct
27 Correct 2 ms 6236 KB Output is correct
28 Correct 2 ms 6236 KB Output is correct
29 Correct 1 ms 6068 KB Output is correct
30 Correct 2 ms 6236 KB Output is correct
31 Correct 2 ms 6232 KB Output is correct
32 Correct 2 ms 6236 KB Output is correct
33 Correct 2 ms 6236 KB Output is correct
34 Correct 2 ms 6236 KB Output is correct
35 Correct 3 ms 6232 KB Output is correct
36 Correct 1 ms 6232 KB Output is correct
37 Correct 2 ms 6232 KB Output is correct
38 Correct 1 ms 6236 KB Output is correct
39 Correct 39 ms 16832 KB Output is correct
40 Correct 47 ms 16580 KB Output is correct
41 Correct 31 ms 14672 KB Output is correct
42 Correct 7 ms 7928 KB Output is correct
43 Correct 1 ms 6236 KB Output is correct
44 Correct 2 ms 6072 KB Output is correct
45 Correct 2 ms 6236 KB Output is correct
46 Correct 1 ms 6236 KB Output is correct
47 Correct 2 ms 6236 KB Output is correct
48 Correct 2 ms 6236 KB Output is correct
49 Correct 2 ms 6232 KB Output is correct
50 Correct 1 ms 6232 KB Output is correct
51 Correct 2 ms 6236 KB Output is correct
52 Correct 1 ms 6232 KB Output is correct
53 Correct 2 ms 6236 KB Output is correct
54 Correct 2 ms 6236 KB Output is correct
55 Correct 2 ms 6236 KB Output is correct
56 Correct 2 ms 6236 KB Output is correct
57 Correct 71 ms 14884 KB Output is correct
58 Correct 62 ms 14540 KB Output is correct
59 Correct 48 ms 14536 KB Output is correct
60 Correct 61 ms 13992 KB Output is correct
61 Correct 67 ms 14164 KB Output is correct
62 Correct 72 ms 13844 KB Output is correct
63 Correct 57 ms 13964 KB Output is correct
64 Correct 56 ms 13928 KB Output is correct
65 Correct 48 ms 14040 KB Output is correct
66 Correct 51 ms 14096 KB Output is correct
67 Correct 56 ms 14552 KB Output is correct
68 Correct 54 ms 14540 KB Output is correct
69 Correct 51 ms 14032 KB Output is correct
70 Correct 64 ms 14000 KB Output is correct
71 Correct 2 ms 6232 KB Output is correct
72 Correct 4 ms 6488 KB Output is correct
73 Correct 3 ms 6488 KB Output is correct
74 Correct 3 ms 6492 KB Output is correct
75 Correct 3 ms 6236 KB Output is correct
76 Correct 3 ms 6492 KB Output is correct
77 Correct 3 ms 6492 KB Output is correct
78 Correct 3 ms 6492 KB Output is correct
79 Correct 3 ms 6492 KB Output is correct
80 Correct 67 ms 14608 KB Output is correct
81 Correct 59 ms 14536 KB Output is correct
82 Correct 56 ms 14288 KB Output is correct
83 Correct 55 ms 14424 KB Output is correct
84 Correct 2 ms 6236 KB Output is correct
85 Correct 2 ms 6236 KB Output is correct
86 Correct 3 ms 6232 KB Output is correct
87 Correct 3 ms 6232 KB Output is correct
88 Correct 3 ms 6236 KB Output is correct
89 Correct 2 ms 6236 KB Output is correct
90 Correct 3 ms 6236 KB Output is correct
91 Correct 4 ms 6236 KB Output is correct
92 Correct 4 ms 6236 KB Output is correct
93 Correct 3 ms 6236 KB Output is correct
94 Correct 2 ms 6236 KB Output is correct
95 Correct 1 ms 6236 KB Output is correct
96 Correct 2 ms 6236 KB Output is correct
97 Correct 1 ms 6236 KB Output is correct
98 Correct 1 ms 6236 KB Output is correct
99 Correct 2 ms 6236 KB Output is correct
100 Correct 1 ms 6236 KB Output is correct
101 Correct 2 ms 6236 KB Output is correct
102 Correct 1 ms 6236 KB Output is correct
103 Correct 2 ms 6236 KB Output is correct
104 Correct 6 ms 7260 KB Output is correct
105 Correct 15 ms 8684 KB Output is correct
106 Correct 2 ms 6236 KB Output is correct
107 Correct 20 ms 10712 KB Output is correct
108 Correct 31 ms 13012 KB Output is correct
109 Correct 2 ms 6236 KB Output is correct
110 Correct 48 ms 13968 KB Output is correct
111 Correct 56 ms 15564 KB Output is correct
112 Correct 3 ms 6232 KB Output is correct
113 Correct 101 ms 21448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 8924 KB Output is correct
2 Correct 27 ms 9036 KB Output is correct
3 Correct 24 ms 8940 KB Output is correct
4 Correct 22 ms 8924 KB Output is correct
5 Correct 18 ms 8924 KB Output is correct
6 Correct 37 ms 9436 KB Output is correct
7 Correct 35 ms 9016 KB Output is correct
8 Correct 22 ms 8820 KB Output is correct
9 Correct 21 ms 8896 KB Output is correct
10 Correct 22 ms 8976 KB Output is correct
11 Correct 2 ms 6236 KB Output is correct
12 Correct 10 ms 7236 KB Output is correct
13 Correct 17 ms 7380 KB Output is correct
14 Correct 10 ms 7380 KB Output is correct
15 Correct 17 ms 7380 KB Output is correct
16 Correct 10 ms 7396 KB Output is correct
17 Correct 10 ms 7380 KB Output is correct
18 Correct 10 ms 7380 KB Output is correct
19 Correct 10 ms 7380 KB Output is correct
20 Incorrect 2 ms 6232 KB Output isn't correct
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6236 KB Output is correct
2 Correct 1 ms 6236 KB Output is correct
3 Correct 2 ms 6236 KB Output is correct
4 Correct 2 ms 6236 KB Output is correct
5 Correct 2 ms 6236 KB Output is correct
6 Correct 2 ms 6236 KB Output is correct
7 Correct 3 ms 6236 KB Output is correct
8 Correct 2 ms 6236 KB Output is correct
9 Correct 2 ms 6236 KB Output is correct
10 Correct 2 ms 6236 KB Output is correct
11 Correct 2 ms 6236 KB Output is correct
12 Correct 2 ms 6232 KB Output is correct
13 Correct 1 ms 6236 KB Output is correct
14 Correct 2 ms 6236 KB Output is correct
15 Correct 2 ms 6236 KB Output is correct
16 Correct 1 ms 6068 KB Output is correct
17 Correct 2 ms 6236 KB Output is correct
18 Correct 2 ms 6232 KB Output is correct
19 Correct 2 ms 6236 KB Output is correct
20 Correct 2 ms 6236 KB Output is correct
21 Correct 2 ms 6236 KB Output is correct
22 Correct 3 ms 6232 KB Output is correct
23 Correct 1 ms 6232 KB Output is correct
24 Correct 2 ms 6232 KB Output is correct
25 Correct 1 ms 6236 KB Output is correct
26 Correct 3 ms 6236 KB Output is correct
27 Correct 4 ms 6236 KB Output is correct
28 Correct 3 ms 6488 KB Output is correct
29 Correct 2 ms 6236 KB Output is correct
30 Correct 2 ms 6488 KB Output is correct
31 Correct 3 ms 6236 KB Output is correct
32 Correct 2 ms 6236 KB Output is correct
33 Correct 2 ms 6236 KB Output is correct
34 Correct 3 ms 6236 KB Output is correct
35 Correct 2 ms 6236 KB Output is correct
36 Correct 2 ms 6236 KB Output is correct
37 Correct 2 ms 6236 KB Output is correct
38 Correct 2 ms 6236 KB Output is correct
39 Correct 2 ms 6236 KB Output is correct
40 Correct 2 ms 6236 KB Output is correct
41 Correct 2 ms 6236 KB Output is correct
42 Correct 2 ms 6236 KB Output is correct
43 Correct 2 ms 6236 KB Output is correct
44 Correct 2 ms 6236 KB Output is correct
45 Correct 2 ms 6236 KB Output is correct
46 Correct 2 ms 6236 KB Output is correct
47 Correct 2 ms 6236 KB Output is correct
48 Correct 2 ms 6164 KB Output is correct
49 Correct 2 ms 6232 KB Output is correct
50 Correct 2 ms 6236 KB Output is correct
51 Correct 2 ms 6236 KB Output is correct
52 Incorrect 2 ms 6232 KB Output isn't correct
53 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 16828 KB Output is correct
2 Correct 43 ms 16468 KB Output is correct
3 Correct 31 ms 14696 KB Output is correct
4 Correct 7 ms 7772 KB Output is correct
5 Correct 6 ms 7260 KB Output is correct
6 Correct 17 ms 8664 KB Output is correct
7 Correct 2 ms 6236 KB Output is correct
8 Correct 22 ms 10768 KB Output is correct
9 Correct 29 ms 13168 KB Output is correct
10 Correct 2 ms 6236 KB Output is correct
11 Correct 38 ms 14040 KB Output is correct
12 Correct 56 ms 15448 KB Output is correct
13 Correct 2 ms 6236 KB Output is correct
14 Correct 2 ms 6236 KB Output is correct
15 Correct 1 ms 6236 KB Output is correct
16 Correct 2 ms 6236 KB Output is correct
17 Correct 2 ms 6236 KB Output is correct
18 Correct 2 ms 6236 KB Output is correct
19 Correct 2 ms 6236 KB Output is correct
20 Correct 3 ms 6236 KB Output is correct
21 Correct 2 ms 6236 KB Output is correct
22 Correct 2 ms 6236 KB Output is correct
23 Correct 2 ms 6236 KB Output is correct
24 Correct 2 ms 6236 KB Output is correct
25 Correct 2 ms 6232 KB Output is correct
26 Correct 1 ms 6236 KB Output is correct
27 Correct 2 ms 6236 KB Output is correct
28 Correct 2 ms 6236 KB Output is correct
29 Correct 1 ms 6068 KB Output is correct
30 Correct 2 ms 6236 KB Output is correct
31 Correct 2 ms 6232 KB Output is correct
32 Correct 2 ms 6236 KB Output is correct
33 Correct 2 ms 6236 KB Output is correct
34 Correct 2 ms 6236 KB Output is correct
35 Correct 3 ms 6232 KB Output is correct
36 Correct 1 ms 6232 KB Output is correct
37 Correct 2 ms 6232 KB Output is correct
38 Correct 1 ms 6236 KB Output is correct
39 Correct 39 ms 16832 KB Output is correct
40 Correct 47 ms 16580 KB Output is correct
41 Correct 31 ms 14672 KB Output is correct
42 Correct 7 ms 7928 KB Output is correct
43 Correct 1 ms 6236 KB Output is correct
44 Correct 2 ms 6072 KB Output is correct
45 Correct 2 ms 6236 KB Output is correct
46 Correct 1 ms 6236 KB Output is correct
47 Correct 2 ms 6236 KB Output is correct
48 Correct 2 ms 6236 KB Output is correct
49 Correct 2 ms 6232 KB Output is correct
50 Correct 1 ms 6232 KB Output is correct
51 Correct 2 ms 6236 KB Output is correct
52 Correct 1 ms 6232 KB Output is correct
53 Correct 2 ms 6236 KB Output is correct
54 Correct 2 ms 6236 KB Output is correct
55 Correct 2 ms 6236 KB Output is correct
56 Correct 2 ms 6236 KB Output is correct
57 Correct 71 ms 14884 KB Output is correct
58 Correct 62 ms 14540 KB Output is correct
59 Correct 48 ms 14536 KB Output is correct
60 Correct 61 ms 13992 KB Output is correct
61 Correct 67 ms 14164 KB Output is correct
62 Correct 72 ms 13844 KB Output is correct
63 Correct 57 ms 13964 KB Output is correct
64 Correct 56 ms 13928 KB Output is correct
65 Correct 48 ms 14040 KB Output is correct
66 Correct 51 ms 14096 KB Output is correct
67 Correct 56 ms 14552 KB Output is correct
68 Correct 54 ms 14540 KB Output is correct
69 Correct 51 ms 14032 KB Output is correct
70 Correct 64 ms 14000 KB Output is correct
71 Correct 2 ms 6232 KB Output is correct
72 Correct 4 ms 6488 KB Output is correct
73 Correct 3 ms 6488 KB Output is correct
74 Correct 3 ms 6492 KB Output is correct
75 Correct 3 ms 6236 KB Output is correct
76 Correct 3 ms 6492 KB Output is correct
77 Correct 3 ms 6492 KB Output is correct
78 Correct 3 ms 6492 KB Output is correct
79 Correct 3 ms 6492 KB Output is correct
80 Correct 67 ms 14608 KB Output is correct
81 Correct 59 ms 14536 KB Output is correct
82 Correct 56 ms 14288 KB Output is correct
83 Correct 55 ms 14424 KB Output is correct
84 Correct 2 ms 6236 KB Output is correct
85 Correct 2 ms 6236 KB Output is correct
86 Correct 3 ms 6232 KB Output is correct
87 Correct 3 ms 6232 KB Output is correct
88 Correct 3 ms 6236 KB Output is correct
89 Correct 2 ms 6236 KB Output is correct
90 Correct 3 ms 6236 KB Output is correct
91 Correct 4 ms 6236 KB Output is correct
92 Correct 4 ms 6236 KB Output is correct
93 Correct 3 ms 6236 KB Output is correct
94 Correct 2 ms 6236 KB Output is correct
95 Correct 1 ms 6236 KB Output is correct
96 Correct 2 ms 6236 KB Output is correct
97 Correct 1 ms 6236 KB Output is correct
98 Correct 1 ms 6236 KB Output is correct
99 Correct 2 ms 6236 KB Output is correct
100 Correct 1 ms 6236 KB Output is correct
101 Correct 2 ms 6236 KB Output is correct
102 Correct 1 ms 6236 KB Output is correct
103 Correct 2 ms 6236 KB Output is correct
104 Correct 6 ms 7260 KB Output is correct
105 Correct 15 ms 8684 KB Output is correct
106 Correct 2 ms 6236 KB Output is correct
107 Correct 20 ms 10712 KB Output is correct
108 Correct 31 ms 13012 KB Output is correct
109 Correct 2 ms 6236 KB Output is correct
110 Correct 48 ms 13968 KB Output is correct
111 Correct 56 ms 15564 KB Output is correct
112 Correct 3 ms 6232 KB Output is correct
113 Correct 101 ms 21448 KB Output is correct
114 Correct 20 ms 8924 KB Output is correct
115 Correct 27 ms 9036 KB Output is correct
116 Correct 24 ms 8940 KB Output is correct
117 Correct 22 ms 8924 KB Output is correct
118 Correct 18 ms 8924 KB Output is correct
119 Correct 37 ms 9436 KB Output is correct
120 Correct 35 ms 9016 KB Output is correct
121 Correct 22 ms 8820 KB Output is correct
122 Correct 21 ms 8896 KB Output is correct
123 Correct 22 ms 8976 KB Output is correct
124 Correct 2 ms 6236 KB Output is correct
125 Correct 10 ms 7236 KB Output is correct
126 Correct 17 ms 7380 KB Output is correct
127 Correct 10 ms 7380 KB Output is correct
128 Correct 17 ms 7380 KB Output is correct
129 Correct 10 ms 7396 KB Output is correct
130 Correct 10 ms 7380 KB Output is correct
131 Correct 10 ms 7380 KB Output is correct
132 Correct 10 ms 7380 KB Output is correct
133 Incorrect 2 ms 6232 KB Output isn't correct
134 Halted 0 ms 0 KB -