Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 11779. 최소비용구하기2 본문

acmicpc

[BAEKJOON] 11779. 최소비용구하기2

코드와이 2021. 12. 10. 18:54

 

다익스트라

 

문제링크

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

 

11779번: 최소비용 구하기 2

첫째 줄에 도시의 개수 n(1≤n≤1,000)이 주어지고 둘째 줄에는 버스의 개수 m(1≤m≤100,000)이 주어진다. 그리고 셋째 줄부터 m+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 버스

www.acmicpc.net

 

package acmicpc.Gold3;

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

public class 최소비용구하기2 {

	static int n;
	static List<Node>[] list;
	static class Node{
		int to, weight;

		public Node(int to, int weight) {
			super();
			this.to = to;
			this.weight = weight;
		}
		
	}
	public static void main(String[] args) throws NumberFormatException, IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		n = Integer.parseInt(br.readLine());
		int m = Integer.parseInt(br.readLine());
		list = new ArrayList[n + 1];
		for(int i = 1 ; i <= n ; i++) {
			list[i] = new ArrayList<>();
		}
		
		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));
		}
		
		st = new StringTokenizer(br.readLine());
		int start = Integer.parseInt(st.nextToken());
		int end = Integer.parseInt(st.nextToken());
		
		go(start, end);
		
	}
	
	public static void go(int s, int e) {
		
		PriorityQueue<Node> pq = new PriorityQueue<>((o1, o2) -> (o1.weight - o2.weight));
		pq.add(new Node(s, 0));
		
		int[] dist = new int[n + 1];
		Arrays.fill(dist, 987654321);
		dist[s] = 0;
		
		boolean[] visited = new boolean[n + 1];
		
		int[] before = new int[n + 1];
		before[s] = -1;
		
		while(!pq.isEmpty()) {
			
			Node q = pq.poll();
			
			if(visited[q.to]) continue;
			
			visited[q.to] = true;
			
			for(Node next : list[q.to]) {
				
				if(dist[next.to] > dist[q.to] + next.weight) {
					dist[next.to] = dist[q.to] + next.weight;
					pq.add(new Node(next.to, dist[next.to]));
					
					before[next.to] = q.to;
				}
				
			}
		}
		
		StringBuilder sb = new StringBuilder();
		sb.append(Integer.toString(dist[e])).append("\n");
		
		int cnt = 1;
		int idx = e;
		String route = "" + e;
		while(before[idx] != -1) {
			cnt++;
			idx = before[idx];
			route = idx + " " + route;
		}
		
		sb.append(Integer.toString(cnt)).append("\n").append(route);
		System.out.println(sb);
		
	}
}

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 2143. 두 배열의 합  (0) 2021.12.18
[BAEKJOON] 2533.사회망 서비스  (0) 2021.12.11
[BAEKJOON] 16637. 괄호 추가하기  (0) 2021.12.06
[BAEKJOON] 11437. LCA  (0) 2021.12.01
[BAEKJOON] 2437. 저울  (0) 2021.11.23