14284. 간선이어가기2 (Java)

2023. 11. 1. 17:44·Algorithm & Data Structures/BOJ

전형적인 다익스트라 문제

였따.

 

package algorithm.src.minho;

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;
	int 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;
	static ArrayList<Node>[] list;
	static int[] dist;
	static int INF=Integer.MAX_VALUE;
	
	public static int Dijkstra(int a, int b) {
		PriorityQueue<Node> pq = new PriorityQueue<>();
		pq.add(new Node(a,0));
		dist[a]=0;
		while(!pq.isEmpty()) {
			
			Node now = pq.poll();
			
			for(Node next : list[now.end]) {
				if(dist[next.end]>dist[now.end]+next.val) { //현재까지의 다음 루트 가중치 VS 다음루트가는데 새로 더 걸릴가중치
					dist[next.end]=dist[now.end]+next.val;
					pq.add(new Node(next.end,dist[next.end]));
				}
			}
		}
		return dist[b];
	}
	
	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());
		
		list = new ArrayList[N+1];
		dist = new int[N+1];
		
		for(int i = 0 ; i < N+1; i++) {
			list[i] = new ArrayList<>();
		}
		
		Arrays.fill(dist, INF);
		
		for(int i = 0 ; i < M ; i++) {
			
			st = new StringTokenizer(br.readLine());
			
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			int c = Integer.parseInt(st.nextToken());
			
			list[a].add(new Node(b,c));
			list[b].add(new Node(a,c));
		}
		st = new StringTokenizer(br.readLine());
		
		int start = Integer.parseInt(st.nextToken());
		int arrive = Integer.parseInt(st.nextToken());
		
		System.out.println(Dijkstra(start, arrive));
		
		
		
	}
}

 

 

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

11779. 최소비용 구하기2 (Java)  (0) 2023.11.07
17396. 백도어 (Java)  (0) 2023.11.03
1504. 특정한 최단경로 (Java)  (0) 2023.10.31
2294. 동전2 (Java)  (0) 2023.10.30
1238. 파티 (Java)  (0) 2023.10.24
'Algorithm & Data Structures/BOJ' 카테고리의 다른 글
  • 11779. 최소비용 구하기2 (Java)
  • 17396. 백도어 (Java)
  • 1504. 특정한 최단경로 (Java)
  • 2294. 동전2 (Java)
Geisha
Geisha
개발 일기
  • Geisha
    Geisha
    Geisha
  • 전체
    오늘
    어제
    • 분류 전체보기 (311) N
      • Algorithm & Data Structures (235) N
        • BOJ (93) N
        • SWEA (1)
        • Programers (137) N
        • Data Structures (3)
      • DB (23) N
        • SQL (17) N
        • 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)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
Geisha
14284. 간선이어가기2 (Java)
상단으로

티스토리툴바