1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| #include <bits/stdc++.h> #define watch(x) std::cout << (#x) << " is " << (x) << std::endl using LL = long long; using pii = std::pair<int, int>; using pll = std::pair<LL, LL>;
using VD = std::vector<double>; const double eps = 1e-10; const double inf = 1e10;
VD simplexCore(VD c, std::vector<VD> A, VD b) { int n = A.size(), m = c.size(); std::vector<int> p(m); std::iota(p.begin(), p.end(), 0); for (int i = 0; i < n; ++i) A[i].emplace_back(b[i]); c.emplace_back(0); A.emplace_back(c); for (int j = n; j <= m; ++j) { for (int i = 0; i < n; ++i) { A[n][j] -= A[n][i] * A[i][j]; } } auto check = [&]() -> bool { for (int j = n; j < m; ++j) if (A[n][j] > eps) { bool flag = false; for (int i = 0; i < n; ++i) if (A[i][j] > eps) { flag = true; break; } if (!flag) return false; } return true; }; while (1) { int ch = std::max_element(A[n].begin() + n, A[n].begin() + m) - A[n].begin(), hc; if (A[n][ch] < eps) break; assert(check()); double theta = DBL_MAX; for (int i = 0; i < n; ++i) if (A[i][ch] > eps && A[i].back() / A[i][ch] < theta) { theta = A[i].back() / A[i][ch]; hc = i; } std::swap(p[ch], p[hc]); double tmp = 1 / A[hc][ch]; for (int j = n; j <= m; ++j) A[hc][j] *= tmp; for (int i = 0; i <= n; ++i) if (i != hc) { for (int j = n; j <= m; ++j) if (j != ch) { A[i][j] -= A[i][ch] * A[hc][j]; } } for (int i = 0; i <= n; ++i) A[i][ch] *= -tmp; A[hc][ch] = tmp; } VD x(m); for (int i = 0; i < n; ++i) x[p[i]] = A[i].back(); watch(-A.back().back()); return x; }
VD simplex(VD c, std::vector<VD> Aq, VD bq, std::vector<VD> Alq, VD blq) { assert(Aq.size() == bq.size()); assert(Alq.size() == blq.size()); int n = Aq.size() + Alq.size(); int m = c.size(); for (int i = 0; i < bq.size(); ++i) if (bq[i] < -eps) { for (auto &x : Aq[i]) x = -x; bq[i] = -bq[i]; } for (int i = 0; i < blq.size(); ++i) if (blq[i] < -eps) { for (auto &x : Alq[i]) x = -x; ++m; } std::vector<VD> A(n, VD(n + m)); VD f(n + m), b(n); int now = n + c.size(); for (int i = 0; i < n; ++i) A[i][i] = 1; for (int i = 0; i < Aq.size(); ++i) { for (int j = 0; j < Aq[i].size(); ++j) A[i][n + j] = Aq[i][j]; b[i] = bq[i]; f[i] = -inf; } for (int i = 0; i < Alq.size(); ++i) { for (int j = 0; j < Alq[i].size(); ++j) A[i + Aq.size()][n + j] = Alq[i][j]; if (blq[i] < -eps) { A[i + Aq.size()][now++] = -1; f[i + Aq.size()] = -inf; } b[i + Aq.size()] = fabs(blq[i]); } for (int i = 0; i < c.size(); ++i) f[n + i] = c[i]; auto x = simplexCore(f, A, b); return VD(x.begin() + n, x.begin() + n + c.size()); } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int cas = 1; std::cin >> cas; while (cas--) { int nlq, nq, m; std::cin >> nlq >> nq >> m; std::vector<std::vector<double>> Alq(nlq, std::vector<double>(m, 0)); std::vector<std::vector<double>> Aq(nq, std::vector<double>(m, 0)); std::vector<double> blq(nlq), bq(nq), c(m); for (auto &x : c) std::cin >> x; for (auto &x : Alq) for (auto &i : x) std::cin >> i; for (auto &x : blq) std::cin >> x; for (auto &x : Aq) for (auto &i : x) std::cin >> i; for (auto &x : bq) std::cin >> x; auto x = simplex(c, Aq, bq, Alq, blq); for (auto t : x) std::cout << t << "\n"; } return 0; }
|