μ΅œμ†ŒλΉ„μš© κ΅¬ν•˜κΈ° 2 (λ°±μ€€, JAVA)

이 κΈ€μ˜ λͺ©μ°¨8개
  1. πŸ“Œ λ¬Έμ œμ—μ„œ μš”κ΅¬ν•˜λŠ” 것은 크게 3κ°€μ§€ 이닀.
  2. ❌ μ˜€λ‹΅ 아이디어
  3. ❌ λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ€ DFS, BFS에 λΉ„ν•΄μ„œ μ‹œκ°„ λ³΅μž‘λ„κ°€ μž‘κΈ° λ•Œλ¬Έμ— DFS, BFSλ₯Ό μ‚¬μš©ν•˜κΈ° μ–΄λ ΅λ‹€.
  4. βœ… 아이디어
  5. 3. μ΅œμ†Œ λΉ„μš©μ„ κ°–λŠ” 경둜의 λ…Έλ“œ λ°©λ¬Έ μˆœμ„œ
  6. β€œλ‚˜λŠ” 이 λ…Έλ“œμ—μ„œ μ™”μ–΄μš”!”
  7. πŸ›  문제 μ˜ˆμ‹œ
  8. πŸ’» μ†ŒμŠ€ μ½”λ“œ (Java)

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

πŸ“Œ λ¬Έμ œμ—μ„œ μš”κ΅¬ν•˜λŠ” 것은 크게 3κ°€μ§€ 이닀.

1. 좜발 λ…Έλ“œμ—μ„œ 도착 λ…Έλ“œκΉŒμ§€μ˜ μ΅œμ†Œ λΉ„μš©

2. μ΅œμ†Œ λΉ„μš©μ„ κ°–λŠ” κ²½λ‘œμ— ν¬ν•¨λœ λ…Έλ“œμ˜ 개수

3. μ΅œμ†Œ λΉ„μš©μ„ κ°–λŠ” 경둜의 λ…Έλ“œ λ°©λ¬Έ μˆœμ„œ

​

❌ μ˜€λ‹΅ 아이디어

1. 좜발 λ…Έλ“œμ—μ„œ 도착 λ…Έλ“œκΉŒμ§€μ˜ μ΅œμ†Œ λΉ„μš©

λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•˜μ—¬ κ΅¬ν•œλ‹€

​

2. μ΅œμ†Œ λΉ„μš©μ„ κ°–λŠ” κ²½λ‘œμ— ν¬ν•¨λœ λ…Έλ“œμ˜ 개수

λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜ λ‘œμ§μ€ BFS처럼 큐λ₯Ό μ‚¬μš©ν•˜λ―€λ‘œ 큐에 λ°©λ¬Έ λ…Έλ“œ 갯수 μš”μ†Œλ₯Ό μΆ”κ°€ν•œλ‹€.

​

3. μ΅œμ†Œ λΉ„μš©μ„ κ°–λŠ” 경둜의 λ…Έλ“œ λ°©λ¬Έ μˆœμ„œ

DFSλ₯Ό μ΄μš©ν•΄μ„œ κ΅¬ν˜„

​

λ‚˜μ˜ μ˜€λ‹΅ μ•„μ΄λ””μ–΄μ—μ„œλŠ” λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜, DFS μ•Œκ³ λ¦¬μ¦˜ 2κ°€μ§€λ₯Ό μ‚¬μš©ν•˜μ—¬ μ‹œκ°„ μ΄ˆκ³Όκ°€ λ°œμƒν–ˆλ‹€.

❌ λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ€ DFS, BFS에 λΉ„ν•΄μ„œ μ‹œκ°„ λ³΅μž‘λ„κ°€ μž‘κΈ° λ•Œλ¬Έμ— DFS, BFSλ₯Ό μ‚¬μš©ν•˜κΈ° μ–΄λ ΅λ‹€.

그렇기에 λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜ μ•ˆμ—μ„œ 3개의 쑰건을 λͺ¨λ‘ κ΅¬ν˜„ν•΄μ•Ό ν•œλ‹€.

​

βœ… 아이디어

1, 2 쑰건은 μƒκ°ν•˜κΈ° μ‰¬μ› μ§€λ§Œ 3번 쑰건을 생각해내지 λͺ»ν–ˆλ‹€.

​

3. μ΅œμ†Œ λΉ„μš©μ„ κ°–λŠ” 경둜의 λ…Έλ“œ λ°©λ¬Έ μˆœμ„œ

λ…Έλ“œ 배열을 ν•˜λ‚˜ λ§Œλ“€μ–΄μ„œ ν•΄κ²°ν•œλ‹€. (moving[])

μ΅œμ†Œ λΉ„μš© λ…Έλ“œλ₯Ό μ°Ύμ•˜μ„ λ•Œ, 이동할 λ…Έλ“œ 배열에 이전 μœ„μΉ˜λ₯Ό ν• λ‹Ήν•œλ‹€.

moving[list[now].get(i)[0]] = now;

// moving[now] = list[now].get(i)[0]; μœ„μ™€ 같이 ν•˜λ©΄ μ•ˆλœλ‹€.

이동할 λ…Έλ“œμ— 이전 μœ„μΉ˜λ₯Ό ν• λ‹Ήν•˜λ©°

β€œλ‚˜λŠ” 이 λ…Έλ“œμ—μ„œ μ™”μ–΄μš”!”

λΌλŠ” 것을 κΈ°λ‘ν•œλ‹€.

​

πŸ›  문제 μ˜ˆμ‹œ

μž…λ ₯이 μœ„μ™€ 같을 λ•Œ moving λ°°μ—΄μ˜ 값은 μ•„λž˜μ™€ κ°™λ‹€.

0번 인덱슀 : λ¬΄μ‹œ (node의 개수 + 1의 크기둜 μ„ μ–Έν•˜μ˜€μœΌλ―€λ‘œ)

1번 인덱슀 : μ‹œμž‘ μ§€μ μ΄λ―€λ‘œ -1

2, 3, 4 인덱슀 : 1번 λ…Έλ“œλ‘œλΆ€ν„° μΆœλ°œν–ˆλ‹€

5 인덱슀 : 4번 λ…Έλ“œλ‘œλΆ€ν„° μΆœλ°œν–ˆλ‹€

​

이 λ•Œ, 5번 μΈλ±μŠ€κ°€ 도착 λ…Έλ“œμ΄λ―€λ‘œ 5번 μΈλ±μŠ€λΆ€ν„° μ—­μΆ”μ ν•˜μ—¬ λ…Έλ“œμ˜ μ§„ν–‰ 과정을 ν™•μΈν•œλ‹€.

리슀트λ₯Ό λ§Œλ“€μ–΄ 도착 λ…Έλ“œμ—μ„œ λΆ€ν„° μ‹œμž‘λ…Έλ“œκΉŒμ§€ μ—­μΆ”μ ν•˜μ—¬ 좜λ ₯ν•˜κ²Œ λœλ‹€.

moving 배열을 -1둜 μ΄ˆκΈ°ν™” ν–ˆκΈ° λ•Œλ¬Έμ— for문의 쑰건이 μœ„μ™€ κ°™λ‹€.

​

πŸ’» μ†ŒμŠ€ μ½”λ“œ (Java)

package backjoon;

import java.io.*;
import java.util.*;

public class Main {
    static int max = Integer.MIN_VALUE;
    static int min = Integer.MAX_VALUE;
    static boolean flag = false;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //StringTokenizer st = new StringTokenizer(br.readLine());
        int node = Integer.parseInt(br.readLine());
        int edge = Integer.parseInt(br.readLine());
        List<int[]>[] list = new List[node + 1];
        for(int i=0;i<list.length;i++) list[i] = new ArrayList<>();
        for(int i=0;i<edge;i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            int temp1 = Integer.parseInt(st.nextToken());
            int temp2 = Integer.parseInt(st.nextToken());
            int temp3 = Integer.parseInt(st.nextToken());
            list[temp1].add(new int[]{temp2, temp3});
        }
        StringTokenizer st2 = new StringTokenizer(br.readLine());
        int start = Integer.parseInt(st2.nextToken());
        int end = Integer.parseInt(st2.nextToken());
        int[] distance = new int[node + 1];
        Arrays.fill(distance, Integer.MAX_VALUE);
        distance[start] = 0;
        PriorityQueue<int[]> queue = new PriorityQueue<>((a,b)->{
            if(a[1]<b[1]) return -1;
            else if(a[1]>b[1]) return 1;
            else return 0;
        });
        // now, cost, 횟수
        queue.add(new int[]{start, 0, 1});
        int move = 0;
        int[] moving = new int[node + 1];
        Arrays.fill(moving, -1);

        while(!queue.isEmpty()){
            int[] temp = queue.poll();
            int now = temp[0];
            int cost = temp[1];
            int length = temp[2];

            if(distance[now] < cost) continue;

            for(int i=0;i<list[now].size();i++){
                if(distance[list[now].get(i)[0]] > cost + list[now].get(i)[1]){
                    distance[list[now].get(i)[0]] = cost + list[now].get(i)[1];
                    moving[list[now].get(i)[0]] = now;
                    if(list[now].get(i)[0] == end) move = length + 1;
                    queue.add(new int[]{list[now].get(i)[0], cost + list[now].get(i)[1], length + 1});
                }
            }
        }

        System.out.println(distance[end]);
        System.out.println(move);

        List<Integer> print = new ArrayList<>();
        for(int i = end; i != -1;i = moving[i]){
            print.add(i);
        }
        for(int i=print.size()-1;i>=0;i--){
            System.out.print(print.get(i)+" ");
        }
    }
}
전체 κΈ€ 보기