5972. 택배배송(Java)

2023. 10. 23. 17:43·Algorithm & Data Structures/BOJ

대표적인 다익스트라 문제

1. list를 사용하여 간선저장(간선리스트)

2. priority queue 를 사용하여 간선 cost 낮은순 자동정렬 저장 및 visited 체크

3. dist로 1번집에서 다른집으로 갈때의 최소 cost 누적 비교 저장

4. dist[N] 이 답이다.

package BOJ;

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

class Node implements Comparable<Node>{
	int end, val;

	public Node(int end, int val) {
		super();
		this.end = end;
		this.val = val;
	}

	@Override
	public int compareTo(Node o) {
		return this.val - o.val;
	}
	
}

public class Main {

	static int N, M, start, end, val, INF=Integer.MAX_VALUE;
	static int[] dist;
	static boolean isVisited[];
	
	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());
		M = Integer.parseInt(st.nextToken());
		ArrayList<Node>[] list ;
		isVisited = new boolean[N+1];
		dist = new int[N+1];
		
		Arrays.fill(dist, INF);
		
		list= new ArrayList[N+1];
		for(int i = 0 ; i < N+1 ; i++) {
			list[i]=new ArrayList<>();
		}
		
		for(int i = 0 ; i < M ; i++) {
			st = new StringTokenizer(br.readLine());
			start = Integer.parseInt(st.nextToken());
			end = Integer.parseInt(st.nextToken());
			val = Integer.parseInt(st.nextToken());
			
			list[start].add(new Node(end,val));
			list[end].add(new Node(start,val));
		}
		
		PriorityQueue<Node> pq = new PriorityQueue<>();
		pq.add(new Node (1,0));
		dist[1]=0;
		while(!pq.isEmpty()) {
			Node now = pq.poll();
			
			if(!isVisited[now.end]) isVisited[now.end] =true;
			else continue;
			
			for(int i = 0 ; i < list[now.end].size();i++) {
				Node next = list[now.end].get(i);
				if(dist[next.end]> dist[now.end]+next.val) {
					dist[next.end] = dist[now.end]+next.val;
					pq.add(new Node(next.end,dist[next.end]));
				}
			}
		}
		
		System.out.println(dist[N]);
	}
}

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

2294. 동전2 (Java)  (0) 2023.10.30
1238. 파티 (Java)  (0) 2023.10.24
1446. 지름길 (Java)  (1) 2023.10.21
1916. 최소비용구하기 (Java)  (1) 2023.10.20
13549. 숨바꼭질3 (Java)  (0) 2023.10.17
'Algorithm & Data Structures/BOJ' 카테고리의 다른 글
  • 2294. 동전2 (Java)
  • 1238. 파티 (Java)
  • 1446. 지름길 (Java)
  • 1916. 최소비용구하기 (Java)
Geisha
Geisha
개발 일기
  • Geisha
    Geisha
    Geisha
  • 전체
    오늘
    어제
    • 분류 전체보기 (316)
      • Algorithm & Data Structures (238)
        • BOJ (96)
        • 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)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
Geisha
5972. 택배배송(Java)
상단으로

티스토리툴바