QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#608274 | #2457. Cheese, If You Please | rxzfn639 | WA | 1ms | 3884kb | C++23 | 2.9kb | 2024-10-03 20:19:24 | 2024-10-03 20:19:24 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 P = 998244353;
const int maxn = 500;
const double inf = 1e100;
const double eps = 1e-10;
double a[maxn][maxn];
int f[maxn], d[maxn];
struct Simplex {
int n, m;
Simplex () {}
void pivot(int r, int c) {
swap(d[c], f[r]);
a[r][c] = 1 / a[r][c];
for (int j = 0; j <= n; j++) if (j != c) a[r][j] *= a[r][c];
for (int i = 0; i <= m; i++) {
if (i != r) {
for (int j = 0; j <= n; j++) {
if (j != c) a[i][j] -= a[i][c] * a[r][j];
}
a[i][c] = -a[i][c] * a[r][c];
}
}
}
bool feasible() {
while (true) {
int r, c;
double p = inf;
for (int i = 0; i < m; i++) if (a[i][n] < p) p = a[r = i][n];
if (p > -eps) return true;
p = 0;
for (int i = 0; i < n; i++) if (a[r][i] < p) p = a[r][c = i];
if (p > -eps) return false;
p = a[r][n] / a[r][c];
for (int i = r + 1; i < m; i++) {
if (a[i][c] > eps) {
double v = a[i][n] / a[i][c];
if (v < p) r = i, p = v;
}
}
pivot(r, c);
}
}
// 解有界返回 1,无解返回 0,无界返回-1。f[i] 为 x[i] 的值,ret 为目标函数的值
int simplex(int n, int m, double x[maxn], double & ret) {
this-> n = n;
this-> m = m;
for (int i = 0; i < n; i++) d[i] = i;
for (int i = 0; i < m; i++) f[i] = n + i;
if (!feasible()) return 0;
while (true) {
int r, c;
double p = 0;
for (int i = 0; i < n; i++) if (a[m][i] > p) p = a[m][c = i];
if (p < eps) {
for (int i = 0; i < n; i++) if (d[i] < n) x[d[i]] = 0;
for (int i = 0; i < m; i++) if (f[i] < n) x[f[i]] = a[i][n];
ret = -a[m][n];
return 1;
}
p = inf;
for (int i = 0; i < m; i++) {
if (a[i][c] > eps) {
double v = a[i][n] / a[i][c];
if (v < p) r = i, p = v;
}
}
if (p == inf) return -1;
pivot(r, c);
}
}
};
void solve() {
int n, m;
cin >> n >> m;
Simplex simp;
for (int i = 0; i < n; i++) {
cin >> a[i][m];
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> a[j][i];
}
cin >> a[n][i];
}
double x[500], ret;
simp.simplex(m, n, x, ret);
cout << fixed << setprecision(2) << ret * 100.0 << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3872kb
input:
4 4 100 0 123 456 10.0 20.0 30.0 40.0 2.56 40.0 0.0 25.0 35.0 3.84 40.0 30.0 20.0 10.0 1.23 33.0 0.0 34.0 33.0 4.23
output:
1281.82
result:
ok single line: '1281.82'
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3884kb
input:
4 4 0 0 0 0 10.0 20.0 30.0 40.0 2.56 40.0 0.0 25.0 35.0 3.84 40.0 30.0 20.0 10.0 1.23 33.0 0.0 34.0 33.0 4.23
output:
-0.00
result:
wrong answer 1st lines differ - expected: '0.00', found: '-0.00'