結果
| 問題 | No.1316 Maximum Minimum Spanning Tree |
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2020-12-12 02:18:17 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.89.0) |
| 結果 |
CE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 6,572 bytes |
| 記録 | |
| コンパイル時間 | 266 ms |
| コンパイル使用メモリ | 24,960 KB |
| 最終ジャッジ日時 | 2025-01-16 22:37:19 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
main.cpp:2:10: fatal error: testlib.h: No such file or directory
2 | #include "testlib.h"
| ^~~~~~~~~~~
compilation terminated.
ソースコード
// Validator
#include "testlib.h"
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>
// MaxFlow (Dinic algorithm)
template <typename T> struct MaxFlow {
struct edge {
int to;
T cap;
int rev;
};
std::vector<std::vector<edge>> edges;
std::vector<int> level; // level[i] = distance between vertex S and i (Default: -1)
std::vector<int> iter; // iteration counter, used for Dinic's DFS
void bfs(int s) {
level.assign(edges.size(), -1);
std::queue<int> q;
level[s] = 0;
q.push(s);
while (!q.empty()) {
int v = q.front();
q.pop();
for (edge &e : edges[v]) {
if (e.cap > 0 and level[e.to] < 0) {
level[e.to] = level[v] + 1;
q.push(e.to);
}
}
}
}
T dfs_dinic(int v, int goal, T f) {
if (v == goal) return f;
for (int &i = iter[v]; i < (int)edges[v].size(); i++) {
edge &e = edges[v][i];
if (e.cap > 0 and level[v] < level[e.to]) {
T d = dfs_dinic(e.to, goal, std::min(f, e.cap));
if (d > 0) {
e.cap -= d;
edges[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
MaxFlow(int N) { edges.resize(N); }
void add_edge(int from, int to, T capacity) {
edges[from].push_back(edge{to, capacity, (int)edges[to].size()});
edges[to].push_back(edge{from, (T)0, (int)edges[from].size() - 1});
}
// Dinic algorithm
// Complexity: O(V^2 E)
T Dinic(int s, int t, T req) {
T flow = 0;
while (req > 0) {
bfs(s);
if (level[t] < 0) break;
iter.assign(edges.size(), 0);
T f;
while ((f = dfs_dinic(s, t, req)) > 0) flow += f, req -= f;
}
return flow;
}
};
// 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);
}
Tvalue error = std::accumulate(x.begin(), x.end(), Tvalue(0)) - xsum;
if (error > EPS or -error > 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-10;
// UnionFind Tree (0-indexed), based on size of each disjoint set
struct UnionFind {
std::vector<int> par, cou;
UnionFind(int N = 0) : par(N), cou(N, 1) { iota(par.begin(), par.end(), 0); }
int find(int x) { return (par[x] == x) ? x : (par[x] = find(par[x])); }
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
if (cou[x] < cou[y]) std::swap(x, y);
par[y] = x, cou[x] += cou[y];
return true;
}
int count(int x) { return cou[find(x)]; }
};
using std::cin, std::cout, std::vector;
int main(int argc, char **argv) {
registerValidation(argc, argv);
int N = inf.readInt(2, 50); // N <= 50
inf.readSpace();
int M = inf.readInt(N - 1, 200); // M <= 200
inf.readSpace();
long long K = inf.readInt(1, 100000000);
inf.readEoln();
vector<int> A(M), B(M);
vector<long long> C(M), D(M);
std::set<std::pair<int, int>> edges;
UnionFind uf(N);
for (int i = 0; i < M; i++) {
A[i] = inf.readInt(1, N);
inf.readSpace();
B[i] = inf.readInt(1, N);
inf.readSpace();
C[i] = inf.readInt(1, 100000000);
inf.readSpace();
D[i] = inf.readInt(1, 100000000);
inf.readEoln();
A[i]--, B[i]--;
assert(A[i] != B[i]); // Graph has NO self-loop.
if (A[i] > B[i]) std::swap(A[i], B[i]);
assert(edges.count(std::make_pair(A[i], B[i])) == 0); // Graph has NO multi-edge.
edges.emplace(A[i], B[i]);
uf.unite(A[i], B[i]);
}
inf.readEof();
assert(uf.count(0) == N); // Graph must be connected.
auto maximize_xi = [&](int ie, const vector<long long> &xnow) -> long long {
MaxFlow<long long> mf(N + 2);
mf.add_edge(N, A[ie], 1LL << 60);
mf.add_edge(N, B[ie], 1LL << 60);
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);
long long ret = mf.Dinic(N, N + 1, 1LL << 62) / 2 - K - std::accumulate(xnow.begin(), xnow.end(), 0LL);
return std::min(ret, D[ie]);
};
LinearProgrammingOnBasePolyhedron<long long> solver(C, maximize_xi, K * (N - 1), 0, true);
if (solver.infeasible) {
cout << "-1\n";
} else {
cout << solver.fun << '\n';
}
}
hitonanode