ABC 317 C - Remembering the Days
https://atcoder.jp/contests/abc317/tasks/abc317_c
提出
code: python
from collections import defaultdict
n, m = map(int, input().split())
abc = list(map(int, input().split())) for _ in range(m)
# 重みつきグラフ
d = defaultdict(list)
for a, b, c in abc:
da.append((b, c))
db.append((a, c))
def bfs()
解答
https://atcoder.jp/contests/abc317/editorial/7025
code: python
from collections import defaultdict
n, m = map(int, input().split())
abc = list(map(int, input().split())) for _ in range(m)
d = defaultdict(list)
for a, b, c in abc:
da.append((b, c))
db.append((a, c))
visited = False * (n + 1)
def dfs(v, s):
visitedv = True
max_s = s
for next, dist in dv:
if not visitednext:
total = dfs(next, s + dist)
if total > max_s:
max_s = total
visitedv = False
return max_s
ans = 0
for i in range(1, n + 1):
total = dfs(i, 0)
if total > ans:
ans = total
print(ans)
code: python
from collections import defaultdict
n, m = map(int, input().split())
abc = list(map(int, input().split())) for _ in range(m)
graph = defaultdict(list)
for a, b, c in abc:
grapha.append((b, c))
graphb.append((a, c))
def dfs(start):
res = 0
stack = []
stack.append((start, 0, set(start)))
while stack:
now, dist, visited = stack.pop()
res = max(res, dist)
for next, cost in graphnow:
if next not in visited:
new_visited = visited.copy()
new_visited.add(next)
stack.append((next, dist + cost, new_visited))
return res
ans = 0
for i in range(1, n + 1):
total = dfs(i)
ans = max(ans, total)
print(ans)
テーマ
#dfs