DSA 섹션쉬움

프림

'Prim' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

사전의 인접 목록(여기서 graph[u][v]은 가장자리 (u, v)의 가중치)으로 표시되는 무방향, 연결, 가중치 그래프를 취하고 Prim의 알고리즘을 사용하여 최소 스패닝 트리(MST)의 가중치 합계를 반환하는 함수 prim_mst(graph)을 작성하세요.

제약
  • 1 <= V <= 500
  • 0 <= E <= 1000

Example 1
Input
graph = {0: {1: 2, 3: 6}, 1: {0: 2, 2: 3, 3: 8, 4: 5}, 2: {1: 3, 4: 7}, 3: {0: 6, 1: 8, 4: 9}, 4: {1: 5, 2: 7, 3: 9}}
Output
16
Explanation

MST edges selected are: (0,1) wt 2, (1,2) wt 3, (1,4) wt 5, (0,3) wt 6. Total weight = 16.

Need a Hint?
세트나 힙과 같은 그래프 관련 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.