結果

問題 No.453 製薬会社
ユーザー neterukunneterukun
提出日時 2022-01-05 15:36:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,574 bytes
コンパイル時間 310 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 52,608 KB
最終ジャッジ日時 2024-04-23 19:27:07
合計ジャッジ時間 1,378 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,096 KB
testcase_01 AC 38 ms
52,096 KB
testcase_02 AC 36 ms
52,352 KB
testcase_03 AC 36 ms
52,224 KB
testcase_04 AC 38 ms
52,352 KB
testcase_05 AC 38 ms
52,608 KB
testcase_06 AC 37 ms
52,224 KB
testcase_07 AC 39 ms
52,352 KB
testcase_08 AC 40 ms
52,352 KB
testcase_09 AC 40 ms
52,096 KB
testcase_10 AC 38 ms
52,096 KB
testcase_11 AC 37 ms
52,224 KB
testcase_12 AC 38 ms
52,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def make_tableau(A, b, c):
    m = len(A)
    n = len(A[0])

    tableau = []
    for i in range(m):
        row = A[i] + [int(j == i) for j in range(m)] + [b[i]]
        tableau.append(row)
    row = c + [0] * (m + 1)
    tableau.append(row)

    return tableau


def pivot_index(tableau):
    cN = tableau[-1][:-1]
    piv_col = -1
    for col, x in enumerate(cN):
        if x > 0:
            piv_col = col
            break
    if piv_col == -1:
        return False, -1, -1

    a = [tableau[i][piv_col] for i in range(len(tableau) - 1)]
    b = [tableau[i][-1] for i in range(len(tableau) - 1)]
    thetas = [bi / ai if ai > 0 else float('inf') for ai, bi in zip(a, b)]
    piv_row = thetas.index(min(thetas))

    if min(thetas) == float('inf'):
        raise Exception("解が非有界")

    return True, piv_row, piv_col


def step(tableau, piv_row, piv_col):
    h = len(tableau)
    w = len(tableau[0])
    piv = tableau[piv_row][piv_col]

    for j in range(w):
        tableau[piv_row][j] /= piv

    for i in range(h):
        if i == piv_row:
            continue
        d = tableau[i][piv_col]
        for j in range(w):
            tableau[i][j] -= d * tableau[piv_row][j]


def simplex(A, b, c):
    tableau = make_tableau(A, b, c)

    while True:
        improved, piv_row, piv_col = pivot_index(tableau)
        if not improved:
            break
        step(tableau, piv_row, piv_col)

    return -tableau[-1][-1]


C, D = map(int, input().split())
A = [
    [3 / 4, 2 / 7],
    [1 / 4, 5 / 7]
]
b = [C, D]
c = [1000, 2000]

print(simplex(A, b, c))
0