Submission #834588

# Submission time Handle Problem Language Result Execution time Memory
834588 2023-08-22T15:33:49 Z _martynas Aesthetic (NOI20_aesthetic) C++11
100 / 100
512 ms 91704 KB
#include <bits/stdc++.h>
    
using namespace std;
    
using ll = long long;
    
const int mxn = 3e5+5;
const ll inf = 1e16;
    
struct Edge {
    int to, id;
};
    
int n, m;
int A[mxn], B[mxn];
vector<Edge> adj[mxn];
ll W[mxn];
vector<Edge> adj2[mxn];
bool isbridge[mxn], visited[mxn];
vector<ll> from1, fromn;
    
vector<ll> get_min_dist(int start) {
    vector<long long> dist(n+1, inf);
    priority_queue<pair<ll, int>> pq;
    dist[start] = 0;
    pq.push({-dist[start], start});;
    while(!pq.empty()) {
        ll d = -pq.top().first;
        int a = pq.top().second;
        pq.pop();
        if(dist[a] != d) continue;
        for(auto e : adj[a]) if(dist[e.to] > d+W[e.id]) {
            dist[e.to] = d+W[e.id];
            pq.push({-dist[e.to], e.to});
        }
    }
    return dist;
}
    
    
int tim = 0;
int disc[mxn], low[mxn];
bool onpathton[mxn];
void bridge_det(int u, int par) {
    visited[u] = true;
    if(u == n) onpathton[u] = true;
    low[u] = disc[u] = tim++;
    for(auto e : adj2[u]) if(e.to != par) {
        if(!visited[e.to]) {
            bridge_det(e.to, u);
            if(low[e.to] > disc[u] && onpathton[e.to]) {
                isbridge[e.id] = true;
            }
        }
        onpathton[u] = onpathton[u] || onpathton[e.to];
        low[u] = min(low[u], low[e.to]);
    }
}
    
int main(int argc, char const *argv[]) {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= m; i++) {
        cin >> A[i] >> B[i] >> W[i];
        adj[A[i]].push_back({B[i], i});
        adj[B[i]].push_back({A[i], i});
    }
    from1 = get_min_dist(1);
    fromn = get_min_dist(n);
    ll mxw = W[m];
    vector<ll> ans(m+1);
    for(int i = m-1; i >= 1; i--) {
        ll nw = W[i]+mxw;
        ll mndist = inf;
        mndist = min(mndist, from1[A[i]]+fromn[B[i]]+nw);
        mndist = min(mndist, from1[B[i]]+fromn[A[i]]+nw);
        ans[i] = mndist;
        mxw = max(mxw, W[i]);
    }
    // build new graph where every path is min
    for(int i = 1; i <= m; i++) {
        if(from1[A[i]]+fromn[B[i]]+W[i] == from1[n] || from1[B[i]]+fromn[A[i]]+W[i] == from1[n]) {
            adj2[A[i]].push_back({B[i], i});
            adj2[B[i]].push_back({A[i], i});
        }
    }
    fill(low, low+n+1, mxn);
    bridge_det(1, -1);
    vector<int> cnt_to_id(m+1);
    fill(visited, visited+n+1, false);
    queue<int> q;
    visited[1] = true; q.push(1);
    int c = 0;
    while(!q.empty()) {
        int u = q.front(); q.pop();
        for(auto e : adj2[u]) if(!visited[e.to]) {
            visited[e.to] = true;
            c += isbridge[e.id];
            if(isbridge[e.id]) cnt_to_id[c] = e.id;
            q.push(e.to);
        }
    }
    vector<long long> dist(n+1, inf);
    vector<int> cnt(n+1);
    priority_queue<pair<pair<ll, int>, int>> pq;
    vector<Edge> used_edge; vector<int> from;
    dist[1] = 0, cnt[1] = 0;
    pq.push({{-dist[1], 1}, -1});
    vector<vector<ll>> ins(m+1), del(m+1);
    while(!pq.empty()) {
        ll d = -pq.top().first.first;
        int a = pq.top().first.second;
        int edgeidx = pq.top().second;
        pq.pop();
        if(edgeidx != -1) {
            if(cnt[used_edge[edgeidx].to] > cnt[from[edgeidx]]+isbridge[used_edge[edgeidx].id]) {
                int l = cnt[from[edgeidx]]+isbridge[used_edge[edgeidx].id]+1, r = cnt[a];
                ll val = from1[from[edgeidx]]+W[used_edge[edgeidx].id]+fromn[a];
                ins[l].push_back(val);
                del[r].push_back(val);
            }
        }
        if(dist[a] != d) continue;
        for(auto e : adj[a]) {
            if(dist[e.to] > d+W[e.id]) {
                cnt[e.to] = cnt[a]+isbridge[e.id];
                dist[e.to] = d+W[e.id];
                pq.push({{-dist[e.to], e.to}, used_edge.size()});
                used_edge.push_back(e); from.push_back(a);
            }
            else {
                if(cnt[e.to] > cnt[a]+isbridge[e.id]) {
                    int l = cnt[a]+isbridge[e.id]+1, r = cnt[e.to];
                    ll val = from1[a]+W[e.id]+fromn[e.to];
                    ins[l].push_back(val);
                    del[r].push_back(val);
                }
            }
        }
    }
    multiset<ll> available;
    for(int i = 0; i <= m; i++) {
        for(ll x : ins[i]) available.insert(x);
        if(cnt_to_id[i] != 0 && !available.empty()) {
            ans[cnt_to_id[i]] = min(ans[cnt_to_id[i]], *available.begin());
        }
        for(ll x : del[i]) available.erase(available.find(x));
    }
    ll answer = 0;
    for(int i = 1; i < m; i++) {
        answer = max(answer, isbridge[i] ? ans[i] : from1[n]);
    }
    cout << answer << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14436 KB Output is correct
3 Correct 6 ms 14432 KB Output is correct
4 Correct 7 ms 14436 KB Output is correct
5 Correct 7 ms 14432 KB Output is correct
6 Correct 7 ms 14424 KB Output is correct
7 Correct 7 ms 14440 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14436 KB Output is correct
3 Correct 6 ms 14432 KB Output is correct
4 Correct 7 ms 14436 KB Output is correct
5 Correct 7 ms 14432 KB Output is correct
6 Correct 7 ms 14424 KB Output is correct
7 Correct 7 ms 14440 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 9 ms 14832 KB Output is correct
10 Correct 10 ms 14708 KB Output is correct
11 Correct 8 ms 14804 KB Output is correct
12 Correct 8 ms 14708 KB Output is correct
13 Correct 8 ms 14676 KB Output is correct
14 Correct 11 ms 14700 KB Output is correct
15 Correct 8 ms 14704 KB Output is correct
16 Correct 8 ms 14696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 480 ms 65768 KB Output is correct
2 Correct 454 ms 65856 KB Output is correct
3 Correct 493 ms 65568 KB Output is correct
4 Correct 395 ms 65472 KB Output is correct
5 Correct 439 ms 66096 KB Output is correct
6 Correct 506 ms 66960 KB Output is correct
7 Correct 457 ms 66544 KB Output is correct
8 Correct 468 ms 66932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 485 ms 67092 KB Output is correct
2 Correct 446 ms 66368 KB Output is correct
3 Correct 469 ms 65736 KB Output is correct
4 Correct 436 ms 66108 KB Output is correct
5 Correct 448 ms 66156 KB Output is correct
6 Correct 485 ms 66232 KB Output is correct
7 Correct 512 ms 66356 KB Output is correct
8 Correct 447 ms 66792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 332 ms 60036 KB Output is correct
2 Correct 192 ms 61352 KB Output is correct
3 Correct 238 ms 68152 KB Output is correct
4 Correct 234 ms 68220 KB Output is correct
5 Correct 240 ms 68120 KB Output is correct
6 Correct 247 ms 68332 KB Output is correct
7 Correct 232 ms 68280 KB Output is correct
8 Correct 264 ms 68704 KB Output is correct
9 Correct 236 ms 68224 KB Output is correct
10 Correct 232 ms 68760 KB Output is correct
11 Correct 251 ms 68464 KB Output is correct
12 Correct 351 ms 61952 KB Output is correct
13 Correct 261 ms 68448 KB Output is correct
14 Correct 144 ms 78904 KB Output is correct
15 Correct 130 ms 76476 KB Output is correct
16 Correct 360 ms 60316 KB Output is correct
17 Correct 341 ms 60944 KB Output is correct
18 Correct 357 ms 59824 KB Output is correct
19 Correct 186 ms 61368 KB Output is correct
20 Correct 186 ms 61488 KB Output is correct
21 Correct 184 ms 61460 KB Output is correct
22 Correct 188 ms 61076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 332 ms 60036 KB Output is correct
2 Correct 192 ms 61352 KB Output is correct
3 Correct 238 ms 68152 KB Output is correct
4 Correct 234 ms 68220 KB Output is correct
5 Correct 240 ms 68120 KB Output is correct
6 Correct 247 ms 68332 KB Output is correct
7 Correct 232 ms 68280 KB Output is correct
8 Correct 264 ms 68704 KB Output is correct
9 Correct 236 ms 68224 KB Output is correct
10 Correct 232 ms 68760 KB Output is correct
11 Correct 251 ms 68464 KB Output is correct
12 Correct 351 ms 61952 KB Output is correct
13 Correct 261 ms 68448 KB Output is correct
14 Correct 144 ms 78904 KB Output is correct
15 Correct 130 ms 76476 KB Output is correct
16 Correct 360 ms 60316 KB Output is correct
17 Correct 341 ms 60944 KB Output is correct
18 Correct 357 ms 59824 KB Output is correct
19 Correct 186 ms 61368 KB Output is correct
20 Correct 186 ms 61488 KB Output is correct
21 Correct 184 ms 61460 KB Output is correct
22 Correct 188 ms 61076 KB Output is correct
23 Correct 456 ms 63532 KB Output is correct
24 Correct 223 ms 63592 KB Output is correct
25 Correct 189 ms 52152 KB Output is correct
26 Correct 220 ms 55452 KB Output is correct
27 Correct 224 ms 54020 KB Output is correct
28 Correct 190 ms 53184 KB Output is correct
29 Correct 221 ms 55956 KB Output is correct
30 Correct 198 ms 53512 KB Output is correct
31 Correct 201 ms 54472 KB Output is correct
32 Correct 206 ms 54136 KB Output is correct
33 Correct 188 ms 53080 KB Output is correct
34 Correct 435 ms 62984 KB Output is correct
35 Correct 194 ms 53736 KB Output is correct
36 Correct 208 ms 91704 KB Output is correct
37 Correct 146 ms 73956 KB Output is correct
38 Correct 417 ms 61936 KB Output is correct
39 Correct 422 ms 62236 KB Output is correct
40 Correct 421 ms 63160 KB Output is correct
41 Correct 228 ms 63420 KB Output is correct
42 Correct 225 ms 63388 KB Output is correct
43 Correct 228 ms 63404 KB Output is correct
44 Correct 223 ms 63440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14436 KB Output is correct
3 Correct 6 ms 14432 KB Output is correct
4 Correct 7 ms 14436 KB Output is correct
5 Correct 7 ms 14432 KB Output is correct
6 Correct 7 ms 14424 KB Output is correct
7 Correct 7 ms 14440 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 9 ms 14832 KB Output is correct
10 Correct 10 ms 14708 KB Output is correct
11 Correct 8 ms 14804 KB Output is correct
12 Correct 8 ms 14708 KB Output is correct
13 Correct 8 ms 14676 KB Output is correct
14 Correct 11 ms 14700 KB Output is correct
15 Correct 8 ms 14704 KB Output is correct
16 Correct 8 ms 14696 KB Output is correct
17 Correct 480 ms 65768 KB Output is correct
18 Correct 454 ms 65856 KB Output is correct
19 Correct 493 ms 65568 KB Output is correct
20 Correct 395 ms 65472 KB Output is correct
21 Correct 439 ms 66096 KB Output is correct
22 Correct 506 ms 66960 KB Output is correct
23 Correct 457 ms 66544 KB Output is correct
24 Correct 468 ms 66932 KB Output is correct
25 Correct 485 ms 67092 KB Output is correct
26 Correct 446 ms 66368 KB Output is correct
27 Correct 469 ms 65736 KB Output is correct
28 Correct 436 ms 66108 KB Output is correct
29 Correct 448 ms 66156 KB Output is correct
30 Correct 485 ms 66232 KB Output is correct
31 Correct 512 ms 66356 KB Output is correct
32 Correct 447 ms 66792 KB Output is correct
33 Correct 332 ms 60036 KB Output is correct
34 Correct 192 ms 61352 KB Output is correct
35 Correct 238 ms 68152 KB Output is correct
36 Correct 234 ms 68220 KB Output is correct
37 Correct 240 ms 68120 KB Output is correct
38 Correct 247 ms 68332 KB Output is correct
39 Correct 232 ms 68280 KB Output is correct
40 Correct 264 ms 68704 KB Output is correct
41 Correct 236 ms 68224 KB Output is correct
42 Correct 232 ms 68760 KB Output is correct
43 Correct 251 ms 68464 KB Output is correct
44 Correct 351 ms 61952 KB Output is correct
45 Correct 261 ms 68448 KB Output is correct
46 Correct 144 ms 78904 KB Output is correct
47 Correct 130 ms 76476 KB Output is correct
48 Correct 360 ms 60316 KB Output is correct
49 Correct 341 ms 60944 KB Output is correct
50 Correct 357 ms 59824 KB Output is correct
51 Correct 186 ms 61368 KB Output is correct
52 Correct 186 ms 61488 KB Output is correct
53 Correct 184 ms 61460 KB Output is correct
54 Correct 188 ms 61076 KB Output is correct
55 Correct 456 ms 63532 KB Output is correct
56 Correct 223 ms 63592 KB Output is correct
57 Correct 189 ms 52152 KB Output is correct
58 Correct 220 ms 55452 KB Output is correct
59 Correct 224 ms 54020 KB Output is correct
60 Correct 190 ms 53184 KB Output is correct
61 Correct 221 ms 55956 KB Output is correct
62 Correct 198 ms 53512 KB Output is correct
63 Correct 201 ms 54472 KB Output is correct
64 Correct 206 ms 54136 KB Output is correct
65 Correct 188 ms 53080 KB Output is correct
66 Correct 435 ms 62984 KB Output is correct
67 Correct 194 ms 53736 KB Output is correct
68 Correct 208 ms 91704 KB Output is correct
69 Correct 146 ms 73956 KB Output is correct
70 Correct 417 ms 61936 KB Output is correct
71 Correct 422 ms 62236 KB Output is correct
72 Correct 421 ms 63160 KB Output is correct
73 Correct 228 ms 63420 KB Output is correct
74 Correct 225 ms 63388 KB Output is correct
75 Correct 228 ms 63404 KB Output is correct
76 Correct 223 ms 63440 KB Output is correct
77 Correct 442 ms 62980 KB Output is correct
78 Correct 239 ms 63036 KB Output is correct
79 Correct 236 ms 55956 KB Output is correct
80 Correct 232 ms 55188 KB Output is correct
81 Correct 228 ms 55504 KB Output is correct
82 Correct 232 ms 55560 KB Output is correct
83 Correct 236 ms 55600 KB Output is correct
84 Correct 229 ms 54612 KB Output is correct
85 Correct 232 ms 55968 KB Output is correct
86 Correct 236 ms 55472 KB Output is correct
87 Correct 230 ms 55052 KB Output is correct
88 Correct 425 ms 61884 KB Output is correct
89 Correct 240 ms 55680 KB Output is correct
90 Correct 217 ms 91100 KB Output is correct
91 Correct 148 ms 73404 KB Output is correct
92 Correct 413 ms 63188 KB Output is correct
93 Correct 420 ms 61076 KB Output is correct
94 Correct 398 ms 60500 KB Output is correct
95 Correct 222 ms 62508 KB Output is correct
96 Correct 228 ms 62364 KB Output is correct
97 Correct 225 ms 62420 KB Output is correct
98 Correct 226 ms 62332 KB Output is correct