結果
| 問題 | 
                            No.2642 Don't cut line!
                             | 
                    
| コンテスト | |
| ユーザー | 
                             noya2
                         | 
                    
| 提出日時 | 2024-02-15 03:18:35 | 
| 言語 | C++23  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                RE
                                 
                             
                            
                            (最新)
                                AC
                                 
                             
                            (最初)
                            
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,728 bytes | 
| コンパイル時間 | 5,042 ms | 
| コンパイル使用メモリ | 323,680 KB | 
| 実行使用メモリ | 6,824 KB | 
| 最終ジャッジ日時 | 2024-09-29 00:39:36 | 
| 合計ジャッジ時間 | 10,247 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 11 RE * 22 | 
ソースコード
#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;
}
            
            
            
        
            
noya2