๐Ÿš™ ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ

๋ฐฑ์ค€ Class4 Gold4

solved_ac[Class4] ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ

๋ฌธ์ œ


๋ฐฉํ–ฅ์„ฑ์ด ์—†๋Š” ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์„ธ์ค€์ด๋Š” 1๋ฒˆ ์ •์ ์—์„œ N๋ฒˆ ์ •์ ์œผ๋กœ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋กœ ์ด๋™ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋˜ํ•œ ์„ธ์ค€์ด๋Š” ๋‘ ๊ฐ€์ง€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด์„œ ์ด๋™ํ•˜๋Š” ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๊ณ  ์‹ถ์€๋ฐ, ๊ทธ๊ฒƒ์€ ๋ฐ”๋กœ ์ž„์˜๋กœ ์ฃผ์–ด์ง„ ๋‘ ์ •์ ์€ ๋ฐ˜๋“œ์‹œ ํ†ต๊ณผํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์„ธ์ค€์ด๋Š” ํ•œ๋ฒˆ ์ด๋™ํ–ˆ๋˜ ์ •์ ์€ ๋ฌผ๋ก , ํ•œ๋ฒˆ ์ด๋™ํ–ˆ๋˜ ๊ฐ„์„ ๋„ ๋‹ค์‹œ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ˜๋“œ์‹œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•ด์•ผ ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์— ์ฃผ์˜ํ•˜๋ผ. 1๋ฒˆ ์ •์ ์—์„œ N๋ฒˆ ์ •์ ์œผ๋กœ ์ด๋™ํ•  ๋•Œ, ์ฃผ์–ด์ง„ ๋‘ ์ •์ ์„ ๋ฐ˜๋“œ์‹œ ๊ฑฐ์น˜๋ฉด์„œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ


์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 โ‰ค N โ‰ค 800, 0 โ‰ค E โ‰ค 200,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ E๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ a, b, c๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, a๋ฒˆ ์ •์ ์—์„œ b๋ฒˆ ์ •์ ๊นŒ์ง€ ์–‘๋ฐฉํ–ฅ ๊ธธ์ด ์กด์žฌํ•˜๋ฉฐ, ๊ทธ ๊ฑฐ๋ฆฌ๊ฐ€ c๋ผ๋Š” ๋œป์ด๋‹ค. (1 โ‰ค c โ‰ค 1,000) ๋‹ค์Œ ์ค„์—๋Š” ๋ฐ˜๋“œ์‹œ ๊ฑฐ์ณ์•ผ ํ•˜๋Š” ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์ •์  ๋ฒˆํ˜ธ v1๊ณผ v2๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (v1 โ‰  v2, v1 โ‰  N, v2 โ‰  1) ์ž„์˜์˜ ๋‘ ์ •์  u์™€ v์‚ฌ์ด์—๋Š” ๊ฐ„์„ ์ด ์ตœ๋Œ€ 1๊ฐœ ์กด์žฌํ•œ๋‹ค.

์ถœ๋ ฅ


์ฒซ์งธ ์ค„์— ๋‘ ๊ฐœ์˜ ์ •์ ์„ ์ง€๋‚˜๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๊ทธ๋Ÿฌํ•œ ๊ฒฝ๋กœ๊ฐ€ ์—†์„ ๋•Œ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1

4 6
1 2 3
2 3 3
3 4 1
1 3 5
2 4 5
1 4 4
2 3

์˜ˆ์ œ ์ถœ๋ ฅ 1

7

๐Ÿ“– ์•Œ๊ณ ๋ฆฌ์ฆ˜

โญ๏ธ[๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ Weight๊ฐ€ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„์—์„œ <ํŠน์ • ๋…ธ๋“œ โžก๏ธ ๋ชจ๋“  ๋…ธ๋“œ> ์ตœ์†Œ ๋น„์šฉ์„ ๊ตฌํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.]โญ๏ธ

์ด ๋ฌธ์ œ๋ฅผ ๋ถ„์„ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • Weight๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ทธ๋ž˜ํ”„
  • ๋…ธ๋“œ 1(ํŠน์ • ๋…ธ๋“œ)์—์„œ ๋…ธ๋“œ N์œผ๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ(์ตœ์†Œ ๋น„์šฉ)์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.

๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋…ธ๋“œ 1์—์„œ๋ถ€ํ„ฐ ๋ชจ๋“  ๋…ธ๋“œ์˜ ์ตœ์†Œ ๋น„์šฉ์ด ๊ตฌํ•ด์งˆํ…Œ๊ณ  ์šฐ๋ฆฌ๋Š” ๋…ธ๋“œ N(N๋ฒˆ ์ธ๋ฑ์Šค)๋งŒ ์ถ”์ถœํ•˜์—ฌ ์ด์šฉํ•˜๋ฉด ์ •๋‹ต์„ ๋งž์ถœ ์ˆ˜ ์žˆ๋‹ค.

Heap์„ ์ด์šฉํ•œ ๋‹ค์ต์ŠคํŠธ๋ผ ๊ธฐ๋ณธ ๊ณจ๊ฒฉ ์ฝ”๋“œ

## ๋ฌธ์ œ ์กฐ๊ฑด ์„ธํŒ… ##
N,E = map(int,input().split())
graph = [[] for _ in range(N+1)]

for i in range(E):
    a,b,c = map(int,input().split())
    graph[a].append((b,c))
    graph[b].append((a,c))

v1,v2 = map(int,input().split())
####

def dijkstra(start):
    distance = [INF]*(N+1)
    heap = []
    h.heappush(heap,(0,start))
    distance[start] = 0

    while heap:
        dist,now = h.heappop(heap)

        if distance[now] < dist:
            continue

        for i in graph[now]:
            cost = dist + i[1]

            if cost < distance[i[0]]:
                distance[i[0]] = cost
                h.heappush(heap,(cost,i[0]))
    
    return distance

๐Ÿ“ ์„ค๊ณ„

  1. ๋…ธ๋“œ 1 โžก๏ธ ๋…ธ๋“œ v1 โžก๏ธ ๋…ธ๋“œ v2 โžก๏ธ ๋…ธ๋“œ N
  2. ๋…ธ๋“œ 1 โžก๏ธ ๋…ธ๋“œ v2 โžก๏ธ ๋…ธ๋“œ v1 โžก๏ธ ๋…ธ๋“œ N

1๋ฒˆ๊ณผ 2๋ฒˆ ์ค‘ ์ตœ์†Œ ์ถœ๋ ฅ

โš ๏ธ ์ฃผ์˜์‚ฌํ•ญ

๋งˆ์ง€๋ง‰ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์กฐ๊ฑด

if answer == INF:

๋กœ ํ•˜๋ฉด ํ‹€๋ฆฐ๋‹ค.

์™œ๋ƒํ•˜๋ฉด ๋…ธ๋“œ 1 โžก๏ธ ๋…ธ๋“œ v1 = INF , ๋…ธ๋“œ v1 โžก๏ธ ๋…ธ๋“œ v2 = INF , ๋…ธ๋“œ v2 โžก๏ธ ๋…ธ๋“œ N = INF ์ผ ๊ฒฝ์šฐ์—

answer์˜ ๊ฐ’์€ 3*INF๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋Š”

if answer >= INF:

๋กœ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป CODE

import heapq as h
import sys
input = sys.stdin.readline
INF = int(1e9)
## ๋ฌธ์ œ ์กฐ๊ฑด ์„ธํŒ… ##
N,E = map(int,input().split())
graph = [[] for _ in range(N+1)]

for i in range(E):
    a,b,c = map(int,input().split())
    graph[a].append((b,c))
    graph[b].append((a,c))

v1,v2 = map(int,input().split())
####

# ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜
# ํŠน์ • ๋…ธ๋“œ --> ํŠน์ • ๋…ธ๋“œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ
# ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•˜์—ฌ ํž™์„ ์‚ฌ์šฉ
def dijkstra(start):
    distance = [INF]*(N+1)
    heap = []
    h.heappush(heap,(0,start))
    distance[start] = 0

    while heap:
        dist,now = h.heappop(heap)

        if distance[now] < dist:
            continue

        for i in graph[now]:
            cost = dist + i[1]

            if cost < distance[i[0]]:
                distance[i[0]] = cost
                h.heappush(heap,(cost,i[0]))
    
    return distance

# === ๋…ธ๋“œ 1 --> ๋…ธ๋“œ N (v1,v2 ๊ฒฝ์œ ) ===
# ๋…ธ๋“œ 1 --> ๋…ธ๋“œ v1 ์ตœ๋‹จ ๊ฒฝ๋กœ
# ๋…ธ๋“œ v1 --> ๋…ธ๋“œ v2 ์ตœ๋‹จ ๊ฒฝ๋กœ
# ๋…ธ๋“œ v2 --> ๋…ธ๋“œ N ์ตœ๋‹จ ๊ฒฝ๋กœ
# =======================================

sum1 = dijkstra(1)[v1] + dijkstra(v1)[v2] + dijkstra(v2)[N]

sum2 = dijkstra(1)[v2] + dijkstra(v2)[v1] + dijkstra(v1)[N]

answer = min(sum1,sum2)

if answer >= INF:
    print(-1)
else:
    print(answer)

ยฉ 2022. All rights reserved.

Powered by Hydejack v9.1.6