from collections import defaultdict
n, m, s, e, t = map(int, input().split())
graph = defaultdict(list)
weight = {}
for _ in range(m):
u, v, w = map(int, input().split())
graph[u].append(v)
graph[v].append(u)
weight[(u, v)] =w
weight[(v, u)] = w
path = []
minWsum = float('inf')
def dfs_e2t(cur,tempPath,w_sum):
global s, e, t, minWsum, path
if w_sum >= minWsum: # 没必要遍历走下去了
return
if cur == t:
if w_sum < minWsum:
path = tempPath.copy()
minWsum = w_sum
return
for next in graph[cur]:
if next in tempPath:
continue
tempPath.append(next)
dfs_e2t(next, tempPath, w_sum+weight[(cur, next)])
tempPath.pop()
return
def dfs_s2t(cur,tempPath,w_sum):
global minWsum, path, s, e, t
if w_sum >= minWsum:
return
if cur == e:
dfs_e2t(cur, tempPath, w_sum)
return
for next in graph[cur]:
if next in tempPath:
continue
tempPath.append(next)
dfs_s2t(next, tempPath, w_sum+weight[(cur, next)])
tempPath.pop()
return
dfs_s2t(s, [s], 0)
print(minWsum)
print(path)
网易雷火(0428)
最新推荐文章于 2024-07-25 11:27:31 发布