From 4c4de31c2da02743084d6de996dc6a91877ad060 Mon Sep 17 00:00:00 2001 From: hitonanode <32937551+hitonanode@users.noreply.github.com> Date: Sun, 6 Sep 2026 19:06:17 +0900 Subject: [PATCH] fix simplex --- combinatorial_opt/simplex.hpp | 6 +-- .../test/simplex.yuki3674.test.cpp | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 combinatorial_opt/test/simplex.yuki3674.test.cpp diff --git a/combinatorial_opt/simplex.hpp b/combinatorial_opt/simplex.hpp index fecf0c0c..c7ea5888 100644 --- a/combinatorial_opt/simplex.hpp +++ b/combinatorial_opt/simplex.hpp @@ -47,11 +47,11 @@ template struct for (int j = 0; j < N + 2; j++) { if (j != j_ch) { mat[i_ch][j] *= -mat[i_ch][j_ch]; - if (abs_(mat[i_ch][j]) > EPS) jupd.push_back(j); + if (mat[i_ch][j] != 0) jupd.push_back(j); } } for (int i = 0; i < M + 2; i++) { - if (abs_(mat[i][j_ch]) < EPS or i == i_ch) continue; + if (mat[i][j_ch] == 0 or i == i_ch) continue; for (auto j : jupd) mat[i][j] += mat[i][j_ch] * mat[i_ch][j]; mat[i][j_ch] *= mat[i_ch][j_ch]; } @@ -76,7 +76,7 @@ template struct i_ch = i; } else if (mat[i_ch][N + 1] / mat[i_ch][j_ch] - mat[i][N + 1] / mat[i][j_ch] < EPS and - idx[i_ch] > idx[i]) { + idx[N + 1 + i_ch] > idx[N + 1 + i]) { i_ch = i; } } diff --git a/combinatorial_opt/test/simplex.yuki3674.test.cpp b/combinatorial_opt/test/simplex.yuki3674.test.cpp new file mode 100644 index 00000000..e9b8198b --- /dev/null +++ b/combinatorial_opt/test/simplex.yuki3674.test.cpp @@ -0,0 +1,51 @@ +#define PROBLEM "https://yukicoder.me/problems/no/3674" +#define ERROR 1e-6 +#include "../simplex.hpp" +#include +#include +#include +using namespace std; + +int main() { + cin.tie(nullptr); + ios::sync_with_stdio(false); + + int T; + cin >> T; + while (T--) { + int N, M; + cin >> N >> M; + vector A(N, vector(M)); + for (auto &a : A) { + for (auto &x : a) cin >> x; + }; + + // Minimize v = v_pos - v_neg over Bob's distribution q: + // A q <= v, sum(q) = 1, q >= 0. + for (auto &a : A) { + a.push_back(-1); + a.push_back(1); + } + + A.push_back({}); + for (int t = 0; t < M; ++t) A.back().push_back(1); + A.back().push_back(0); + A.back().push_back(0); + + A.push_back({}); + for (int t = 0; t < M; ++t) A.back().push_back(-1); + A.back().push_back(0); + A.back().push_back(0); + + vector B(A.size() - 2, 0); + B.push_back(1); + B.push_back(-1); + + vector C(A.front().size()); + C.at(C.size() - 2) = -1; + C.back() = 1; + + Simplex simplex{A, B, C}; + cout << fixed << setprecision(15) << -simplex.ans << '\n'; + } +}