QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#255932 | #7753. Energy Distribution | ucup-team133 | AC ✓ | 4ms | 4076kb | C++17 | 10.1kb | 2023-11-18 17:32:53 | 2024-10-31 10:23:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
using ll = long long;
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
for (int i = 0; i < (int) v.size(); i++) {
os << v[i] << (i + 1 == (int) v.size() ? "" : ", ");
}
os << "]";
return os;
}
//#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;
#define debug(...);
struct Simplex {
bool infinity, // which the problem is unbounded or not
infeasible; // which the problem is infeasible or not
int n, // the number of variables
m; // the number of constraints
vector<double> x; // optimal solution
double opt; // optimal value
vector<int> index; // indices of non-basis (< n) and basis (otherwise)
int r, s; // pivot row / column
vector<vector<double>> tableau;
/**
* @brief Construct a new Simplex object
*
* @param A Coefficients of constraints
* @param b Bounds of constraints
* @param c Coefficients of objective function
* @param mode choose pivot by smallest subscript rule if mode = 0, largest coefficient rule otherwise
* @details Maximize cx s.t. Ax <= b and x >= 0
*/
Simplex(const vector<vector<double>> &A, const vector<double> &b, const vector<double> &c) {
infinity = infeasible = false;
init(A, b, c);
solve();
}
private:
static constexpr double EPS = 1e-10;
inline int sgn(const double &x) { return x < -EPS ? -1 : x > EPS ? 1 : 0; }
inline int compare(const double &a, const double &b) { return sgn(a - b); }
inline bool equals(const double &a, const double &b) { return compare(a, b) == 0; }
void init(const vector<vector<double>> &A, const vector<double> &b, const vector<double> &c) {
m = A.size(), n = c.size();
index.resize(n + 1 + m);
iota(index.begin(), index.end(), 0);
tableau.assign(m + 2, vector<double>(n + 2, 0));
r = m;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) tableau[i][j] = -A[i][j];
tableau[i][n] = 1;
tableau[i][n + 1] = b[i];
if (tableau[i][n + 1] < tableau[r][n + 1]) r = i;
}
for (int j = 0; j < n; j++) tableau[m][j] = c[j];
tableau[m + 1][n] = -1;
}
void smallest_subscript_rule() {
r = s = -1;
for (int j = 0; j < n + 1; j++) {
if (s < 0 or index[j] < index[s]) {
if (compare(tableau[m + 1][j], 0) > 0 or
(equals(tableau[m + 1][j], 0) and compare(tableau[m][j], 0) > 0)) {
s = j;
}
}
}
if (s < 0) return;
r = -1;
for (int i = 0; i < m; i++) {
if (compare(tableau[i][s], 0) >= 0) continue;
if (r < 0)
r = i;
else if (compare(tableau[i][n + 1] / (-tableau[i][s]), tableau[r][n + 1] / (-tableau[r][s])) < 0)
r = i;
else if (equals(tableau[i][n + 1] / (-tableau[i][s]), tableau[r][n + 1] / (-tableau[r][s])) and
index[n + 1 + i] < index[n + 1 + r]) {
r = i;
}
}
}
void solve() {
vector<int> nonzero;
for (s = n;;) {
if (r < m) {
swap(index[s], index[n + 1 + r]);
tableau[r][s] = double(1) / tableau[r][s];
nonzero.clear();
for (int j = 0; j < n + 2; j++) {
if (j == s) continue;
tableau[r][j] *= -tableau[r][s];
if (!equals(tableau[r][j], 0)) nonzero.emplace_back(j);
}
for (int i = 0; i < m + 2; i++) {
if (i == r or equals(tableau[i][s], 0)) continue;
for (const auto &j: nonzero) tableau[i][j] += tableau[i][s] * tableau[r][j];
tableau[i][s] *= tableau[r][s];
}
}
smallest_subscript_rule();
if (s < 0) break;
if (r < 0) {
infinity = true;
return;
}
}
if (compare(tableau[m + 1][n + 1], 0) < 0) {
infeasible = true;
return;
}
x.assign(n, 0);
for (int i = 0; i < m; i++) {
if (index[n + 1 + i] < n) {
x[index[n + 1 + i]] = tableau[i][n + 1];
}
}
opt = tableau[m][n + 1];
}
};
template<class T>
pair<T, int> gaussian(vector<vector<T>> &a, int last = 0) {
int h = a.size();
int w = a[0].size();
T d = 1;
int r = 0;
for (int j = 0; j < w - last; j++) {
int id = -1;
for (int i = r; i < h; i++) {
if (a[i][j] != T(0)) {
id = i;
break;
}
}
if (id == -1) {
d = 0;
continue;
}
if (r != id) {
d *= -1;
swap(a[r], a[id]);
}
d *= a[r][j];
for (int k = w - 1; k >= j; k--) {
a[r][k] /= a[r][j];
}
for (int i = 0; i < h; i++) {
if (i == r) continue;
for (int k = w - 1; k >= j; k--) {
a[i][k] -= a[r][k] * a[i][j];
}
}
r++;
}
return {d, r};
}
template<typename T>
vector<vector<T>> linear(vector<vector<T>> &a, const vector<T> &b) {
int h = a.size();
int w = a[0].size();
for (int i = 0; i < h; i++) a[i].emplace_back(b[i]);
auto [det, rank] = gaussian(a, 1);
for (int i = rank; i < h; i++) {
if (a[i][w] != T(0)) {
return vector<vector<T>>();
}
}
vector<vector<T>> res(1, vector<T>(w));
vector<int> p(w, -1);
for (int i = 0, j = 0; i < rank and j < w; i++) {
while (a[i][j] == T(0)) {
j++;
}
res[0][j] = a[i][w];
p[j] = i;
}
for (int j = 0; j < w; j++) {
if (p[j] != -1) continue;
vector<T> c(w);
c[j] = 1;
for (int k = 0; k < j; k++) {
if (p[k] != -1) {
c[k] = -a[p[k]][j];
}
}
res.emplace_back(c);
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector W(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> W[i][j];
}
}
auto f = [&](const vector<int> &inv, const vector<double> &x) {
double res = 0;
for (int i = 0; i < n; i++) {
if (inv[i] == -1) continue;
for (int j = i + 1; j < n; j++) {
if (inv[j] == -1) continue;
res += x[inv[i]] * W[i][j] * x[inv[j]];
}
}
return res;
};
double ans = 0;
auto calc = [&](int mask) -> double {
vector<vector<double>> C;
vector<int> inv(n, -1);
int t = 0;
for (int i = 0; i < n; i++) {
if ((mask >> i) & 1) {
inv[i] = t++;
}
}
debug(inv);
debug(t);
vector<vector<double>> A;
{
vector<double> b;
for (int i = 0; i < n; i++) {
if ((~mask >> i) & 1) continue;
for (int j = 0; j < n; j++) {
if ((~mask >> j) & 1) continue;
if (W[i][j] == 0) continue;
vector<double> a(t);
a[inv[i]] = 1;
a[inv[j]] = 0;
for (int k = 0; k < n; k++) {
if (k == i or k == j) continue;
if ((~mask >> k) & 1) continue;
a[inv[k]] = 0.5 - (W[i][k] - W[j][k]) * 1.0 / (2 * W[i][j]);
}
A.emplace_back(a);
b.emplace_back(0.5);
}
}
{
vector<double> a(t, 1);
A.emplace_back(a);
b.emplace_back(1);
}
C = linear(A, b);
debug(A);
debug(b);
}
if (C.empty()){
vector<double> fuck(t,0);
for (int i=0;i<t;i++){
fuck[i] = A[i].back();
if (fuck[i] < 0) return 0;
}
return f(inv,fuck);
};
for (int i = 0; i < int(C.size()); i++) {
debug(C[i]);
}
{
double res = f(inv,C[0]);
int len = C.size();
if (len > 1) {
vector<vector<double>> A;
vector<double> b, c;
for (int i = 0; i < t; i++) {
vector<double> a;
for (int j = 1; j < len; j++) a.emplace_back(-C[j][i]);
A.emplace_back(a);
b.emplace_back(C[0][i]);
}
for (int i = 1; i < len; i++) c.emplace_back(f(inv, C[i]));
Simplex simplex(A, b, c);
if (simplex.infeasible) return 0;
vector<double> d = C[0];
for (int i = 1; i < len; i++) {
for (int j = 0; j < t; j++) {
d[j] += simplex.x[i - 1] * C[i][j];
}
}
debug(d);
res = f(inv, d);
} else {
for (int i = 0; i < t; i++) {
if (C[0][i] < 0) {
return 0;
}
}
res = f(inv, C[0]);
}
return res;
}
};
for (int i = (1 << n) - 1; i > 0; i--) {
debug(i);
double res = calc(i);
debug(res);
ans = max(ans, res);
}
cout << fixed << setprecision(10);
cout << ans << "\n";
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3832kb
input:
2 0 1 1 0
output:
0.2500000000
result:
ok found '0.2500000', expected '0.2500000', error '0.0000000'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3952kb
input:
3 0 2 1 2 0 2 1 2 0
output:
0.5714285714
result:
ok found '0.5714286', expected '0.5714290', error '0.0000004'
Test #3:
score: 0
Accepted
time: 0ms
memory: 4056kb
input:
3 0 1 2 1 0 1 2 1 0
output:
0.5000000000
result:
ok found '0.5000000', expected '0.5000000', error '0.0000000'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
4 0 3 1 0 3 0 1 0 1 1 0 2 0 0 2 0
output:
0.7500000000
result:
ok found '0.7500000', expected '0.7500000', error '0.0000000'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3772kb
input:
5 0 0 0 4 4 0 0 2 0 4 0 2 0 2 0 4 0 2 0 0 4 4 0 0 0
output:
1.0000000000
result:
ok found '1.0000000', expected '1.0000000', error '0.0000000'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3776kb
input:
6 0 9 5 5 10 6 9 0 0 0 0 1 5 0 0 0 3 0 5 0 0 0 10 5 10 0 3 10 0 0 6 1 0 5 0 0
output:
2.8571428571
result:
ok found '2.8571429', expected '2.8571430', error '0.0000001'
Test #7:
score: 0
Accepted
time: 1ms
memory: 3832kb
input:
7 0 0 0 0 50 10 45 0 0 0 0 0 3 1 0 0 0 0 0 4 16 0 0 0 0 44 0 0 50 0 0 44 0 37 6 10 3 4 0 37 0 2 45 1 16 0 6 2 0
output:
12.5115848007
result:
ok found '12.5115848', expected '12.5115850', error '0.0000000'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3780kb
input:
8 0 0 56 0 0 58 16 0 0 0 37 20 0 82 0 0 56 37 0 0 98 0 83 0 0 20 0 0 0 0 100 0 0 0 98 0 0 62 29 0 58 82 0 0 62 0 43 0 16 0 83 100 29 43 0 4 0 0 0 0 0 0 4 0
output:
25.0091178965
result:
ok found '25.0091179', expected '25.0091180', error '0.0000000'
Test #9:
score: 0
Accepted
time: 1ms
memory: 3956kb
input:
9 0 0 0 135 0 0 0 448 476 0 0 0 0 0 0 208 17 0 0 0 0 467 1 0 0 0 134 135 0 467 0 0 0 92 369 207 0 0 1 0 0 176 0 235 0 0 0 0 0 176 0 65 363 413 0 208 0 92 0 65 0 0 0 448 17 0 369 235 363 0 0 0 476 0 134 207 0 413 0 0 0
output:
119.0000000000
result:
ok found '119.0000000', expected '119.0000000', error '0.0000000'
Test #10:
score: 0
Accepted
time: 3ms
memory: 3840kb
input:
10 0 0 0 289 0 397 0 0 140 155 0 0 28 101 35 614 0 0 545 300 0 28 0 0 329 702 0 242 0 298 289 101 0 0 0 0 0 0 720 0 0 35 329 0 0 0 0 38 0 0 397 614 702 0 0 0 229 0 0 0 0 0 0 0 0 229 0 317 0 0 0 0 242 0 38 0 317 0 961 309 140 545 0 720 0 0 0 961 0 92 155 300 298 0 0 0 0 309 92 0
output:
240.2500000000
result:
ok found '240.2500000', expected '240.2500000', error '0.0000000'
Test #11:
score: 0
Accepted
time: 4ms
memory: 3852kb
input:
10 0 295 2 809 333 880 284 305 41 295 295 0 512 1000 281 153 42 550 962 930 2 512 0 727 709 969 665 973 301 410 809 1000 727 0 282 551 960 804 274 956 333 281 709 282 0 613 505 406 896 441 880 153 969 551 613 0 769 770 40 288 284 42 665 960 505 769 0 919 989 490 305 550 973 804 406 770 919 0 400 209...
output:
327.3736589667
result:
ok found '327.3736590', expected '327.3736590', error '0.0000000'
Test #12:
score: 0
Accepted
time: 4ms
memory: 3852kb
input:
10 0 403 2 164 0 399 279 156 109 472 403 0 292 279 100 326 153 124 103 426 2 292 0 0 58 87 0 177 324 334 164 279 0 0 256 188 0 257 467 23 0 100 58 256 0 453 75 21 0 309 399 326 87 188 453 0 0 319 395 434 279 153 0 0 75 0 0 342 431 72 156 124 177 257 21 319 342 0 265 0 109 103 324 467 0 395 431 265 0...
output:
155.3703581450
result:
ok found '155.3703581', expected '155.3703580', error '0.0000000'
Test #13:
score: 0
Accepted
time: 4ms
memory: 3864kb
input:
10 0 3 10 8 0 3 3 8 9 6 3 0 6 10 0 10 9 9 4 9 10 6 0 7 1 5 6 8 0 0 8 10 7 0 10 6 0 6 9 4 0 0 1 10 0 1 0 2 0 1 3 10 5 6 1 0 6 4 0 4 3 9 6 0 0 6 0 10 0 8 8 9 8 6 2 4 10 0 0 2 9 4 0 9 0 0 0 0 0 10 6 9 0 4 1 4 8 2 10 0
output:
3.1332737030
result:
ok found '3.1332737', expected '3.1332740', error '0.0000001'
Test #14:
score: 0
Accepted
time: 4ms
memory: 3848kb
input:
10 0 52 25 22 39 47 85 63 8 2 52 0 0 87 2 20 50 87 88 6 25 0 0 77 27 58 81 0 98 0 22 87 77 0 28 0 49 53 59 0 39 2 27 28 0 0 65 76 0 0 47 20 58 0 0 0 88 0 0 3 85 50 81 49 65 88 0 60 85 1 63 87 0 53 76 0 60 0 76 88 8 88 98 59 0 0 85 76 0 0 2 6 0 0 0 3 1 88 0 0
output:
29.5429468484
result:
ok found '29.5429468', expected '29.5429470', error '0.0000000'
Test #15:
score: 0
Accepted
time: 3ms
memory: 4076kb
input:
10 0 79 0 192 19 79 0 0 0 0 79 0 0 0 164 100 74 0 26 176 0 0 0 0 184 153 152 0 0 39 192 0 0 0 0 90 54 0 18 0 19 164 184 0 0 195 107 0 8 53 79 100 153 90 195 0 0 124 96 149 0 74 152 54 107 0 0 181 28 0 0 0 0 0 0 124 181 0 0 3 0 26 0 18 8 96 28 0 0 0 0 176 39 0 53 149 0 3 0 0
output:
59.3834104972
result:
ok found '59.3834105', expected '59.3834100', error '0.0000000'
Test #16:
score: 0
Accepted
time: 3ms
memory: 3836kb
input:
10 0 416 0 476 0 0 2 0 204 750 416 0 0 850 0 0 471 0 835 67 0 0 0 0 184 274 0 135 557 709 476 850 0 0 904 0 0 556 0 0 0 0 184 904 0 141 0 0 0 0 0 0 274 0 141 0 0 167 486 0 2 471 0 0 0 0 0 0 617 0 0 0 135 556 0 167 0 0 241 226 204 835 557 0 0 486 617 241 0 0 750 67 709 0 0 0 0 226 0 0
output:
226.0000000000
result:
ok found '226.0000000', expected '226.0000000', error '0.0000000'
Test #17:
score: 0
Accepted
time: 3ms
memory: 4076kb
input:
10 0 777 0 543 382 5 266 0 255 250 777 0 0 0 0 0 566 637 0 469 0 0 0 0 183 671 0 711 335 294 543 0 0 0 929 475 0 925 0 46 382 0 183 929 0 777 674 399 0 732 5 0 671 475 777 0 0 814 453 0 266 566 0 0 674 0 0 0 0 703 0 637 711 925 399 814 0 0 654 928 255 0 335 0 0 453 0 654 0 0 250 469 294 46 732 0 703...
output:
272.2710567423
result:
ok found '272.2710567', expected '272.2710570', error '0.0000000'
Test #18:
score: 0
Accepted
time: 3ms
memory: 3848kb
input:
10 0 1 0 0 0 0 4 0 0 0 1 0 0 2 3 0 5 0 0 0 0 0 0 1 0 0 0 0 0 0 0 2 1 0 0 2 3 0 0 3 0 3 0 0 0 0 0 0 0 1 0 0 0 2 0 0 4 0 0 0 4 5 0 3 0 4 0 0 3 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 3 0 0 1 0 0 0 3 1 0 0 5 1 0
output:
1.2500000000
result:
ok found '1.2500000', expected '1.2500000', error '0.0000000'
Test #19:
score: 0
Accepted
time: 4ms
memory: 3780kb
input:
10 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0
output:
0.2500000000
result:
ok found '0.2500000', expected '0.2500000', error '0.0000000'
Test #20:
score: 0
Accepted
time: 3ms
memory: 3964kb
input:
10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 622 0 0 0 0 0 0 0 0 622 0 0 0 0 0 0 585 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 982 0 0 0 0 0 0 0 0 0 0 0 0 0 585 0 0 0 982 0 0
output:
245.5000000000
result:
ok found '245.5000000', expected '245.5000000', error '0.0000000'
Test #21:
score: 0
Accepted
time: 2ms
memory: 3776kb
input:
10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
output:
0.0000000000
result:
ok found '0.0000000', expected '0.0000000', error '-0.0000000'
Extra Test:
score: 0
Extra Test Passed