Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions combinatorial_opt/simplex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ template <typename Float = double, int DEPS = 30, bool Randomize = true> 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];
}
Expand All @@ -76,7 +76,7 @@ template <typename Float = double, int DEPS = 30, bool Randomize = true> 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;
}
}
Expand Down
51 changes: 51 additions & 0 deletions combinatorial_opt/test/simplex.yuki3674.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#define PROBLEM "https://yukicoder.me/problems/no/3674"
#define ERROR 1e-6
#include "../simplex.hpp"
#include <iomanip>
#include <iostream>
#include <vector>
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<double>(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<double> B(A.size() - 2, 0);
B.push_back(1);
B.push_back(-1);

vector<double> C(A.front().size());
C.at(C.size() - 2) = -1;
C.back() = 1;

Simplex<double> simplex{A, B, C};
cout << fixed << setprecision(15) << -simplex.ans << '\n';
}
}
Loading