結果
| 問題 |
No.1316 Maximum Minimum Spanning Tree
|
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2021-02-21 01:01:14 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 27 ms / 2,000 ms |
| コード長 | 3,285 bytes |
| コンパイル時間 | 1,388 ms |
| コンパイル使用メモリ | 115,932 KB |
| 最終ジャッジ日時 | 2025-01-19 02:53:49 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 78 |
ソースコード
#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>
#include <atcoder/maxflow>
// LinearProgrammingOnBasePolyhedron : Maximize/minimize linear function on base polyhedron, using Edmonds' algorithm
//
// maximize/minimize cx s.t. (x on some base polyhedron)
// Reference: <https://www.amazon.co.jp/dp/B01N6G0579>, Sec. 2.4, Algorithm 2.2-2.3
// "Submodular Functions, Matroids, and Certain Polyhedra" [Edmonds+, 1970]
template <typename Tvalue> struct LinearProgrammingOnBasePolyhedron {
using Tfunc = std::function<Tvalue(int, const std::vector<Tvalue> &)>;
static Tvalue EPS;
int N;
std::vector<Tvalue> c;
Tfunc maximize_xi;
Tvalue xsum;
bool minimize;
Tvalue fun;
std::vector<Tvalue> x;
bool infeasible;
void _init(const std::vector<Tvalue> &c_, Tfunc q_, Tvalue xsum_, Tvalue xlowerlimit, bool minimize_) {
N = c_.size();
c = c_;
maximize_xi = q_;
xsum = xsum_;
minimize = minimize_;
fun = 0;
x.assign(N, xlowerlimit);
infeasible = false;
}
void _solve() {
std::vector<std::pair<Tvalue, int>> c2i(N);
for (int i = 0; i < N; i++) c2i[i] = std::make_pair(c[i], i);
std::sort(c2i.begin(), c2i.end());
if (!minimize) std::reverse(c2i.begin(), c2i.end());
for (const auto &p : c2i) {
const int i = p.second;
x[i] = maximize_xi(i, x);
}
if (std::abs(std::accumulate(x.begin(), x.end(), Tvalue(0)) - xsum) > EPS) {
infeasible = true;
} else {
for (int i = 0; i < N; i++) fun += x[i] * c[i];
}
}
LinearProgrammingOnBasePolyhedron(const std::vector<Tvalue> &c_, Tfunc q_, Tvalue xsum_, Tvalue xlowerlimit, bool minimize_) {
_init(c_, q_, xsum_, xlowerlimit, minimize_);
_solve();
}
};
template <> long long LinearProgrammingOnBasePolyhedron<long long>::EPS = 0;
template <> long double LinearProgrammingOnBasePolyhedron<long double>::EPS = 1e-12;
using std::cin, std::cout, std::vector;
int main() {
using Num = long long;
int N, M;
long long K;
cin >> N >> M >> K;
vector<int> A(M), B(M);
vector<Num> C(M), D(M);
for (int i = 0; i < M; i++) {
cin >> A[i] >> B[i] >> C[i] >> D[i];
A[i]--, B[i]--;
}
auto maximize_xi = [&](int ie, const vector<Num> &xnow) -> Num {
atcoder::mf_graph<Num> mf(N + 2);
mf.add_edge(N, A[ie], 2 * K * N);
mf.add_edge(N, B[ie], 2 * K * N);
for (int je = 0; je < M; je++) {
mf.add_edge(A[je], B[je], xnow[je]);
mf.add_edge(B[je], A[je], xnow[je]);
mf.add_edge(N, A[je], xnow[je]);
mf.add_edge(N, B[je], xnow[je]);
}
for (int iv = 0; iv < N; iv++) mf.add_edge(iv, N + 1, 2 * K);
Num ret = mf.flow(N, N + 1) / 2 - K - std::accumulate(xnow.begin(), xnow.end(), (Num)0);
return std::min(ret, D[ie]);
};
LinearProgrammingOnBasePolyhedron<Num> solver(C, maximize_xi, K * (N - 1), 0, true);
if (solver.infeasible) {
cout << "-1\n";
} else {
cout << (long long)solver.fun << '\n';
}
}
hitonanode