QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#608264#2457. Cheese, If You Pleaserxzfn639WA 2ms5868kbC++232.9kb2024-10-03 20:16:262024-10-03 20:16:26

Judging History

你现在查看的是最新测评结果

  • [2024-10-03 20:16:26]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:5868kb
  • [2024-10-03 20:16:26]
  • 提交

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;
struct Simplex {
    int n, m;
    double a[maxn][maxn];
    int f[maxn], d[maxn];
    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 >> simp.a[i][m];
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cin >> simp.a[j][i];
        }
        cin >> simp.a[n][i];
    }
    double x[500], ret;
    simp.simplex(m, n, x, ret);
    cout << fixed << setprecision(12) << 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: 0
Wrong Answer
time: 2ms
memory: 5868kb

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.818181818182

result:

wrong answer 1st lines differ - expected: '1281.82', found: '1281.818181818182'