結果
問題 | No.2642 Don't cut line! |
ユーザー |
![]() |
提出日時 | 2024-02-15 03:12:19 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 149 ms / 4,000 ms |
コード長 | 2,682 bytes |
コンパイル時間 | 5,775 ms |
コンパイル使用メモリ | 322,904 KB |
実行使用メモリ | 50,288 KB |
最終ジャッジ日時 | 2024-09-29 03:30:32 |
合計ジャッジ時間 | 9,887 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
#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;}