본문 바로가기

Algorithm475

[파이썬] 백준 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.
[파이썬] 백준 1956 : 운동 (골드4) [파이썬] 백준 1956 : 운동 (골드4)https://www.acmicpc.net/problem/1956풀이방향성 생각사이클이 a -> b -> a로도 생긴다는 것을 알려줌.N = 400이라 N^3으로도 가능한 플로이드워셜 사용다익같은 경우, 간선이 N^^2까지 주어져서 시간/메모리 관리를 빡빡하게 해야할듯..전체코드import sysinput = lambda : sys.stdin.readline().rstrip()INF = sys.maxsizeN,M = map(int,input().split())G = [[INF]*(N+1) for _ in range(N+1)]for _ in range(M): a,b,cost = map(int,input().split()) G[a][b] = cost# 초.. 2025. 1. 5.
[파이썬] 백준 1238 : 파티 (골드3) [파이썬] 백준 1238 : 파티 (골드3)https://www.acmicpc.net/problem/1238풀이방향성 생각모든 노드에 대해서 다익스트라 돌려서 걸리는 시간 얻기. 전체코드import heapq as hqimport sysinput = lambda : sys.stdin.readline().rstrip()INF = float('inf')N,M,X = 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))def di(x): V = [INF]*(N+1) V[x] = 0 heap = [(0,x)] .. 2025. 1. 4.