Submission #870621

# Submission time Handle Problem Language Result Execution time Memory
870621 2023-11-08T14:38:24 Z fanwen Aesthetic (NOI20_aesthetic) C++17
100 / 100
359 ms 56076 KB
#include <bits/stdc++.h>

using namespace std;

#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }

const int MAX = 3e5 + 5;
const long long INF = numeric_limits <long long>::max() / 2;

int N, M, low[MAX], num[MAX];
long long dp1[MAX], dpN[MAX];
vector <tuple <int, int, int>> adj[MAX];

long long dijkstra(int source, int target, long long dp[]) {
    fill(dp + 1, dp + N + 1, INF);
    priority_queue <pair <long long, int>, vector <pair <long long, int>>, greater <pair <long long, int>>> q;
    q.emplace(dp[source] = 0, source);
    while(!q.empty()) {
        auto [du, u] = q.top(); q.pop();
        if(dp[u] != du) continue;
        for (auto [v, w, _] : adj[u]) if(minimize(dp[v], du + w)) {
            q.emplace(dp[v], v);
        }
    }
    return dp[target];
}

bool check(long long val) {
    auto is_correct = [&] (int u, int v, int w, int r) -> bool {
        return dp1[u] + w + r + dpN[v] >= val and dpN[u] + w + r + dp1[v] >= val;
    };

    auto is_edge = [&] (int u, int v, int w) -> bool {
        return dp1[u] + w + dpN[v] < val or dpN[u] + w + dp1[v] < val;
    };

    fill(num + 1, num + N + 1, 0);
    fill(low + 1, low + N + 1, 0);
    int Time = 0;
    bool ans = false;
    function <void(int, int)> dfs = [&] (int u, int p) {
        num[u] = low[u] = ++Time;
        for (auto [v, w, r] : adj[u]) if(v != p && is_edge(u, v, w)) {
            if(!num[v]) {
                dfs(v, u);
                minimize(low[u], low[v]);
                if(low[v] > num[u] and is_correct(u, v, w, r) and low[N] && low[v] <= num[N]) {
                    ans |= true;
                }
            } else minimize(low[u], low[v]);
        }
    };
    dfs(1, 0);

    return ans;
}

void you_make_it(void) {
    cin >> N >> M;
    vector <tuple <int, int, int>> edges(M);
    for (auto &[u, v, w] : edges) cin >> u >> v >> w;
    reverse(ALL(edges));
    int Max = 0;
    for (auto [u, v, w] : edges) {
        adj[u].emplace_back(v, w, Max);
        adj[v].emplace_back(u, w, Max);
        maximize(Max, w);
    }

    long long Min_dist = dijkstra(1, N, dp1); dijkstra(N, 1, dpN);

    long long L = Min_dist, R = Min_dist + Max + 2;
    while(R - L > 1) {
        long long Mid = L + R >> 1;
        if(check(Mid)) L = Mid;
        else R = Mid;
    }
    cout << L;
}

signed main() {

#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
    file("Aesthetic");
    auto start_time = chrono::steady_clock::now();

    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);

    you_make_it();

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

// Dream it. Wish it. Do it.

Compilation message

Aesthetic.cpp: In function 'void you_make_it()':
Aesthetic.cpp:85:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   85 |         long long Mid = L + R >> 1;
      |                         ~~^~~
Aesthetic.cpp: In function 'int main()':
Aesthetic.cpp:13:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Aesthetic.cpp:98:5: note: in expansion of macro 'file'
   98 |     file("Aesthetic");
      |     ^~~~
Aesthetic.cpp:13:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Aesthetic.cpp:98:5: note: in expansion of macro 'file'
   98 |     file("Aesthetic");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 3 ms 12952 KB Output is correct
5 Correct 3 ms 12892 KB Output is correct
6 Correct 2 ms 12892 KB Output is correct
7 Correct 2 ms 12948 KB Output is correct
8 Correct 2 ms 12888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 3 ms 12952 KB Output is correct
5 Correct 3 ms 12892 KB Output is correct
6 Correct 2 ms 12892 KB Output is correct
7 Correct 2 ms 12948 KB Output is correct
8 Correct 2 ms 12888 KB Output is correct
9 Correct 3 ms 12948 KB Output is correct
10 Correct 4 ms 12892 KB Output is correct
11 Correct 4 ms 12892 KB Output is correct
12 Correct 3 ms 13096 KB Output is correct
13 Correct 4 ms 13148 KB Output is correct
14 Correct 3 ms 12892 KB Output is correct
15 Correct 3 ms 13152 KB Output is correct
16 Correct 3 ms 12980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 303 ms 36288 KB Output is correct
2 Correct 285 ms 36360 KB Output is correct
3 Correct 266 ms 36168 KB Output is correct
4 Correct 230 ms 36092 KB Output is correct
5 Correct 295 ms 36176 KB Output is correct
6 Correct 227 ms 36700 KB Output is correct
7 Correct 240 ms 36588 KB Output is correct
8 Correct 247 ms 36872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 300 ms 36772 KB Output is correct
2 Correct 272 ms 36448 KB Output is correct
3 Correct 247 ms 36208 KB Output is correct
4 Correct 234 ms 36792 KB Output is correct
5 Correct 225 ms 36276 KB Output is correct
6 Correct 244 ms 36432 KB Output is correct
7 Correct 251 ms 36600 KB Output is correct
8 Correct 321 ms 36632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 33896 KB Output is correct
2 Correct 119 ms 31568 KB Output is correct
3 Correct 166 ms 40016 KB Output is correct
4 Correct 155 ms 40096 KB Output is correct
5 Correct 147 ms 40216 KB Output is correct
6 Correct 169 ms 40096 KB Output is correct
7 Correct 133 ms 40060 KB Output is correct
8 Correct 138 ms 40272 KB Output is correct
9 Correct 155 ms 39976 KB Output is correct
10 Correct 133 ms 40276 KB Output is correct
11 Correct 132 ms 40100 KB Output is correct
12 Correct 191 ms 34648 KB Output is correct
13 Correct 150 ms 40276 KB Output is correct
14 Correct 99 ms 55864 KB Output is correct
15 Correct 114 ms 56076 KB Output is correct
16 Correct 198 ms 34744 KB Output is correct
17 Correct 188 ms 34132 KB Output is correct
18 Correct 185 ms 34632 KB Output is correct
19 Correct 116 ms 31572 KB Output is correct
20 Correct 116 ms 31572 KB Output is correct
21 Correct 121 ms 31548 KB Output is correct
22 Correct 116 ms 31608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 33896 KB Output is correct
2 Correct 119 ms 31568 KB Output is correct
3 Correct 166 ms 40016 KB Output is correct
4 Correct 155 ms 40096 KB Output is correct
5 Correct 147 ms 40216 KB Output is correct
6 Correct 169 ms 40096 KB Output is correct
7 Correct 133 ms 40060 KB Output is correct
8 Correct 138 ms 40272 KB Output is correct
9 Correct 155 ms 39976 KB Output is correct
10 Correct 133 ms 40276 KB Output is correct
11 Correct 132 ms 40100 KB Output is correct
12 Correct 191 ms 34648 KB Output is correct
13 Correct 150 ms 40276 KB Output is correct
14 Correct 99 ms 55864 KB Output is correct
15 Correct 114 ms 56076 KB Output is correct
16 Correct 198 ms 34744 KB Output is correct
17 Correct 188 ms 34132 KB Output is correct
18 Correct 185 ms 34632 KB Output is correct
19 Correct 116 ms 31572 KB Output is correct
20 Correct 116 ms 31572 KB Output is correct
21 Correct 121 ms 31548 KB Output is correct
22 Correct 116 ms 31608 KB Output is correct
23 Correct 264 ms 37024 KB Output is correct
24 Correct 167 ms 31564 KB Output is correct
25 Correct 128 ms 35924 KB Output is correct
26 Correct 128 ms 34896 KB Output is correct
27 Correct 127 ms 34780 KB Output is correct
28 Correct 138 ms 36252 KB Output is correct
29 Correct 139 ms 34856 KB Output is correct
30 Correct 141 ms 35772 KB Output is correct
31 Correct 140 ms 35652 KB Output is correct
32 Correct 130 ms 35088 KB Output is correct
33 Correct 125 ms 35664 KB Output is correct
34 Correct 247 ms 35632 KB Output is correct
35 Correct 130 ms 35408 KB Output is correct
36 Correct 122 ms 50312 KB Output is correct
37 Correct 142 ms 51156 KB Output is correct
38 Correct 209 ms 34264 KB Output is correct
39 Correct 269 ms 35420 KB Output is correct
40 Correct 239 ms 35744 KB Output is correct
41 Correct 150 ms 31572 KB Output is correct
42 Correct 187 ms 31568 KB Output is correct
43 Correct 157 ms 31544 KB Output is correct
44 Correct 166 ms 31316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 3 ms 12952 KB Output is correct
5 Correct 3 ms 12892 KB Output is correct
6 Correct 2 ms 12892 KB Output is correct
7 Correct 2 ms 12948 KB Output is correct
8 Correct 2 ms 12888 KB Output is correct
9 Correct 3 ms 12948 KB Output is correct
10 Correct 4 ms 12892 KB Output is correct
11 Correct 4 ms 12892 KB Output is correct
12 Correct 3 ms 13096 KB Output is correct
13 Correct 4 ms 13148 KB Output is correct
14 Correct 3 ms 12892 KB Output is correct
15 Correct 3 ms 13152 KB Output is correct
16 Correct 3 ms 12980 KB Output is correct
17 Correct 303 ms 36288 KB Output is correct
18 Correct 285 ms 36360 KB Output is correct
19 Correct 266 ms 36168 KB Output is correct
20 Correct 230 ms 36092 KB Output is correct
21 Correct 295 ms 36176 KB Output is correct
22 Correct 227 ms 36700 KB Output is correct
23 Correct 240 ms 36588 KB Output is correct
24 Correct 247 ms 36872 KB Output is correct
25 Correct 300 ms 36772 KB Output is correct
26 Correct 272 ms 36448 KB Output is correct
27 Correct 247 ms 36208 KB Output is correct
28 Correct 234 ms 36792 KB Output is correct
29 Correct 225 ms 36276 KB Output is correct
30 Correct 244 ms 36432 KB Output is correct
31 Correct 251 ms 36600 KB Output is correct
32 Correct 321 ms 36632 KB Output is correct
33 Correct 202 ms 33896 KB Output is correct
34 Correct 119 ms 31568 KB Output is correct
35 Correct 166 ms 40016 KB Output is correct
36 Correct 155 ms 40096 KB Output is correct
37 Correct 147 ms 40216 KB Output is correct
38 Correct 169 ms 40096 KB Output is correct
39 Correct 133 ms 40060 KB Output is correct
40 Correct 138 ms 40272 KB Output is correct
41 Correct 155 ms 39976 KB Output is correct
42 Correct 133 ms 40276 KB Output is correct
43 Correct 132 ms 40100 KB Output is correct
44 Correct 191 ms 34648 KB Output is correct
45 Correct 150 ms 40276 KB Output is correct
46 Correct 99 ms 55864 KB Output is correct
47 Correct 114 ms 56076 KB Output is correct
48 Correct 198 ms 34744 KB Output is correct
49 Correct 188 ms 34132 KB Output is correct
50 Correct 185 ms 34632 KB Output is correct
51 Correct 116 ms 31572 KB Output is correct
52 Correct 116 ms 31572 KB Output is correct
53 Correct 121 ms 31548 KB Output is correct
54 Correct 116 ms 31608 KB Output is correct
55 Correct 264 ms 37024 KB Output is correct
56 Correct 167 ms 31564 KB Output is correct
57 Correct 128 ms 35924 KB Output is correct
58 Correct 128 ms 34896 KB Output is correct
59 Correct 127 ms 34780 KB Output is correct
60 Correct 138 ms 36252 KB Output is correct
61 Correct 139 ms 34856 KB Output is correct
62 Correct 141 ms 35772 KB Output is correct
63 Correct 140 ms 35652 KB Output is correct
64 Correct 130 ms 35088 KB Output is correct
65 Correct 125 ms 35664 KB Output is correct
66 Correct 247 ms 35632 KB Output is correct
67 Correct 130 ms 35408 KB Output is correct
68 Correct 122 ms 50312 KB Output is correct
69 Correct 142 ms 51156 KB Output is correct
70 Correct 209 ms 34264 KB Output is correct
71 Correct 269 ms 35420 KB Output is correct
72 Correct 239 ms 35744 KB Output is correct
73 Correct 150 ms 31572 KB Output is correct
74 Correct 187 ms 31568 KB Output is correct
75 Correct 157 ms 31544 KB Output is correct
76 Correct 166 ms 31316 KB Output is correct
77 Correct 235 ms 38072 KB Output is correct
78 Correct 302 ms 33876 KB Output is correct
79 Correct 202 ms 42324 KB Output is correct
80 Correct 199 ms 42308 KB Output is correct
81 Correct 220 ms 42324 KB Output is correct
82 Correct 199 ms 42320 KB Output is correct
83 Correct 204 ms 42576 KB Output is correct
84 Correct 215 ms 42728 KB Output is correct
85 Correct 230 ms 42320 KB Output is correct
86 Correct 202 ms 42576 KB Output is correct
87 Correct 207 ms 42584 KB Output is correct
88 Correct 289 ms 36924 KB Output is correct
89 Correct 206 ms 42708 KB Output is correct
90 Correct 194 ms 52008 KB Output is correct
91 Correct 188 ms 52396 KB Output is correct
92 Correct 246 ms 38012 KB Output is correct
93 Correct 277 ms 37964 KB Output is correct
94 Correct 219 ms 37532 KB Output is correct
95 Correct 359 ms 33852 KB Output is correct
96 Correct 290 ms 34148 KB Output is correct
97 Correct 277 ms 33856 KB Output is correct
98 Correct 283 ms 33984 KB Output is correct