Submission #367661

# Submission time Handle Problem Language Result Execution time Memory
367661 2021-02-17T22:23:33 Z 534351 Aesthetic (NOI20_aesthetic) C++17
13 / 100
2000 ms 47240 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();
    FOR(i, 0, N)
    {
        if (seen[i]) continue;
        if (i == N - 1) return false;
        dfs(i, N);
    }
    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 = 1e15;
    // 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 6 ms 7404 KB Output is correct
2 Correct 6 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 6 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 7 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 6 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 6 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 7 ms 7404 KB Output is correct
9 Correct 10 ms 7788 KB Output is correct
10 Correct 10 ms 7660 KB Output is correct
11 Correct 9 ms 7660 KB Output is correct
12 Correct 9 ms 7660 KB Output is correct
13 Correct 9 ms 7660 KB Output is correct
14 Correct 11 ms 7660 KB Output is correct
15 Correct 9 ms 7660 KB Output is correct
16 Correct 9 ms 7660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2071 ms 39948 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2069 ms 40656 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2055 ms 47240 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2055 ms 47240 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 6 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 6 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 7 ms 7404 KB Output is correct
9 Correct 10 ms 7788 KB Output is correct
10 Correct 10 ms 7660 KB Output is correct
11 Correct 9 ms 7660 KB Output is correct
12 Correct 9 ms 7660 KB Output is correct
13 Correct 9 ms 7660 KB Output is correct
14 Correct 11 ms 7660 KB Output is correct
15 Correct 9 ms 7660 KB Output is correct
16 Correct 9 ms 7660 KB Output is correct
17 Execution timed out 2071 ms 39948 KB Time limit exceeded
18 Halted 0 ms 0 KB -