티스토리 뷰
얘는 직접 간선의 가중치를 만들어줘야 하는 문제.
각 정점을 1, 2, 3, ... 번 정점이라고 생각하고 그 둘을 잇는 거리를 구해서
다른 문제들과 마찬가지로 (정점1, 정점2, 가중치) 형태로 간선을 저장한 다음에 일반적인 최소신장트리 풀 듯 풀면 된다.
from itertools import combinations
import sys
input = sys.stdin.readline
n = int(input())
edges = []
parent = [0] * (n+1)
for i in range(1, n+1):
parent[i] = i
xy_ = [[0, 0]]
for _ in range(n):
x, y = map(float, input().split())
xy_.append([x, y])
# parent[i] = i, xy_[i] = [x, y]
pair_ = list(combinations([i for i in range(1, n+1)], 2))
for p1, p2 in pair_:
edges.append((p1, p2, ((xy_[p1][0] - xy_[p2][0]) ** 2 + (xy_[p1][1] - xy_[p2][1]) ** 2) ** 0.5))
def find_parent(x):
if parent[x] != x:
parent[x] = find_parent(parent[x])
return parent[x]
def union_node(a, b):
a = find_parent(a)
b = find_parent(b)
if a < b:
parent[b] = a
else:
parent[a] = b
edges = sorted(edges, key = lambda x: x[2])
answer = 0
'코딩테스트 대비' 카테고리의 다른 글
[백준] 2887번 행성 터널 (0) | 2022.03.08 |
---|---|
[백준] 1774번 우주신과의 교감 (0) | 2022.03.08 |
[백준] 1197번 최소 스패닝 트리 (0) | 2022.03.08 |
[백준] 9372번 상근이의 여행 (0) | 2022.03.08 |
[백준] 11404번 플로이드 (0) | 2022.03.08 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- CUDA
- version
- 파이썬
- 동적프로그래밍
- torch
- 이것이코딩테스트다
- dfs
- numpy
- torchscript
- PIP
- 설치하기
- notfound
- 백준
- 최소신장트리
- LGSVL
- tensorflow
- BFS
- 코딩테스트
- 프로그래머스
- matplotlib
- 카카오
- pytorch
- n과m
- shellscript
- 다익스트라
- Python
- 백트래킹
- 설치
- error
- docker
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함