結果

問題 No.8078 Very Simple Traveling Salesman Problem
ユーザー burita083
提出日時 2021-04-01 21:58:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,095 ms / 2,000 ms
コード長 796 bytes
コンパイル時間 849 ms
コンパイル使用メモリ 82,228 KB
実行使用メモリ 93,184 KB
最終ジャッジ日時 2024-12-21 06:17:29
合計ジャッジ時間 5,015 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

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