본문 바로가기

Algorithm/Graph188

[파이썬] 백준 2917 : 늑대사냥꾼 (골드2) [파이썬] 백준 2917 : 늑대사냥꾼 (골드2)https://www.acmicpc.net/problem/2917풀이방향성 생각BFS로 거리 계산 후 다익스트라 전체코드from collections import dequeimport heapq as hqimport sysinput = lambda : sys.stdin.readline().rstrip()dire = [(1,0),(0,1),(-1,0),(0,-1)]inside = lambda x,y : 0 V[ny][nx]: V[ny][nx] = nd hq.heappush(heap,(-nd,nx,ny))print(V[ey][ex])코멘트. 2025. 1. 11.
[파이썬] 백준 1726 : 로봇 (골드3) [파이썬] 백준 1726 : 로봇 (골드3)https://www.acmicpc.net/problem/1726풀이방향성 생각H * W * 4방향으로 visit 배열 생성하기 전체코드from collections import dequeimport sysinput = lambda : sys.stdin.readline().rstrip()H,W = map(int,input().split())arr = [list(map(int,input().split())) for _ in range(H)]# 인덱스 1씩 빼주기sy,sx,sd = map(lambda x:int(x)-1,input().split())ey,ex,ed = map(lambda x:int(x)-1,input().split())# 0123(동서남북) -> 0.. 2025. 1. 10.
[파이썬] 백준 2211 : 네트워크 복구 (골드2) [파이썬] 백준 2211 : 네트워크 복구 (골드2)https://www.acmicpc.net/problem/2211 풀이방향성 생각루트 노드 1이랑 모든 노드가 연결되야 한다.다른 노드까지 비용은 최소로 -> 다익다른 노드와 연결될 때, 어떤 노드에서 왔는지 trace에 기록해준다. 전체코드import heapq as hqimport sysinput = lambda : sys.stdin.readline().rstrip()INF = sys.maxsizeN,M = map(int,input().split())G = [[] for _ in range(N+1)]for _ in range(M): a,b,cost = map(int,input().split()) G[a].append((b,cost)) .. 2025. 1. 9.
[파이썬] 백준 15809 : 전국시대 (골드4) [파이썬] 백준 15809 : 전국시대 (골드4)https://www.acmicpc.net/problem/15809풀이방향성 생각유니온 파인드 + 조건문전체코드import sysinput = lambda : sys.stdin.readline().rstrip()N,M = map(int,input().split())# 입력 받기arr = [0]for _ in range(N): arr.append(int(input()))# 부모 관리P = [i for i in range(N+1)]def find(x): if P[x] != x: P[x] = find(P[x]) return P[x]# 입력 쿼리for _ in range(M): cmd,a,b = map(int,input().spl.. 2025. 1. 6.