답안 #834587

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
834587 2023-08-22T15:33:02 Z _martynas Aesthetic (NOI20_aesthetic) C++11
100 / 100
710 ms 94484 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[]) {
    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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14404 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14416 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 7 ms 14424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14404 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14416 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 7 ms 14424 KB Output is correct
9 Correct 11 ms 14804 KB Output is correct
10 Correct 9 ms 14676 KB Output is correct
11 Correct 10 ms 14684 KB Output is correct
12 Correct 9 ms 14676 KB Output is correct
13 Correct 10 ms 14676 KB Output is correct
14 Correct 10 ms 14676 KB Output is correct
15 Correct 10 ms 14768 KB Output is correct
16 Correct 11 ms 14676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 625 ms 71260 KB Output is correct
2 Correct 663 ms 71436 KB Output is correct
3 Correct 597 ms 70968 KB Output is correct
4 Correct 612 ms 71044 KB Output is correct
5 Correct 593 ms 71228 KB Output is correct
6 Correct 647 ms 72300 KB Output is correct
7 Correct 613 ms 72148 KB Output is correct
8 Correct 634 ms 72508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 676 ms 72432 KB Output is correct
2 Correct 710 ms 71580 KB Output is correct
3 Correct 548 ms 70984 KB Output is correct
4 Correct 611 ms 71528 KB Output is correct
5 Correct 614 ms 71324 KB Output is correct
6 Correct 602 ms 71452 KB Output is correct
7 Correct 630 ms 71476 KB Output is correct
8 Correct 602 ms 71968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 443 ms 62576 KB Output is correct
2 Correct 303 ms 64248 KB Output is correct
3 Correct 361 ms 70584 KB Output is correct
4 Correct 349 ms 70684 KB Output is correct
5 Correct 339 ms 70752 KB Output is correct
6 Correct 343 ms 70728 KB Output is correct
7 Correct 346 ms 70744 KB Output is correct
8 Correct 339 ms 70856 KB Output is correct
9 Correct 344 ms 70616 KB Output is correct
10 Correct 354 ms 71172 KB Output is correct
11 Correct 353 ms 70880 KB Output is correct
12 Correct 449 ms 64640 KB Output is correct
13 Correct 354 ms 70912 KB Output is correct
14 Correct 258 ms 81676 KB Output is correct
15 Correct 241 ms 79228 KB Output is correct
16 Correct 471 ms 62984 KB Output is correct
17 Correct 482 ms 63760 KB Output is correct
18 Correct 445 ms 62468 KB Output is correct
19 Correct 298 ms 64232 KB Output is correct
20 Correct 293 ms 64340 KB Output is correct
21 Correct 292 ms 64280 KB Output is correct
22 Correct 293 ms 64264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 443 ms 62576 KB Output is correct
2 Correct 303 ms 64248 KB Output is correct
3 Correct 361 ms 70584 KB Output is correct
4 Correct 349 ms 70684 KB Output is correct
5 Correct 339 ms 70752 KB Output is correct
6 Correct 343 ms 70728 KB Output is correct
7 Correct 346 ms 70744 KB Output is correct
8 Correct 339 ms 70856 KB Output is correct
9 Correct 344 ms 70616 KB Output is correct
10 Correct 354 ms 71172 KB Output is correct
11 Correct 353 ms 70880 KB Output is correct
12 Correct 449 ms 64640 KB Output is correct
13 Correct 354 ms 70912 KB Output is correct
14 Correct 258 ms 81676 KB Output is correct
15 Correct 241 ms 79228 KB Output is correct
16 Correct 471 ms 62984 KB Output is correct
17 Correct 482 ms 63760 KB Output is correct
18 Correct 445 ms 62468 KB Output is correct
19 Correct 298 ms 64232 KB Output is correct
20 Correct 293 ms 64340 KB Output is correct
21 Correct 292 ms 64280 KB Output is correct
22 Correct 293 ms 64264 KB Output is correct
23 Correct 593 ms 66764 KB Output is correct
24 Correct 336 ms 66756 KB Output is correct
25 Correct 291 ms 54848 KB Output is correct
26 Correct 309 ms 58236 KB Output is correct
27 Correct 314 ms 56696 KB Output is correct
28 Correct 306 ms 56000 KB Output is correct
29 Correct 322 ms 58708 KB Output is correct
30 Correct 335 ms 56208 KB Output is correct
31 Correct 328 ms 57276 KB Output is correct
32 Correct 314 ms 56948 KB Output is correct
33 Correct 311 ms 55784 KB Output is correct
34 Correct 509 ms 66148 KB Output is correct
35 Correct 319 ms 56512 KB Output is correct
36 Correct 305 ms 93856 KB Output is correct
37 Correct 253 ms 77000 KB Output is correct
38 Correct 513 ms 64912 KB Output is correct
39 Correct 516 ms 65188 KB Output is correct
40 Correct 545 ms 66184 KB Output is correct
41 Correct 336 ms 66752 KB Output is correct
42 Correct 349 ms 66756 KB Output is correct
43 Correct 333 ms 66736 KB Output is correct
44 Correct 336 ms 66688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14404 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14416 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 7 ms 14424 KB Output is correct
9 Correct 11 ms 14804 KB Output is correct
10 Correct 9 ms 14676 KB Output is correct
11 Correct 10 ms 14684 KB Output is correct
12 Correct 9 ms 14676 KB Output is correct
13 Correct 10 ms 14676 KB Output is correct
14 Correct 10 ms 14676 KB Output is correct
15 Correct 10 ms 14768 KB Output is correct
16 Correct 11 ms 14676 KB Output is correct
17 Correct 625 ms 71260 KB Output is correct
18 Correct 663 ms 71436 KB Output is correct
19 Correct 597 ms 70968 KB Output is correct
20 Correct 612 ms 71044 KB Output is correct
21 Correct 593 ms 71228 KB Output is correct
22 Correct 647 ms 72300 KB Output is correct
23 Correct 613 ms 72148 KB Output is correct
24 Correct 634 ms 72508 KB Output is correct
25 Correct 676 ms 72432 KB Output is correct
26 Correct 710 ms 71580 KB Output is correct
27 Correct 548 ms 70984 KB Output is correct
28 Correct 611 ms 71528 KB Output is correct
29 Correct 614 ms 71324 KB Output is correct
30 Correct 602 ms 71452 KB Output is correct
31 Correct 630 ms 71476 KB Output is correct
32 Correct 602 ms 71968 KB Output is correct
33 Correct 443 ms 62576 KB Output is correct
34 Correct 303 ms 64248 KB Output is correct
35 Correct 361 ms 70584 KB Output is correct
36 Correct 349 ms 70684 KB Output is correct
37 Correct 339 ms 70752 KB Output is correct
38 Correct 343 ms 70728 KB Output is correct
39 Correct 346 ms 70744 KB Output is correct
40 Correct 339 ms 70856 KB Output is correct
41 Correct 344 ms 70616 KB Output is correct
42 Correct 354 ms 71172 KB Output is correct
43 Correct 353 ms 70880 KB Output is correct
44 Correct 449 ms 64640 KB Output is correct
45 Correct 354 ms 70912 KB Output is correct
46 Correct 258 ms 81676 KB Output is correct
47 Correct 241 ms 79228 KB Output is correct
48 Correct 471 ms 62984 KB Output is correct
49 Correct 482 ms 63760 KB Output is correct
50 Correct 445 ms 62468 KB Output is correct
51 Correct 298 ms 64232 KB Output is correct
52 Correct 293 ms 64340 KB Output is correct
53 Correct 292 ms 64280 KB Output is correct
54 Correct 293 ms 64264 KB Output is correct
55 Correct 593 ms 66764 KB Output is correct
56 Correct 336 ms 66756 KB Output is correct
57 Correct 291 ms 54848 KB Output is correct
58 Correct 309 ms 58236 KB Output is correct
59 Correct 314 ms 56696 KB Output is correct
60 Correct 306 ms 56000 KB Output is correct
61 Correct 322 ms 58708 KB Output is correct
62 Correct 335 ms 56208 KB Output is correct
63 Correct 328 ms 57276 KB Output is correct
64 Correct 314 ms 56948 KB Output is correct
65 Correct 311 ms 55784 KB Output is correct
66 Correct 509 ms 66148 KB Output is correct
67 Correct 319 ms 56512 KB Output is correct
68 Correct 305 ms 93856 KB Output is correct
69 Correct 253 ms 77000 KB Output is correct
70 Correct 513 ms 64912 KB Output is correct
71 Correct 516 ms 65188 KB Output is correct
72 Correct 545 ms 66184 KB Output is correct
73 Correct 336 ms 66752 KB Output is correct
74 Correct 349 ms 66756 KB Output is correct
75 Correct 333 ms 66736 KB Output is correct
76 Correct 336 ms 66688 KB Output is correct
77 Correct 651 ms 68512 KB Output is correct
78 Correct 376 ms 68652 KB Output is correct
79 Correct 389 ms 61288 KB Output is correct
80 Correct 402 ms 60552 KB Output is correct
81 Correct 357 ms 60656 KB Output is correct
82 Correct 390 ms 60808 KB Output is correct
83 Correct 394 ms 60852 KB Output is correct
84 Correct 451 ms 60004 KB Output is correct
85 Correct 383 ms 61332 KB Output is correct
86 Correct 388 ms 60876 KB Output is correct
87 Correct 378 ms 60472 KB Output is correct
88 Correct 605 ms 67536 KB Output is correct
89 Correct 398 ms 61244 KB Output is correct
90 Correct 332 ms 94484 KB Output is correct
91 Correct 296 ms 77556 KB Output is correct
92 Correct 670 ms 69236 KB Output is correct
93 Correct 641 ms 66960 KB Output is correct
94 Correct 627 ms 66268 KB Output is correct
95 Correct 409 ms 68772 KB Output is correct
96 Correct 390 ms 68724 KB Output is correct
97 Correct 403 ms 68716 KB Output is correct
98 Correct 438 ms 68700 KB Output is correct