結果

問題 No.2642 Don't cut line!
ユーザー noya2noya2
提出日時 2024-02-15 03:12:19
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 129 ms / 4,000 ms
コード長 2,682 bytes
コンパイル時間 5,634 ms
コンパイル使用メモリ 321,904 KB
実行使用メモリ 50,548 KB
最終ジャッジ日時 2024-02-20 12:45:31
合計ジャッジ時間 9,812 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 125 ms
50,292 KB
testcase_02 AC 122 ms
50,548 KB
testcase_03 AC 121 ms
48,884 KB
testcase_04 AC 120 ms
48,756 KB
testcase_05 AC 120 ms
49,396 KB
testcase_06 AC 45 ms
6,720 KB
testcase_07 AC 47 ms
6,780 KB
testcase_08 AC 45 ms
6,732 KB
testcase_09 AC 45 ms
6,744 KB
testcase_10 AC 45 ms
6,780 KB
testcase_11 AC 45 ms
6,732 KB
testcase_12 AC 47 ms
6,832 KB
testcase_13 AC 45 ms
6,732 KB
testcase_14 AC 45 ms
6,756 KB
testcase_15 AC 49 ms
6,832 KB
testcase_16 AC 81 ms
14,208 KB
testcase_17 AC 96 ms
43,168 KB
testcase_18 AC 105 ms
45,484 KB
testcase_19 AC 82 ms
35,084 KB
testcase_20 AC 52 ms
17,684 KB
testcase_21 AC 45 ms
7,424 KB
testcase_22 AC 54 ms
11,392 KB
testcase_23 AC 129 ms
48,608 KB
testcase_24 AC 55 ms
24,192 KB
testcase_25 AC 53 ms
21,760 KB
testcase_26 AC 49 ms
12,064 KB
testcase_27 AC 77 ms
33,452 KB
testcase_28 AC 116 ms
45,064 KB
testcase_29 AC 52 ms
14,720 KB
testcase_30 AC 51 ms
19,456 KB
testcase_31 AC 94 ms
38,560 KB
testcase_32 AC 52 ms
17,920 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using ll = long long;
using pil = pair<int,ll>;

template<typename T>
bool chmax(T &a, const T &b){
    if (a >= b) return false;
    a = b;
    return true;
}

struct edge {
    int from, to;
    ll cost, prof;
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m; cin >> n >> m;
    ll lim; cin >> lim;
    vector<edge> es(m);
    for (auto &e : es){
        cin >> e.from >> e.to >> e.cost >> e.prof;
        e.from--, e.to--;
    }
    vector<int> ids(m); iota(ids.begin(),ids.end(),0);
    sort(ids.begin(),ids.end(),[&](int l, int r){
        return es[l].cost < es[r].cost;
    });
    atcoder::dsu d(n);
    ll csum = 0, pmax = 0;
    vector<int> yet;
    vector<vector<pil>> g(n);
    for (int i : ids){
        auto [u, v, c, p] = es[i];
        if (d.same(u,v)){
            yet.emplace_back(i);
        }
        else {
            d.merge(u,v);
            csum += c;
            chmax(pmax,p);
            g[u].emplace_back(v,c);
            g[v].emplace_back(u,c);
        }
    }
    if (csum > lim){
        cout << -1 << endl;
        return 0;
    }
    vector<vector<pil>> to(20,vector<pil>(n,pil(-1,-1)));
    vector<int> dep(n,0);
    auto dfs = [&](auto sfs, int v, int f) -> void {
        for (auto [u, c] : g[v]){
            if (u == f) continue;
            to[0][u] = pil(v,c);
            dep[u] = dep[v]+1;
            sfs(sfs,u,v);
        }
    };
    dfs(dfs,0,-1);
    for (int i = 1; i < 20; i++){
        for (int v = 0; v < n; v++){
            if (to[i-1][v].first == -1) continue;
            auto [u, c] = to[i-1][to[i-1][v].first];
            to[i][v] = pil(u,max(c,to[i-1][v].second));
        }
    }
    auto max_cost = [&](int u, int v){
        ll cpath = -1;
        if (dep[u] > dep[v]) swap(u,v);
        for (int d = 19; d >= 0; d--){
            if ((dep[v]-dep[u]) >> d & 1){
                chmax(cpath,to[d][v].second);
                v = to[d][v].first;
            }
        }
        if (u == v){
            return cpath;
        }
        for (int d = 19; d >= 0; d--){
            if (to[d][u].first == to[d][v].first) continue;
            chmax(cpath,to[d][u].second);
            u = to[d][u].first;
            chmax(cpath,to[d][v].second);
            v = to[d][v].first;
        }
        chmax(cpath,to[0][u].second);
        chmax(cpath,to[0][v].second);
        return cpath;
    };
    for (int i : yet){
        auto [u, v, c, p] = es[i];
        if (pmax >= p) continue;
        ll cur = csum - max_cost(u,v) + c;
        if (cur <= lim){
            pmax = p;
        }
    }
    cout << pmax << endl;
}
0