๐Ÿค–/๋ฐฑ์ค€

๋ฐฑ์ค€ 1774: ์šฐ์ฃผ์‹ ๊ณผ์˜ ๊ต๊ฐ (Python)

sssbin 2022. 8. 4. 18:35

 

https://www.acmicpc.net/problem/1774

 

1774๋ฒˆ: ์šฐ์ฃผ์‹ ๊ณผ์˜ ๊ต๊ฐ

(1,1) (3,1) (2,3) (4,3) ์ด๋ ‡๊ฒŒ ์šฐ์ฃผ์‹ ๋“ค๊ณผ ํ™ฉ์„ ์ž์”จ์˜ ์ขŒํ‘œ๊ฐ€ ์ฃผ์–ด์กŒ๊ณ  1๋ฒˆํ•˜๊ณ  4๋ฒˆ์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด 1๋ฒˆํ•˜๊ณ  2๋ฒˆ์„ ์ž‡๋Š” ํ†ต๋กœ๋ฅผ ๋งŒ๋“ค๊ณ  3๋ฒˆํ•˜๊ณ  4๋ฒˆ์„ ์ž‡๋Š” ํ†ต๋กœ๋ฅผ ๋งŒ๋“ค๋ฉด ์‹ ๋“ค๊ณผ ์„ ์ž์”จ๋ผ

www.acmicpc.net

 

ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์ด๋ฏธ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์„ ๋ฏธ๋ฆฌ ์—ฐ๊ฒฐํ•ด์ฃผ๊ณ 

๋…ธ๋“œ๋“ค์˜ ๊ฐ„์„ ์„ ์ง์ ‘ ์ •์˜ํ•ด์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ๋‹ค

 

์ฒ˜์Œ ์ƒ๊ฐํ–ˆ๋˜ ๊ฑด 

์ด๋ฏธ ์—ฐ๊ฒฐ๋œ ํ†ต๋กœ๋“ค์„ ๋”ฐ๋กœ ์ €์žฅํ•˜๊ณ  ํ•ฉ์นœ ํ›„

๊ทธ ํ†ต๋กœ๋“ค์„ ๋นผ๊ณ  ๊ฐ„์„ ๋“ค์„ ๋ชจ๋‘ ๋Œ๋ฉด์„œ unionํ•˜๋Š” ๊ฑฐ์˜€๋Š”๋ฐ

์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹คใ… 

 

def find(parent, x):
    if parent[x] != x:
        parent[x] = find(parent, parent[x])
    return parent[x]

def union(parent, a, b):
    a = find(parent, a)
    b = find(parent, b)
    if a < b:
        parent[b] = a
    else:
        parent[a] = b

n, m = map(int, input().split())

parent = [0] * (n+1) # ๋ถ€๋ชจ ๋…ธ๋“œ ์ €์žฅ
for i in range(1, n+1):
    parent[i] = i

space = [0] # ์šฐ์ฃผ์‹ ๋“ค์˜ ์ขŒํ‘œ
for _ in range(n):
    x, y = map(int, input().split())
    space.append((x, y))

path = [] # ์ด๋ฏธ ์—ฐ๊ฒฐ๋œ ํ†ต๋กœ
for _ in range(m):
    a, b = map(int, input().split())
    union(parent, a, b)
    path.append((a, b))
    path.append((b, a))

edges = [] # ๋ชจ๋“  ๋…ธ๋“œ๋“ค์˜ ๊ฐ„์„ 
for i in range(1, n+1):
    for j in range(1, n+1):
        if i != j and (i, j) not in path:
            x1 = space[i][0]
            y1 = space[i][1]
            x2 = space[j][0]
            y2 = space[j][1]
            c = ((x2-x1)**2 + (y2-y1)**2) ** (1/2)
            edges.append((c, i, j))
edges.sort()

res = 0
for i in edges:
    c, a, b = i
    if find(parent, a) != find(parent, b):
        union(parent, a, b)
        res += c

print(f'{res:.2f}') # ์†Œ์ˆ˜์  ๋‘˜์งธ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅ

 

๊ทธ ํ›„ ์‹œ๊ฐ„์ด ์—†์–ด์„œ ๊ทธ๋Œ€๋กœ ๋ฎ์—ˆ๋‹ค๊ฐ€

3์ผ์ด ์ง€๋‚œ ์˜ค๋Š˜,,, ๋‹ค์‹œ ์ฝ”๋“œ๋ฅผ ์ข€ ์‚ดํŽด๋ณด๋‹ค๊ฐ€

path๊ฐ€ ์—†์–ด๋„ ๋Œ์•„๊ฐˆ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด์–ด์„œ ์•„์˜ˆ path ๋ถ€๋ถ„์„ ์ง€์›Œ๋ฒ„๋ ธ๋‹ค ใ…Ž

 

def find(parent, x):
    if parent[x] != x:
        parent[x] = find(parent, parent[x])
    return parent[x]

def union(parent, a, b):
    a = find(parent, a)
    b = find(parent, b)
    if a < b:
        parent[b] = a
    else:
        parent[a] = b

n, m = map(int, input().split())

parent = [0] * (n+1) # ๋ถ€๋ชจ ๋…ธ๋“œ ์ €์žฅ
for i in range(1, n+1):
    parent[i] = i

space = [0] # ์šฐ์ฃผ์‹ ๋“ค์˜ ์ขŒํ‘œ
for _ in range(n):
    x, y = map(int, input().split())
    space.append((x, y))

for _ in range(m):
    a, b = map(int, input().split())
    union(parent, a, b)

edges = [] # ๋ชจ๋“  ๋…ธ๋“œ๋“ค์˜ ๊ฐ„์„ 
for i in range(1, n+1):
    for j in range(1, n+1):
        x1 = space[i][0]
        y1 = space[i][1]
        x2 = space[j][0]
        y2 = space[j][1]
        c = ((x2-x1)**2 + (y2-y1)**2) ** (1/2)
        edges.append((c, i, j))
edges.sort()

res = 0
for i in edges:
    c, a, b = i
    if find(parent, a) != find(parent, b):
        union(parent, a, b)
        res += c

print(f'{res:.2f}') # ์†Œ์ˆ˜์  ๋‘˜์งธ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅ

 

์„ฑ๊ณต์€ ํ–ˆ์ง€๋งŒ ์‹œ๊ฐ„์ด ๋„˜ ์˜ค๋ž˜ ๊ฑธ๋ฆผ,,,