結果

問題 No.2642 Don't cut line!
ユーザー noya2noya2
提出日時 2024-02-15 03:18:35
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,728 bytes
コンパイル時間 5,828 ms
コンパイル使用メモリ 321,948 KB
実行使用メモリ 6,784 KB
最終ジャッジ日時 2024-02-18 15:43:59
合計ジャッジ時間 10,316 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 44 ms
6,676 KB
testcase_06 AC 45 ms
6,780 KB
testcase_07 AC 44 ms
6,676 KB
testcase_08 AC 43 ms
6,676 KB
testcase_09 AC 46 ms
6,780 KB
testcase_10 AC 45 ms
6,676 KB
testcase_11 AC 47 ms
6,784 KB
testcase_12 AC 46 ms
6,676 KB
testcase_13 AC 46 ms
6,676 KB
testcase_14 AC 48 ms
6,784 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 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>;
const int mx = 12;

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;
    assert(n <= 1000);
    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(mx,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 < mx; 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 = mx-1; 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 = mx-1; 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