結果

問題 No.3078 Very Simple Traveling Salesman Problem
ユーザー burita083burita083
提出日時 2021-04-01 21:58:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,170 ms / 2,000 ms
コード長 796 bytes
コンパイル時間 418 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 95,600 KB
最終ジャッジ日時 2023-08-23 06:00:22
合計ジャッジ時間 5,729 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
72,804 KB
testcase_01 AC 111 ms
73,260 KB
testcase_02 AC 111 ms
72,996 KB
testcase_03 AC 977 ms
93,304 KB
testcase_04 AC 240 ms
82,384 KB
testcase_05 AC 167 ms
79,196 KB
testcase_06 AC 111 ms
73,092 KB
testcase_07 AC 396 ms
85,736 KB
testcase_08 AC 204 ms
80,780 KB
testcase_09 AC 804 ms
90,980 KB
testcase_10 AC 1,170 ms
95,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
N, M = map(int, input().split())

graph = [[] for i in range(N)]
cost = [[float('inf')]*N for _ in range(N)] # 重み

for _ in range(M):
  a, b, c = map(int, input().split())
  graph[a-1].append(b-1)
  graph[b-1].append(a-1)
  cost[a-1][b-1] = c
  cost[b-1][a-1] = c

l = []
count = 0
L = []
import copy
def dfs(start):
    l.append(start)
    if len(l) == N:
        global count
        count += 1
        l.append(l[0])

    for n in graph[start]:
        if n+1 in l:
            continue
        dfs(n)
    temp = copy.deepcopy(l)
    if len(temp) == N+1:
        L.append(temp)
    l.pop()

for i in range(N):
    dfs(i)
mn = float('inf')
for l in L:
    ans = 0
    for i in range(len(l)-1):
        ans += cost[l[i]][l[i+1]]

    mn = min(mn, ans)
print(mn)
0