結果

問題 No.119 旅行のツアーの問題
ユーザー rpy3cpp
提出日時 2015-09-08 12:56:24
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 952 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-12-21 10:53:14
合計ジャッジ時間 1,556 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

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