목차

우주신과의 교감

ps
링크acmicpc.net/…
출처BOJ
문제 번호1774
문제명우주신과의 교감
레벨골드 4
분류

최소 신장 트리

시간복잡도O(n^2)
인풋사이즈n<=1000
사용한 언어Python
제출기록32952KB / 388ms
최고기록388ms
해결날짜2022/10/04
태그

30단계

풀이

코드

"""Solution code for "BOJ 1774. 우주신과의 교감".

- Problem link: https://www.acmicpc.net/problem/1774
- Solution link: http://www.teferi.net/ps/problems/boj/1774

Tags: [Minimum spanning tree]
"""

import math
import sys
from teflib import graph as tgraph


def main():
    N, M = [int(x) for x in sys.stdin.readline().split()]
    coords = [[int(x) for x in sys.stdin.readline().split()] for _ in range(N)]
    edges = set()
    for _ in range(M):
        u, v = [int(x) for x in sys.stdin.readline().split()]
        edges.add((u - 1, v - 1))
        edges.add((v - 1, u - 1))

    mat_graph = tgraph.create_mat_graph(
        N, lambda u, v: (0 if
                         (u, v) in edges else math.dist(coords[u], coords[v])))
    mst_edges = tgraph.minimum_spanning_tree_dense(mat_graph)
    answer = sum(w for u, v, w in mst_edges)
    print(f'{answer:.2f}')


if __name__ == '__main__':
    main()