Submission #367663

# Submission time Handle Problem Language Result Execution time Memory
367663 2021-02-17T22:27:17 Z 534351 Aesthetic (NOI20_aesthetic) C++17
100 / 100
652 ms 59344 KB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)

const int MAXN = 3e5 + 13;
const long long LLINF = 3e18;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef pair<pll, int> ppi;

int N, M, C, T;
vector<ppi> edge[MAXN];
ll dist[2][MAXN];
ll D;
int st[MAXN], ft[MAXN], parent[MAXN], flagged[MAXN];
bitset<MAXN> seen;

void dijkstra(int u)
{
    bool b = (u > 0);
    fill(dist[b], dist[b] + N + 1, LLINF);
    dist[b][u] = 0;
    priority_queue<pll, vector<pll>, greater<pll> > pq;
    pq.push({0, u});
    while(!pq.empty())
    {
        ll d = pq.top().fi; int u = pq.top().se; pq.pop();
        if (d != dist[b][u]) continue;
        for (auto e : edge[u])
        {
            int v = e.se; ll nd = d + e.fi.fi;
            if (nd < dist[b][v])
            {
                dist[b][v] = nd;
                pq.push({nd, v});
            }
        }
    }
}

void dfs(int u, int p)
{
    st[u] = T;
    ft[u] = T;
    T++;
    seen[u] = true;
    for (auto e : edge[u])
    {
        int v = e.se; ll d = e.fi.fi;
        if (dist[0][u] + d + dist[1][v] >= D && dist[1][u] + d + dist[0][v] >= D) continue;
        //oh no it is part of a path shorter than D, so the edge actually exists.
        if (v == p) continue;
        if (seen[v])
        {
            if (st[v] > st[u]) continue;
            ckmin(ft[u], st[v]);
            continue;
        }
        parent[v] = u;
        dfs(v, u);
        ckmin(ft[u], ft[v]);
        if (ft[v] > st[u])
        {
            flagged[v]++;
        }
    }
}

bool check()
{
    //can you make the dist >D?
    //ans is YES if there's a bridge & this thing extends dist by more than D.
    T = 0;
    fill(parent, parent + N, N);
    fill(st, st + N, 0);
    fill(ft, ft + N, 0);
    fill(flagged, flagged + N, 0);
    seen.reset();
    dfs(0, N);
    if (!seen[N - 1]) return false;
    for (int i = N - 1; i != 0; i = parent[i])
    {
        flagged[i]++;
    }
    FOR(u, 0, N)
    {
        for (auto e : edge[u])
        {
            int v = e.se; ll mx = e.fi.se;
            if (u != parent[v]) continue;
            if (flagged[v] != 2) continue;
            // cerr << u << ' ' << v << ' ' << mx << ' ' << dist[0][u] + mx + dist[1][v] << ' ' << dist[1][u] + mx + dist[0][v] << endl;
            if (dist[0][u] + mx + dist[1][v] >= D && dist[1][u] + mx + dist[0][v] >= D) return true;
        }
    }
    return false;
    //now just check if there's something we can extend that is a bridge.
}

int32_t main()
{
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(4);
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N >> M;
    vector<array<int, 3> > eds;
    FOR(i, 0, M)
    {
        int a, b, w;
        cin >> a >> b >> w; a--; b--;
        if (a == b) continue;
        eds.PB({a, b, w});
    }
    reverse(ALL(eds));
    int mx = 0;
    for (auto e : eds)
    {
        int a = e[0], b = e[1], w = e[2];
        edge[a].PB({{w, w + mx}, b});
        edge[b].PB({{w, w + mx}, a});
        ckmax(mx, w);
    }
    dijkstra(0);
    dijkstra(N - 1);
    ll lo = dist[0][N - 1], hi = lo + mx + 69;
    // cerr << mx << ' '<< lo << ' ' << hi << endl;
    while(hi > lo)
    {
        D = (hi + lo + 1) >> 1;
        if (check()) lo = D;
        else hi = D - 1;
    }
    cout << lo << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
9 Correct 9 ms 7660 KB Output is correct
10 Correct 9 ms 7660 KB Output is correct
11 Correct 7 ms 7660 KB Output is correct
12 Correct 7 ms 7660 KB Output is correct
13 Correct 7 ms 7660 KB Output is correct
14 Correct 7 ms 7660 KB Output is correct
15 Correct 7 ms 7660 KB Output is correct
16 Correct 7 ms 7660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 565 ms 39876 KB Output is correct
2 Correct 557 ms 40016 KB Output is correct
3 Correct 562 ms 39716 KB Output is correct
4 Correct 551 ms 39760 KB Output is correct
5 Correct 572 ms 39888 KB Output is correct
6 Correct 582 ms 47312 KB Output is correct
7 Correct 564 ms 47212 KB Output is correct
8 Correct 573 ms 47596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 584 ms 40656 KB Output is correct
2 Correct 580 ms 46672 KB Output is correct
3 Correct 560 ms 46672 KB Output is correct
4 Correct 543 ms 47312 KB Output is correct
5 Correct 569 ms 46544 KB Output is correct
6 Correct 557 ms 46672 KB Output is correct
7 Correct 559 ms 46672 KB Output is correct
8 Correct 555 ms 47056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 642 ms 47056 KB Output is correct
2 Correct 284 ms 37968 KB Output is correct
3 Correct 452 ms 41040 KB Output is correct
4 Correct 455 ms 41168 KB Output is correct
5 Correct 469 ms 41040 KB Output is correct
6 Correct 465 ms 41168 KB Output is correct
7 Correct 522 ms 41216 KB Output is correct
8 Correct 489 ms 41296 KB Output is correct
9 Correct 496 ms 41040 KB Output is correct
10 Correct 469 ms 41580 KB Output is correct
11 Correct 482 ms 41168 KB Output is correct
12 Correct 595 ms 47824 KB Output is correct
13 Correct 463 ms 41244 KB Output is correct
14 Correct 211 ms 54736 KB Output is correct
15 Correct 204 ms 59344 KB Output is correct
16 Correct 636 ms 52432 KB Output is correct
17 Correct 588 ms 51268 KB Output is correct
18 Correct 614 ms 52060 KB Output is correct
19 Correct 282 ms 42448 KB Output is correct
20 Correct 293 ms 42448 KB Output is correct
21 Correct 284 ms 42448 KB Output is correct
22 Correct 291 ms 42448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 642 ms 47056 KB Output is correct
2 Correct 284 ms 37968 KB Output is correct
3 Correct 452 ms 41040 KB Output is correct
4 Correct 455 ms 41168 KB Output is correct
5 Correct 469 ms 41040 KB Output is correct
6 Correct 465 ms 41168 KB Output is correct
7 Correct 522 ms 41216 KB Output is correct
8 Correct 489 ms 41296 KB Output is correct
9 Correct 496 ms 41040 KB Output is correct
10 Correct 469 ms 41580 KB Output is correct
11 Correct 482 ms 41168 KB Output is correct
12 Correct 595 ms 47824 KB Output is correct
13 Correct 463 ms 41244 KB Output is correct
14 Correct 211 ms 54736 KB Output is correct
15 Correct 204 ms 59344 KB Output is correct
16 Correct 636 ms 52432 KB Output is correct
17 Correct 588 ms 51268 KB Output is correct
18 Correct 614 ms 52060 KB Output is correct
19 Correct 282 ms 42448 KB Output is correct
20 Correct 293 ms 42448 KB Output is correct
21 Correct 284 ms 42448 KB Output is correct
22 Correct 291 ms 42448 KB Output is correct
23 Correct 527 ms 46672 KB Output is correct
24 Correct 309 ms 42576 KB Output is correct
25 Correct 392 ms 45008 KB Output is correct
26 Correct 388 ms 45292 KB Output is correct
27 Correct 392 ms 45136 KB Output is correct
28 Correct 387 ms 45384 KB Output is correct
29 Correct 374 ms 45264 KB Output is correct
30 Correct 381 ms 45284 KB Output is correct
31 Correct 390 ms 45520 KB Output is correct
32 Correct 391 ms 45072 KB Output is correct
33 Correct 377 ms 45520 KB Output is correct
34 Correct 490 ms 45648 KB Output is correct
35 Correct 381 ms 45648 KB Output is correct
36 Correct 203 ms 55208 KB Output is correct
37 Correct 210 ms 55848 KB Output is correct
38 Correct 485 ms 45744 KB Output is correct
39 Correct 494 ms 45776 KB Output is correct
40 Correct 501 ms 47056 KB Output is correct
41 Correct 310 ms 42504 KB Output is correct
42 Correct 307 ms 42448 KB Output is correct
43 Correct 312 ms 42576 KB Output is correct
44 Correct 303 ms 42448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 5 ms 7404 KB Output is correct
7 Correct 5 ms 7404 KB Output is correct
8 Correct 5 ms 7404 KB Output is correct
9 Correct 9 ms 7660 KB Output is correct
10 Correct 9 ms 7660 KB Output is correct
11 Correct 7 ms 7660 KB Output is correct
12 Correct 7 ms 7660 KB Output is correct
13 Correct 7 ms 7660 KB Output is correct
14 Correct 7 ms 7660 KB Output is correct
15 Correct 7 ms 7660 KB Output is correct
16 Correct 7 ms 7660 KB Output is correct
17 Correct 565 ms 39876 KB Output is correct
18 Correct 557 ms 40016 KB Output is correct
19 Correct 562 ms 39716 KB Output is correct
20 Correct 551 ms 39760 KB Output is correct
21 Correct 572 ms 39888 KB Output is correct
22 Correct 582 ms 47312 KB Output is correct
23 Correct 564 ms 47212 KB Output is correct
24 Correct 573 ms 47596 KB Output is correct
25 Correct 584 ms 40656 KB Output is correct
26 Correct 580 ms 46672 KB Output is correct
27 Correct 560 ms 46672 KB Output is correct
28 Correct 543 ms 47312 KB Output is correct
29 Correct 569 ms 46544 KB Output is correct
30 Correct 557 ms 46672 KB Output is correct
31 Correct 559 ms 46672 KB Output is correct
32 Correct 555 ms 47056 KB Output is correct
33 Correct 642 ms 47056 KB Output is correct
34 Correct 284 ms 37968 KB Output is correct
35 Correct 452 ms 41040 KB Output is correct
36 Correct 455 ms 41168 KB Output is correct
37 Correct 469 ms 41040 KB Output is correct
38 Correct 465 ms 41168 KB Output is correct
39 Correct 522 ms 41216 KB Output is correct
40 Correct 489 ms 41296 KB Output is correct
41 Correct 496 ms 41040 KB Output is correct
42 Correct 469 ms 41580 KB Output is correct
43 Correct 482 ms 41168 KB Output is correct
44 Correct 595 ms 47824 KB Output is correct
45 Correct 463 ms 41244 KB Output is correct
46 Correct 211 ms 54736 KB Output is correct
47 Correct 204 ms 59344 KB Output is correct
48 Correct 636 ms 52432 KB Output is correct
49 Correct 588 ms 51268 KB Output is correct
50 Correct 614 ms 52060 KB Output is correct
51 Correct 282 ms 42448 KB Output is correct
52 Correct 293 ms 42448 KB Output is correct
53 Correct 284 ms 42448 KB Output is correct
54 Correct 291 ms 42448 KB Output is correct
55 Correct 527 ms 46672 KB Output is correct
56 Correct 309 ms 42576 KB Output is correct
57 Correct 392 ms 45008 KB Output is correct
58 Correct 388 ms 45292 KB Output is correct
59 Correct 392 ms 45136 KB Output is correct
60 Correct 387 ms 45384 KB Output is correct
61 Correct 374 ms 45264 KB Output is correct
62 Correct 381 ms 45284 KB Output is correct
63 Correct 390 ms 45520 KB Output is correct
64 Correct 391 ms 45072 KB Output is correct
65 Correct 377 ms 45520 KB Output is correct
66 Correct 490 ms 45648 KB Output is correct
67 Correct 381 ms 45648 KB Output is correct
68 Correct 203 ms 55208 KB Output is correct
69 Correct 210 ms 55848 KB Output is correct
70 Correct 485 ms 45744 KB Output is correct
71 Correct 494 ms 45776 KB Output is correct
72 Correct 501 ms 47056 KB Output is correct
73 Correct 310 ms 42504 KB Output is correct
74 Correct 307 ms 42448 KB Output is correct
75 Correct 312 ms 42576 KB Output is correct
76 Correct 303 ms 42448 KB Output is correct
77 Correct 522 ms 48336 KB Output is correct
78 Correct 491 ms 44752 KB Output is correct
79 Correct 554 ms 47568 KB Output is correct
80 Correct 526 ms 47568 KB Output is correct
81 Correct 556 ms 47632 KB Output is correct
82 Correct 508 ms 47384 KB Output is correct
83 Correct 526 ms 47696 KB Output is correct
84 Correct 567 ms 47940 KB Output is correct
85 Correct 529 ms 47536 KB Output is correct
86 Correct 519 ms 47568 KB Output is correct
87 Correct 527 ms 47824 KB Output is correct
88 Correct 651 ms 46672 KB Output is correct
89 Correct 539 ms 47696 KB Output is correct
90 Correct 291 ms 55720 KB Output is correct
91 Correct 298 ms 56488 KB Output is correct
92 Correct 633 ms 48208 KB Output is correct
93 Correct 638 ms 47696 KB Output is correct
94 Correct 652 ms 47460 KB Output is correct
95 Correct 489 ms 44752 KB Output is correct
96 Correct 492 ms 44752 KB Output is correct
97 Correct 506 ms 44756 KB Output is correct
98 Correct 486 ms 44752 KB Output is correct