Submission #1087631

# Submission time Handle Problem Language Result Execution time Memory
1087631 2024-09-13T03:23:17 Z vjudge1 Aesthetic (NOI20_aesthetic) C++17
100 / 100
914 ms 71756 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
#define MAX                 300005
int numNode, numEdge;
vector<int> G[MAX];
struct Edge{
    int u, v, w;
    Edge(){}
    Edge(int _u, int _v, int _w): u(_u), v(_v), w(_w){}

    int other(int x){return (x ^ u ^ v);}
} edge[MAX];
int minDist[2][MAX];

void dijkstra(int s, int minDist[]){
    for (int i = 1; i <= numNode; ++i) minDist[i] = LINF;
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
    q.emplace(0, s);
    minDist[s] = 0;
    while(q.size()){
        int du, u; tie(du, u) = q.top(); q.pop();
        if(du > minDist[u]) continue;

        for(int&i : G[u]) {
            int v = edge[i].other(u);
            if(minimize(minDist[v], minDist[u] + edge[i].w)){
                q.emplace(minDist[v], v);
            }
        }
    }
}

int low[MAX], num[MAX], timeDfs = 0;

int suffix[MAX];
bool contain[MAX];
vector<pair<int, int>> newG[MAX];
bool ok = 0;

int value = 0;
void tarjan(int u, int p){
    if(u == numNode) contain[u] = true;
    low[u] = num[u] = ++timeDfs;
    for (pair<int, int>&x : newG[u]){
        int v = x.first, i = x.second;
        if(i ^ p){
            if(num[v]) minimize(low[u], num[v]);
            else{
                tarjan(v, i);
                minimize(low[u], low[v]);
                contain[u] |= contain[v];

                if(num[v] == low[v]){
                    if(!contain[v]) continue;
                    int eu = edge[i].u, ev = edge[i].v;
                    if(minDist[0][eu] + edge[i].w + minDist[1][ev] != minDist[0][numNode]) continue;
                    if(minDist[0][eu] + edge[i].w + minDist[1][ev] + suffix[i] >= minDist[0][numNode] + value)
                        ok = 1;
                }
            }
        }
    }
}

void process(void){
    cin >> numNode >> numEdge;
    int max_weight = 0;
    for (int i = 1; i <= numEdge; ++i){
        cin >> edge[i].u >> edge[i].v >> edge[i].w;
        G[edge[i].u].emplace_back(i);
        G[edge[i].v].emplace_back(i);
        maximize(max_weight, edge[i].w);
    }

    dijkstra(1, minDist[0]);
    dijkstra(numNode, minDist[1]);

    suffix[numEdge] = -LINF;
    for (int i = numEdge - 1; i >= 1; --i){
        maximize(suffix[i], suffix[i + 1]);
        maximize(suffix[i], edge[i + 1].w);
    }

    for (int i = 1; i <= numEdge; ++i){
        if(minDist[0][edge[i].u] > minDist[0][edge[i].v]) swap(edge[i].u, edge[i].v);
    }
    int l = 1, r = max_weight;
    int add = 0;

    auto check = [&](int val) -> bool{
        timeDfs = 0, ok = 0;
        value = val;
        for (int i = 1; i <= numNode; ++i) newG[i].clear(), contain[i] = num[i] = low[i] = 0;

        auto active = [&](int u, int v, int w, int x) -> bool{
            if(minDist[0][u] + minDist[1][v] + w < minDist[0][numNode] + x) return true;
            if(minDist[0][v] + minDist[1][u] + w < minDist[0][numNode] + x) return true;
            return false;
        };

        for (int u = 1; u <= numNode; ++u){
            for (int&i : G[u]){
                int v = edge[i].other(u);
                if(active(u, v, edge[i].w, val)) {
                    newG[u].emplace_back(v, i);
                }
            }
        }
        tarjan(1, 0);
        return (ok);
    };

    while(l <= r){
        int m = (l + r) >> 1;
        if(check(m)) l = m + 1, add = m;
        else r = m - 1;
    }

    cout << minDist[0][numNode] + add;
}
signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    process();
    return (0 ^ 0);
}




Compilation message

Aesthetic.cpp: In lambda function:
Aesthetic.cpp:128:81: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  128 |         for (int i = 1; i <= numNode; ++i) newG[i].clear(), contain[i] = num[i] = low[i] = 0;
      |                                                                          ~~~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14612 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 7 ms 14564 KB Output is correct
4 Correct 6 ms 14428 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 8 ms 14564 KB Output is correct
7 Correct 6 ms 14424 KB Output is correct
8 Correct 6 ms 14428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14612 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 7 ms 14564 KB Output is correct
4 Correct 6 ms 14428 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 8 ms 14564 KB Output is correct
7 Correct 6 ms 14424 KB Output is correct
8 Correct 6 ms 14428 KB Output is correct
9 Correct 7 ms 14844 KB Output is correct
10 Correct 8 ms 14684 KB Output is correct
11 Correct 7 ms 14736 KB Output is correct
12 Correct 11 ms 14756 KB Output is correct
13 Correct 11 ms 14684 KB Output is correct
14 Correct 8 ms 14684 KB Output is correct
15 Correct 7 ms 14680 KB Output is correct
16 Correct 7 ms 14684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 795 ms 50512 KB Output is correct
2 Correct 844 ms 50768 KB Output is correct
3 Correct 819 ms 50436 KB Output is correct
4 Correct 742 ms 50648 KB Output is correct
5 Correct 684 ms 50616 KB Output is correct
6 Correct 914 ms 51400 KB Output is correct
7 Correct 774 ms 51464 KB Output is correct
8 Correct 766 ms 51704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 768 ms 51960 KB Output is correct
2 Correct 719 ms 51096 KB Output is correct
3 Correct 672 ms 51088 KB Output is correct
4 Correct 702 ms 51536 KB Output is correct
5 Correct 663 ms 51188 KB Output is correct
6 Correct 743 ms 51024 KB Output is correct
7 Correct 811 ms 51092 KB Output is correct
8 Correct 765 ms 51540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 248 ms 44988 KB Output is correct
2 Correct 127 ms 45708 KB Output is correct
3 Correct 187 ms 59732 KB Output is correct
4 Correct 186 ms 60000 KB Output is correct
5 Correct 175 ms 59724 KB Output is correct
6 Correct 200 ms 59728 KB Output is correct
7 Correct 172 ms 59732 KB Output is correct
8 Correct 171 ms 59984 KB Output is correct
9 Correct 174 ms 59728 KB Output is correct
10 Correct 176 ms 60316 KB Output is correct
11 Correct 188 ms 59984 KB Output is correct
12 Correct 226 ms 45144 KB Output is correct
13 Correct 194 ms 60076 KB Output is correct
14 Correct 88 ms 56656 KB Output is correct
15 Correct 95 ms 56552 KB Output is correct
16 Correct 199 ms 45084 KB Output is correct
17 Correct 200 ms 44112 KB Output is correct
18 Correct 223 ms 44864 KB Output is correct
19 Correct 122 ms 45852 KB Output is correct
20 Correct 145 ms 45800 KB Output is correct
21 Correct 119 ms 45904 KB Output is correct
22 Correct 126 ms 45908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 248 ms 44988 KB Output is correct
2 Correct 127 ms 45708 KB Output is correct
3 Correct 187 ms 59732 KB Output is correct
4 Correct 186 ms 60000 KB Output is correct
5 Correct 175 ms 59724 KB Output is correct
6 Correct 200 ms 59728 KB Output is correct
7 Correct 172 ms 59732 KB Output is correct
8 Correct 171 ms 59984 KB Output is correct
9 Correct 174 ms 59728 KB Output is correct
10 Correct 176 ms 60316 KB Output is correct
11 Correct 188 ms 59984 KB Output is correct
12 Correct 226 ms 45144 KB Output is correct
13 Correct 194 ms 60076 KB Output is correct
14 Correct 88 ms 56656 KB Output is correct
15 Correct 95 ms 56552 KB Output is correct
16 Correct 199 ms 45084 KB Output is correct
17 Correct 200 ms 44112 KB Output is correct
18 Correct 223 ms 44864 KB Output is correct
19 Correct 122 ms 45852 KB Output is correct
20 Correct 145 ms 45800 KB Output is correct
21 Correct 119 ms 45904 KB Output is correct
22 Correct 126 ms 45908 KB Output is correct
23 Correct 313 ms 45328 KB Output is correct
24 Correct 188 ms 48468 KB Output is correct
25 Correct 150 ms 43344 KB Output is correct
26 Correct 169 ms 41552 KB Output is correct
27 Correct 153 ms 41808 KB Output is correct
28 Correct 169 ms 43980 KB Output is correct
29 Correct 141 ms 41808 KB Output is correct
30 Correct 161 ms 42840 KB Output is correct
31 Correct 152 ms 42496 KB Output is correct
32 Correct 152 ms 42064 KB Output is correct
33 Correct 161 ms 43064 KB Output is correct
34 Correct 292 ms 44804 KB Output is correct
35 Correct 146 ms 42576 KB Output is correct
36 Correct 111 ms 59212 KB Output is correct
37 Correct 119 ms 59976 KB Output is correct
38 Correct 318 ms 44888 KB Output is correct
39 Correct 282 ms 44568 KB Output is correct
40 Correct 287 ms 45372 KB Output is correct
41 Correct 191 ms 48464 KB Output is correct
42 Correct 185 ms 48416 KB Output is correct
43 Correct 203 ms 48468 KB Output is correct
44 Correct 202 ms 48408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14612 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 7 ms 14564 KB Output is correct
4 Correct 6 ms 14428 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 8 ms 14564 KB Output is correct
7 Correct 6 ms 14424 KB Output is correct
8 Correct 6 ms 14428 KB Output is correct
9 Correct 7 ms 14844 KB Output is correct
10 Correct 8 ms 14684 KB Output is correct
11 Correct 7 ms 14736 KB Output is correct
12 Correct 11 ms 14756 KB Output is correct
13 Correct 11 ms 14684 KB Output is correct
14 Correct 8 ms 14684 KB Output is correct
15 Correct 7 ms 14680 KB Output is correct
16 Correct 7 ms 14684 KB Output is correct
17 Correct 795 ms 50512 KB Output is correct
18 Correct 844 ms 50768 KB Output is correct
19 Correct 819 ms 50436 KB Output is correct
20 Correct 742 ms 50648 KB Output is correct
21 Correct 684 ms 50616 KB Output is correct
22 Correct 914 ms 51400 KB Output is correct
23 Correct 774 ms 51464 KB Output is correct
24 Correct 766 ms 51704 KB Output is correct
25 Correct 768 ms 51960 KB Output is correct
26 Correct 719 ms 51096 KB Output is correct
27 Correct 672 ms 51088 KB Output is correct
28 Correct 702 ms 51536 KB Output is correct
29 Correct 663 ms 51188 KB Output is correct
30 Correct 743 ms 51024 KB Output is correct
31 Correct 811 ms 51092 KB Output is correct
32 Correct 765 ms 51540 KB Output is correct
33 Correct 248 ms 44988 KB Output is correct
34 Correct 127 ms 45708 KB Output is correct
35 Correct 187 ms 59732 KB Output is correct
36 Correct 186 ms 60000 KB Output is correct
37 Correct 175 ms 59724 KB Output is correct
38 Correct 200 ms 59728 KB Output is correct
39 Correct 172 ms 59732 KB Output is correct
40 Correct 171 ms 59984 KB Output is correct
41 Correct 174 ms 59728 KB Output is correct
42 Correct 176 ms 60316 KB Output is correct
43 Correct 188 ms 59984 KB Output is correct
44 Correct 226 ms 45144 KB Output is correct
45 Correct 194 ms 60076 KB Output is correct
46 Correct 88 ms 56656 KB Output is correct
47 Correct 95 ms 56552 KB Output is correct
48 Correct 199 ms 45084 KB Output is correct
49 Correct 200 ms 44112 KB Output is correct
50 Correct 223 ms 44864 KB Output is correct
51 Correct 122 ms 45852 KB Output is correct
52 Correct 145 ms 45800 KB Output is correct
53 Correct 119 ms 45904 KB Output is correct
54 Correct 126 ms 45908 KB Output is correct
55 Correct 313 ms 45328 KB Output is correct
56 Correct 188 ms 48468 KB Output is correct
57 Correct 150 ms 43344 KB Output is correct
58 Correct 169 ms 41552 KB Output is correct
59 Correct 153 ms 41808 KB Output is correct
60 Correct 169 ms 43980 KB Output is correct
61 Correct 141 ms 41808 KB Output is correct
62 Correct 161 ms 42840 KB Output is correct
63 Correct 152 ms 42496 KB Output is correct
64 Correct 152 ms 42064 KB Output is correct
65 Correct 161 ms 43064 KB Output is correct
66 Correct 292 ms 44804 KB Output is correct
67 Correct 146 ms 42576 KB Output is correct
68 Correct 111 ms 59212 KB Output is correct
69 Correct 119 ms 59976 KB Output is correct
70 Correct 318 ms 44888 KB Output is correct
71 Correct 282 ms 44568 KB Output is correct
72 Correct 287 ms 45372 KB Output is correct
73 Correct 191 ms 48464 KB Output is correct
74 Correct 185 ms 48416 KB Output is correct
75 Correct 203 ms 48468 KB Output is correct
76 Correct 202 ms 48408 KB Output is correct
77 Correct 607 ms 47880 KB Output is correct
78 Correct 440 ms 50776 KB Output is correct
79 Correct 433 ms 62192 KB Output is correct
80 Correct 410 ms 62008 KB Output is correct
81 Correct 422 ms 62288 KB Output is correct
82 Correct 406 ms 61784 KB Output is correct
83 Correct 398 ms 62548 KB Output is correct
84 Correct 403 ms 62600 KB Output is correct
85 Correct 378 ms 62172 KB Output is correct
86 Correct 421 ms 62544 KB Output is correct
87 Correct 412 ms 62292 KB Output is correct
88 Correct 588 ms 47596 KB Output is correct
89 Correct 406 ms 62356 KB Output is correct
90 Correct 225 ms 67100 KB Output is correct
91 Correct 232 ms 71756 KB Output is correct
92 Correct 595 ms 47120 KB Output is correct
93 Correct 517 ms 46620 KB Output is correct
94 Correct 497 ms 46344 KB Output is correct
95 Correct 454 ms 50508 KB Output is correct
96 Correct 499 ms 50808 KB Output is correct
97 Correct 413 ms 50476 KB Output is correct
98 Correct 459 ms 50768 KB Output is correct