-
[BOJ] 11779 최소비용 구하기2ALGORITHM/BOJ 2022. 6. 29. 23:01
https://www.acmicpc.net/problem/11779
2022-06-29
1) ArrayList 들고 다니면서
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;public class Main11779_BufferedReader {static class Route implements Comparable<Route>{int num;int cost;List<Integer> bus = new ArrayList<>();public Route(int num, int cost, List<Integer> bus) {this.num = num;this.cost = cost;this.bus = bus;}@Overridepublic int compareTo(Route o) {return this.cost - o.cost;}}public static int N, M;public static List<List<Route>> list;public static List<Integer> answer;public static PriorityQueue<Route> pq;public static int[] dist;public static void solve(int start, int end) {pq = new PriorityQueue<>();pq.add(new Route(start, 0, answer));dist[start] = 0;boolean[] vtd = new boolean[N+1];while(!pq.isEmpty()) {Route r = pq.poll();if(vtd[r.num]) continue;vtd[r.num] = true;for(Route ro: list.get(r.num)) {if(dist[ro.num] > dist[r.num] + ro.cost) {dist[ro.num] = dist[r.num] + ro.cost;List<Integer> tmp = new ArrayList<>(r.bus);tmp.add(ro.num);if(ro.num == end) {answer = tmp;}pq.add(new Route(ro.num, dist[ro.num], tmp));}}}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());st = new StringTokenizer(bf.readLine());M = Integer.parseInt(st.nextToken());list = new ArrayList<>();dist = new int[N+1];for(int i = 0; i <= N; i++) {list.add(new ArrayList<>());dist[i] = 987654321;}for(int i = 0; i < M; i++) {st = new StringTokenizer(bf.readLine());int a = Integer.parseInt(st.nextToken());int b = Integer.parseInt(st.nextToken());int c = Integer.parseInt(st.nextToken());list.get(a).add(new Route(b, c, new ArrayList<>()));}st = new StringTokenizer(bf.readLine());int start = Integer.parseInt(st.nextToken());int end = Integer.parseInt(st.nextToken());answer = new ArrayList<>();answer.add(start);solve(start, end);System.out.println(dist[end]);System.out.println(answer.size());StringBuilder sb = new StringBuilder();for(int i = 0; i < answer.size(); i++) sb.append(answer.get(i)).append(" ");System.out.println(sb.toString());}}cs 2) Visited 경로
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;public class Main11779_BufferedReader_visited {static class Route implements Comparable<Route>{int num;int cost;public Route(int num, int cost) {this.num = num;this.cost = cost;}@Overridepublic int compareTo(Route o) {return this.cost - o.cost;}}public static int N, M;public static List<List<Route>> list;public static PriorityQueue<Route> pq;public static int[] dist, visited;public static void solve(int start, int end) {pq = new PriorityQueue<>();pq.offer(new Route(start, 0));dist[start] = 0;boolean[] vtd = new boolean[N+1];while(!pq.isEmpty()) {Route r = pq.poll();if(vtd[r.num]) continue;vtd[r.num] = true;for(Route ro: list.get(r.num)) {if(dist[ro.num] > dist[r.num] + ro.cost) {dist[ro.num] = dist[r.num] + ro.cost;pq.offer(new Route(ro.num, dist[ro.num]));visited[ro.num] = r.num; // visited[cur] = previous}}}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());st = new StringTokenizer(bf.readLine());M = Integer.parseInt(st.nextToken());list = new ArrayList<>();dist = new int[N+1];visited = new int[N+1];for(int i = 0; i <= N; i++) {list.add(new ArrayList<>());dist[i] = 987654321;}for(int i = 0; i < M; i++) {st = new StringTokenizer(bf.readLine());int a = Integer.parseInt(st.nextToken());int b = Integer.parseInt(st.nextToken());int c = Integer.parseInt(st.nextToken());list.get(a).add(new Route(b, c));}st = new StringTokenizer(bf.readLine());int start = Integer.parseInt(st.nextToken());int end = Integer.parseInt(st.nextToken());solve(start, end);List<Integer> temp = new ArrayList<>();int find = end;while(find > 0) {temp.add(find);find = visited[find]; //5 -> 4 -> 1}System.out.println(dist[end]);System.out.println(temp.size());StringBuilder sb = new StringBuilder();for(int i = temp.size() - 1; i >= 0; i--) sb.append(temp.get(i)).append(" ");System.out.println(sb.toString());}}cs #문제풀이
다익스트라 문제이다.
arraylist를 들고다니면서 체크하는 방식으로 풀었는데, 다른 사람들 풀이를 보니 경로를 저장한 방법을 사용한 사람들이 있어서 해당 방법으로도 풀어보았다.
예를 들어 1 -> 4 -> 5 라는 경로로 이동 했다고 치면, end(5)부터 경로를 거꾸로 가보면 이런식으로 루트를 찾을 수 있다.
visited[5]
ㄴ visited[4]
ㄴ visited[1]
'ALGORITHM > BOJ' 카테고리의 다른 글
[백준] 3273 두 수의 합 (0) 2022.07.25 [BOJ] 1541 잃어버린 괄호 (0) 2022.07.04 [BOJ] 10282 해킹 (0) 2022.06.27 [BOJ] 14938 서강그라운드 (0) 2022.06.26 [BOJ] 2458 키 순서 (0) 2022.06.26