1446. 지름길 (Java)

2023. 10. 21. 14:33·Algorithm & Data Structures/BOJ

지름길은 dijkstra 개념이 들어간 문제였다. 

DP와 비슷하다고 느꼈다.

package BOJ;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

class Road implements Comparable<Road> {
	int start;
	int end;
	int val;

	public Road(int start, int end, int val) {
		this.start = start;
		this.end = end;
		this.val = val;
	}

	@Override
	public int compareTo(Road o) {
		if (this.start == o.start)
			return this.end - o.end;
		return this.start - o.start;
	}

}

public class Main {

	static int N, D;
	static PriorityQueue<Road> pq;

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());

		N = Integer.parseInt(st.nextToken());
		D = Integer.parseInt(st.nextToken());

		pq = new PriorityQueue<>();

		for (int i = 0; i < N; i++) {

			st = new StringTokenizer(br.readLine());

			int s = Integer.parseInt(st.nextToken());
			int e = Integer.parseInt(st.nextToken());
			int v = Integer.parseInt(st.nextToken());

			if (e - s < v || e > D)
				continue;
			else
				pq.add(new Road(s, e, v));

		}
		int move = 0;
		int[] dist = new int[D + 1];
		Arrays.fill(dist, D + 1);
		dist[0] = 0;
		while (move < D) {
			if (!pq.isEmpty()) {
				Road r = pq.peek();
				if (move == r.start) {
					dist[r.end] = Math.min(dist[move] + r.val, dist[r.end]);
					pq.poll();
				} else {
					dist[move + 1] = Math.min(dist[move + 1], dist[move] + 1);
					move++;
				}
			} else {
				dist[move + 1] = Math.min(dist[move + 1], dist[move] + 1);
				move++;
			}

		}
		System.out.println(dist[D]);
	}
}

'Algorithm & Data Structures > BOJ' 카테고리의 다른 글

1238. 파티 (Java)  (0) 2023.10.24
5972. 택배배송(Java)  (1) 2023.10.23
1916. 최소비용구하기 (Java)  (1) 2023.10.20
13549. 숨바꼭질3 (Java)  (0) 2023.10.17
2206. 벽부수고 이동하기 (Java)  (0) 2023.10.16
'Algorithm & Data Structures/BOJ' 카테고리의 다른 글
  • 1238. 파티 (Java)
  • 5972. 택배배송(Java)
  • 1916. 최소비용구하기 (Java)
  • 13549. 숨바꼭질3 (Java)
Geisha
Geisha
개발 일기
  • Geisha
    Geisha
    Geisha
  • 전체
    오늘
    어제
    • 분류 전체보기 (314) N
      • Algorithm & Data Structures (236) N
        • BOJ (94) N
        • SWEA (1)
        • Programers (137)
        • Data Structures (3)
      • DB (25)
        • SQL (19)
        • RDBMS (2)
      • Java (1)
        • Class (1)
      • Spring (5)
        • Spring MVC (1)
        • Annotations (1)
      • CS (36)
        • 운영체제 (13)
        • 네트워크 (5)
      • Tool (6)
        • Git (5)
        • AWS (1)
      • Project (1)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

    스택
    동적계획법
    programmers
    binarySearch
    전위순회
    dfs
    unionfind
    Dijkstra
    DynamicProgramming
    백준
    PriorityQueue
    이분탐색
    후위순회
    SQL
    Stack
    알고리즘
    백트래킹
    유니온파인드
    구현
    baekjoon
    프로그래머스
    Java
    BFS
    골드
    dp
    algorithm
    투포인터
    다익스트라
    Union-Find
    경로압축
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
Geisha
1446. 지름길 (Java)
상단으로

티스토리툴바