<Algorithm> 51. 11779번 최소비용 구하기2
by BFine반응형
1. 11779번 최소비용 구하기2
최소비용 구하기에서 경로와 정점개수를 추가로 출력하는 문제 (다익스트라)
주의할점은 경로가 최단거리로 바뀔 수 있기 때문에 유동적으로 배열의 인덱스를 활용해서 접근해야한다.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Comparator; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main { static int[] dist; static int[] path; static boolean[] visited; public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; int N = Integer.parseInt(br.readLine())+1; int M = Integer.parseInt(br.readLine()); ArrayList<Edge>[] alists = new ArrayList[N]; dist = new int[N]; path = new int[N]; visited = new boolean[N]; for(int i = 0 ; i < N ; i++) alists[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 w = Integer.parseInt(st.nextToken()); alists[a].add(new Edge(b, w)); } st = new StringTokenizer(br.readLine()); int start = Integer.parseInt(st.nextToken()); int end = Integer.parseInt(st.nextToken()); dijkstra(alists, start); ArrayList<Integer> ans = new ArrayList<>(); int count = 1; System.out.println(dist[end]); ans.add(end); while(true) { int k = path[end]; if(k == 0) break; ans.add(k); end = k; count ++; } System.out.println(count); for(int i = ans.size() -1 ; i >= 0 ; i--) { System.out.print(ans.get(i)+" "); } } public static void dijkstra(ArrayList<Edge>[] alists, int startVertex ) { PriorityQueue<Edge> pq = new PriorityQueue<>(new Comparator<Edge>() { @Override public int compare(Edge o1, Edge o2) { // TODO Auto-generated method stub return o1.weight - o2.weight; } }); for(int i = 0; i < alists.length; i++) { dist[i] = Integer.MAX_VALUE; path[i] = -1; } dist[startVertex] = 0; path[startVertex] = 0; pq.add(new Edge(startVertex, 0)); while(!pq.isEmpty()) { Edge edge = pq.poll(); int vertex = edge.vertex; if(visited[vertex]) continue; visited[vertex] = true; for(Edge e : alists[vertex]) { int layoverWeight = dist[vertex] + e.weight; int destination = e.vertex; if(dist[destination] > layoverWeight) { dist[destination] = layoverWeight; path[destination] = vertex; pq.add(new Edge(destination, dist[destination])); } } } } } class Edge{ int vertex, weight; public Edge(int vertex, int weight) { super(); this.vertex = vertex; this.weight = weight; } } | cs |
반응형
'공부(2018~2019) - 스킨변경전 > Algorithm' 카테고리의 다른 글
<Algorithm> 53. 11404번 플로이드 (0) | 2018.08.15 |
---|---|
<Algorithm> 52. 1922번 네트워크 연결 (0) | 2018.08.14 |
<Algorithm> 50. 1916번 최소비용 구하기(Dijkstra) (0) | 2018.08.13 |
<Algorithm> 49. 1197번 최소 스패닝 트리(Kruskal & prim) (0) | 2018.08.12 |
<Algorithm> 48. 1717번 집합의 표현 (0) | 2018.08.12 |
블로그의 정보
57개월 BackEnd
BFine