結果

問題 No.357 品物の並び替え (Middle)
ユーザー efunyoefunyo
提出日時 2020-03-27 12:41:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 170 ms / 5,000 ms
コード長 1,411 bytes
コンパイル時間 2,162 ms
コンパイル使用メモリ 86,808 KB
実行使用メモリ 79,160 KB
最終ジャッジ日時 2023-08-30 16:22:32
合計ジャッジ時間 5,414 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 105 ms
77,632 KB
testcase_01 AC 92 ms
71,400 KB
testcase_02 AC 100 ms
76,736 KB
testcase_03 AC 98 ms
76,704 KB
testcase_04 AC 102 ms
77,192 KB
testcase_05 AC 104 ms
77,652 KB
testcase_06 AC 92 ms
76,536 KB
testcase_07 AC 86 ms
71,392 KB
testcase_08 AC 108 ms
77,560 KB
testcase_09 AC 132 ms
78,296 KB
testcase_10 AC 118 ms
77,704 KB
testcase_11 AC 123 ms
78,432 KB
testcase_12 AC 170 ms
78,504 KB
testcase_13 AC 137 ms
78,328 KB
testcase_14 AC 136 ms
78,084 KB
testcase_15 AC 125 ms
78,656 KB
testcase_16 AC 122 ms
78,232 KB
testcase_17 AC 131 ms
79,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#https://yukicoder.me/problems/810

def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    N,M = map(int, input().split())
    #edge[i]:品物iの後にあると得点
    edge = [[] for i in range(N)]
    for _ in range(M):
        a,b,c = map(int, input().split())
        edge[a].append([b,c])

    #dp[s]:sは並べ済みの品物
    #         dp[s]は残りの品物から得られる最大得点
    dp = [-1]*(1<<N)
    dp[-1] = 0


    def solve(s):
        if dp[s] >= 0:
            return dp[s]

        for i in range(N):
            if s & (1<<i):
                continue
            #品物iを並べることで得られる得点
            point = 0
            for j in range(N):
                if s & (1<<j):
                    for b,c in edge[j]:
                        if b==i:
                            point += c
                            break
            dp[s] = max(dp[s], solve(s|(1<<i))+point)
        return dp[s]

    print(solve(0))

if __name__ == '__main__':
    main()
0