結果

問題 No.90 品物の並び替え
ユーザー 萩3萩3
提出日時 2021-04-30 14:55:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 150 ms / 5,000 ms
コード長 3,676 bytes
コンパイル時間 399 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 89,216 KB
最終ジャッジ日時 2024-07-18 12:06:16
合計ジャッジ時間 2,351 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
86,400 KB
testcase_01 AC 149 ms
89,088 KB
testcase_02 AC 129 ms
86,400 KB
testcase_03 AC 146 ms
89,088 KB
testcase_04 AC 144 ms
88,576 KB
testcase_05 AC 150 ms
89,068 KB
testcase_06 AC 149 ms
89,080 KB
testcase_07 AC 131 ms
86,784 KB
testcase_08 AC 127 ms
86,272 KB
testcase_09 AC 149 ms
89,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections
import decimal
import math
import sys
import time

#mod = 1_000_000_007
def resolve():
    inp = int2d()
    n,m = inp[0]
    graph = [[0]*n for _ in range(n)]
    for item1,item2,score in inp[1:]:
        graph[item1][item2] = score

    #bit, last vertex
    dp = [[0]*n for _ in range(1<<n)]
    for bit in range(1<<n):
        for vertex in range(n):
            if not(bit & 1<<vertex): continue
            if bit == 1<<vertex: break

            prebit = bit ^ 1<<vertex
            delta = 0
            tmp = 0
            for shift in range(n):
                tmp = max(tmp, dp[prebit][shift])
                if prebit & 1<<shift: delta += graph[shift][vertex]
            
            dp[bit][vertex] = tmp + delta

    print(max(dp[-1]))

#region MyLibrary
#region Input
def str1d():return sys.stdin.read().splitlines()
def int1d():return list(map(int, sys.stdin.read().split()))
def float1d():return list(map(float,sys.stdin.read().split()))
def dec1d():return list(map(decimal.Decimal,sys.stdin.read().split()))
def str2d():return [list(s.split()) for s in str1d()]
def int2d():return [list(map(int,s.split())) for s in str1d()]
def float2d():return [list(map(float,s.split())) for s in str1d()]
def dec2d():return [list(map(decimal.Decimal,s.split())) for s in str1d()]
#endregion

#region math
def sieve_of_eratosthenes(n):
    # 29 => [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
    if n <= 1: return []
    if n == 2: return [2]
    
    deq = collections.deque([2])
    sqrt_i = int(decimal.Decimal(n).sqrt())//2
    isprime = [True] * math.ceil(n/2)#i => 2i+1
    len_is_prime = len(isprime)
    
    for i in range(1, len_is_prime):
        if not isprime[i]: continue

        deq.append(2*i+1)
        if i>sqrt_i: continue

        for j in range(2*i*(i+1), len_is_prime, 2*i+1):
            isprime[j] = False
    return list(deq)

def alldevisor(n):
    # 30 => [1, 2, 3, 5, 6, 10, 15, 30]
    if n == 1:return[1]
    l, r = collections.deque([1]), collections.deque([n])
    tmp = 2
    while(tmp**2 <= n):
        if n%tmp == 0:
            l.append(tmp)
            if n != tmp**2: r.appendleft(n//tmp)
        tmp += 1
    return list(l+r)

def factorization(n, prime_iterable = None):
    # 48 => [(2,4), (3,1)]
    seq = prime_iterable if prime_iterable != None else range(2,n)
    l = []
    for prime in seq:
        if prime * prime > n: break
        cnt = 0
        while n % prime ==0:
            n //= prime
            cnt += 1
        if cnt > 0: l.append((prime, cnt))
    if n > 1: l.append((n, 1))
    return l
#endregion

class SegTree:
    """
    reffered to 'maspy', noncommutative operation is available
    X_f = min, X_unit = INF
    X_f = max, X_unit = -INF
    X_f = sum, X_unit = 0
    X_f = lambda _,a,b: a+b, X_unit = ''
    """
    X_unit = ''
    X_f = lambda _,a,b:a+b

    def __init__(self, seq):
        l = list(seq)
        self.N = len(l)
        self.X = [self.X_unit] * self.N + l
        for i in range(self.N - 1, 0, -1):
            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])

    def set_val(self, i, x):
        i += self.N
        self.X[i] = x
        while i > 1:
            i >>= 1
            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])

    def fold(self, L, R):#[L,R)
        L += self.N
        R += self.N
        vL = self.X_unit
        vR = self.X_unit
        while L < R:
            if L & 1:
                vL = self.X_f(vL, self.X[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.X_f(self.X[R], vR)
            L >>= 1
            R >>= 1
        return self.X_f(vL, vR)

#endregion

resolve()
0