[백준] 1238번 파티 _ Python

2024. 4. 8. 16:47·Algorithms/Shortest path

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

 

1238번: 파티

첫째 줄에 N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 10,000), X가 공백으로 구분되어 입력된다. 두 번째 줄부터 M+1번째 줄까지 i번째 도로의 시작점, 끝점, 그리고 이 도로를 지나는데 필요한 소요시간 Ti가 들어

www.acmicpc.net

 

 


 

1. Preview

시간 복잡도: O(NlogM*N)

   N : 현재 노드와 연결된 간선 수 (최대 N)

   logM : 간선 heap 추출

   N : N번의 다익스트라

 

공간 복잡도: O(M)

 

참고 : -

 

유형: 다익스트라

 

 


 

2. 초기 접근 방법

'i에서 X까지의 소요시간' 과 'X에서 i까지의 소요시간' 을 구한 뒤 가장 오래 걸리는 소요시간을 구하면 된다.

# x에서 i까지의 소요시간
xToi_Distance = dijkstra(X)

result = 0 # 가장 오래 걸리는 학생의 소요시간
for i in range(1, N+1):
    if i == X:
        continue

    # i에서 x까지의 소요시간
    iTox_Distance = dijkstra(i)
    result = max(result, iTox_Distance[X] + xToi_Distance[i])

 

 


 

3. 생각

-

 


 

4. 코드

import heapq
import sys
input = sys.stdin.readline

def dijkstra(i):
    pq = []
    heapq.heappush(pq, (0, i))
    distance = [float('inf')] * (N + 1)
    distance[i] = 0

    while pq:
        cost, now = heapq.heappop(pq)

        # 기존에 저장(갱신)된 있는 cost가, heap에 저장된 cost보다 더 작으면 고려할 필요가 없다.
        if distance[now] < cost:
            continue

        for next_cost, next_node in graph[now]:
            # 현재 테이블에 저장된 비용 vs now까지 오는 비용 + now에서 next_node까지 가는 비용
            if distance[next_node] > distance[now] + next_cost:
                distance[next_node] = distance[now] + next_cost
                heapq.heappush(pq, (distance[now] + next_cost, next_node))

    return distance


N, M, X = map(int, input().split())
graph = [[] for _ in range(N+1)]

# 단방향 그래프 그리기
for _ in range(M):
    s, e, t = map(int, input().split())
    graph[s].append((t, e))

# x에서 i까지의 소요시간
xToi_Distance = dijkstra(X)

result = 0 # 가장 오래 걸리는 학생의 소요시간
for i in range(1, N+1):
    if i == X:
        continue

    # i에서 x까지의 소요시간
    iTox_Distance = dijkstra(i)
    result = max(result, iTox_Distance[X] + xToi_Distance[i])

print(result)
 
저작자표시

'Algorithm > Shortest path' 카테고리의 다른 글

[백준] 9370번 미확인 도착지 _ Python  (0) 2024.03.25
[백준] 11562번 백양로 브레이크 _ Python  (0) 2024.02.23
[백준] 1719번 택배 _ Python  (1) 2024.01.21
[백준] 1865번 웜홀 _ Python  (1) 2024.01.09
[백준] 11657번 타임머신 _ Python  (1) 2024.01.08
'Algorithms/Shortest path' 카테고리의 다른 글
  • [백준] 9370번 미확인 도착지 _ Python
  • [백준] 11562번 백양로 브레이크 _ Python
  • [백준] 1719번 택배 _ Python
  • [백준] 1865번 웜홀 _ Python
wch_t
wch_t
  • wch_t
    끄적끄적(TIL)
    wch_t
  • 글쓰기 관리
  • 전체
    오늘
    어제
    • 분류 전체보기 (168)
      • Architecture (0)
      • Algorithm (67)
        • Math (5)
        • Simulation (1)
        • Data Structure (4)
        • DP (7)
        • Brute Fource (10)
        • Binary Search (6)
        • Greedy (2)
        • Graph (11)
        • Mst (1)
        • Shortest path (10)
        • Two Pointer (1)
        • Tsp (3)
        • Union Find (2)
        • Mitm (1)
      • CS (2)
        • 데이터베이스 (5)
        • 네트워크 (5)
      • DB (6)
      • DevOps (15)
        • AWS (9)
        • Docker (1)
        • CI-CD (5)
      • Error (1)
      • Project (0)
        • kotrip (0)
      • Spring (59)
        • 끄적끄적 (5)
        • 기본 (9)
        • MVC 1 (7)
        • MVC 2 (11)
        • ORM (8)
        • JPA 1 (7)
        • JPA 2 (5)
        • Spring Data Jpa (7)
      • Test (2)
      • TIL (5)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Sxssf
    백준 3015 파이썬
    백준 17289 파이썬
    view algorithm
    백준 17299 파이썬
    apache poi
    docker: not found
    spring-cloud-starter-aws-secrets-manager-config
    애플
    response_mode
    spring-cloud-starter-bootstrap
    aws secrets manager
    TempTable
    form_post
    Merge
    docker
    scope
    Jenkins
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
wch_t
[백준] 1238번 파티 _ Python
상단으로

티스토리툴바