結果

問題 No.119 旅行のツアーの問題
ユーザー rpy3cpprpy3cpp
提出日時 2015-09-08 12:56:24
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 952 bytes
コンパイル時間 101 ms
コンパイル使用メモリ 11,028 KB
実行使用メモリ 9,280 KB
最終ジャッジ日時 2023-08-23 09:20:14
合計ジャッジ時間 1,598 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,664 KB
testcase_01 AC 45 ms
9,280 KB
testcase_02 AC 19 ms
8,704 KB
testcase_03 AC 18 ms
8,512 KB
testcase_04 AC 19 ms
8,664 KB
testcase_05 AC 20 ms
8,784 KB
testcase_06 AC 32 ms
8,976 KB
testcase_07 AC 19 ms
8,520 KB
testcase_08 AC 19 ms
8,704 KB
testcase_09 AC 19 ms
8,688 KB
testcase_10 AC 18 ms
8,564 KB
testcase_11 AC 18 ms
8,600 KB
testcase_12 AC 18 ms
8,532 KB
testcase_13 AC 18 ms
8,656 KB
testcase_14 AC 19 ms
8,516 KB
testcase_15 AC 19 ms
8,704 KB
testcase_16 AC 21 ms
8,672 KB
testcase_17 AC 18 ms
8,608 KB
testcase_18 AC 20 ms
8,576 KB
testcase_19 AC 19 ms
8,536 KB
testcase_20 AC 22 ms
8,708 KB
testcase_21 AC 21 ms
8,580 KB
testcase_22 AC 21 ms
8,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections


def read_data():
    N = int(input())
    BCs = []
    for n in range(N):
        b, c = map(int, input().split())
        BCs.append((b, c))
    M = int(input())
    Es = [0] * N
    for m in range(M):
        d, e = map(int, input().split())
        Es[d] += (1 << e)
    return N, BCs, Es


def solve(N, BCs, Es):
    dp = collections.defaultdict(int)
    dp[0] = 0
    for i in range(N):
        b, c = BCs[i]
        mask = Es[i]
        new_dp = collections.defaultdict(int)
        idx = 1 << i
        for key, val in dp.items():
            tour = key & idx
            new_key = key - tour
            new_val = val if tour else (val + c)
            if new_val > new_dp[new_key]: new_dp[new_key] = new_val
            new_key |= mask
            new_val = val + b
            if new_val > new_dp[new_key]: new_dp[new_key] = new_val
        dp = new_dp
    return dp[0]

N, BCs, Es = read_data()
print(solve(N, BCs, Es))
0