11779번: 최소비용 구하기 2
첫째 줄에 도시의 개수 n(1≤n≤1,000)이 주어지고 둘째 줄에는 버스의 개수 m(1≤m≤100,000)이 주어진다. 그리고 셋째 줄부터 m+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 버스
www.acmicpc.net
package 백준.자바.최소비용구하기2_11779;
import java.io.*;
import java.util.*;
import java.util.concurrent.LinkedTransferQueue;
public class Main {
StringBuilder sb = new StringBuilder();
private int V, E, start, end;
private int[] visit;
private List<ArrayList<Node>> graph = new ArrayList<>();
class Node{
int idx;
int cost;
public Node(int idx, int cost) {
this.idx = idx;
this.cost = cost;
}
}
public void solution() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int s, e, cost;
StringTokenizer st;
V = Integer.parseInt(br.readLine());
E = Integer.parseInt(br.readLine());
graph = new ArrayList<ArrayList<Node>>();
for(int i = 0; i < V+1; i++)
graph.add(new ArrayList<Node>());
for(int i = 0; i < E; i++){
st = new StringTokenizer(br.readLine());
s = Integer.parseInt(st.nextToken());
e = Integer.parseInt(st.nextToken());
cost = Integer.parseInt(st.nextToken());
graph.get(s).add(new Node(e, cost));
}
st = new StringTokenizer(br.readLine());
start = Integer.parseInt(st.nextToken());
end = Integer.parseInt(st.nextToken());
int[] dist = new int[V+1];
visit = new int[V+1];
for(int i = 0; i < V+1; i++) {
dist[i] = Integer.MAX_VALUE;
// visit[i] = i;
}
PriorityQueue<Node> q = new PriorityQueue<Node>((o1, o2) -> Integer.compare(o1.cost, o2.cost));
q.offer(new Node(start, 0));
dist[start] = 0;
while (!q.isEmpty()) {
Node curNode = q.poll();
if(curNode.idx == end)
break;
if(dist[curNode.idx] < curNode.cost) continue;
for(int i = 0; i < graph.get(curNode.idx).size(); i++){
Node next = graph.get(curNode.idx).get(i);
if(dist[next.idx] > curNode.cost+next.cost){
dist[next.idx] = curNode.cost+next.cost;
visit[next.idx] = curNode.idx;
q.offer(new Node(next.idx, dist[next.idx]));
}
}
}
int cnt = 0;
sb.append(dist[end]).append('\n');
Stack<Integer> stack = new Stack<>();
stack.push(end);
while(visit[end] != 0){
cnt += 1;
stack.push(visit[end]);
end = visit[end];
}
sb.append(cnt+1).append('\n');
while (!stack.isEmpty()) {
sb.append(stack.pop()).append(" ");
}
System.out.println(sb);
}
public static void main(String[] args) throws Exception{
new Main().solution();
}
}
'문제풀이 > 백준' 카테고리의 다른 글
소수의 연속합 1644 Java (0) | 2024.02.09 |
---|---|
네트워크연결 1922 Java (0) | 2024.02.08 |
친구네트워크 4195 Java (1) | 2024.02.07 |
다리만들기2 17472 Java (1) | 2024.02.07 |
우주신과의교감 1774 Java (0) | 2024.02.07 |