πŸ“– [λ°±μ€€μ•Œκ³ λ¦¬μ¦˜ 풀이] Q.1238 νŒŒν‹° 문제 풀이 - java

πŸ“– 문제

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

N개의 숫자둜 κ΅¬λΆ„λœ 각각의 λ§ˆμ„μ— ν•œ λͺ…μ˜ 학생이 μ‚΄κ³  μžˆλ‹€.

μ–΄λŠ λ‚  이 Nλͺ…μ˜ 학생이 X (1 ≀ X ≀ N)번 λ§ˆμ„μ— λͺ¨μ—¬μ„œ νŒŒν‹°λ₯Ό 벌이기둜 ν–ˆλ‹€. 이 λ§ˆμ„ μ‚¬μ΄μ—λŠ” 총 M개의 단방ν–₯ λ„λ‘œλ“€μ΄ 있고 i번째 길을 μ§€λ‚˜λŠ”λ° Ti(1 ≀ Ti ≀ 100)의 μ‹œκ°„μ„ μ†ŒλΉ„ν•œλ‹€.

각각의 학생듀은 νŒŒν‹°μ— μ°Έμ„ν•˜κΈ° μœ„ν•΄ κ±Έμ–΄κ°€μ„œ λ‹€μ‹œ κ·Έλ“€μ˜ λ§ˆμ„λ‘œ λŒμ•„μ™€μ•Ό ν•œλ‹€. ν•˜μ§€λ§Œ 이 학생듀은 μ›Œλ‚™ κ²Œμ„λŸ¬μ„œ μ΅œλ‹¨ μ‹œκ°„μ— 였고 κ°€κΈ°λ₯Ό μ›ν•œλ‹€.

이 λ„λ‘œλ“€μ€ 단방ν–₯이기 λ•Œλ¬Έμ— μ•„λ§ˆ 그듀이 였고 κ°€λŠ” 길이 λ‹€λ₯Όμ§€λ„ λͺ¨λ₯Έλ‹€. Nλͺ…μ˜ 학생듀 쀑 였고 κ°€λŠ”λ° κ°€μž₯ λ§Žμ€ μ‹œκ°„μ„ μ†ŒλΉ„ν•˜λŠ” 학생은 λˆ„κ΅¬μΌμ§€ κ΅¬ν•˜μ—¬λΌ.

πŸ” 접근법

BOJ Q.1238 νŒŒν‹° λ¬Έμ œμž…λ‹ˆλ‹€.

M개의 단방ν–₯ λ„λ‘œλ“€μ΄ 주어지고 각 λ„λ‘œμ— λŒ€ν•΄ μ§€λ‚˜κ°€λŠ” μ‹œκ°„μ΄ μ£Όμ–΄μ§‘λ‹ˆλ‹€.

μ—¬κΈ°μ„œ ꡬ해야 ν•  것은

Nλͺ…μ˜ 학생이 X번 λ§ˆμ„μ— λͺ¨μ—¬μ„œ νŒŒν‹°λ₯Ό 벌일 λ•Œ Nλͺ…μ˜ 학생듀 쀑 였고 κ°€λŠ”λ° κ°€μž₯ λ§Žμ€ μ‹œκ°„μ„ μ†ŒλΉ„ν•˜λŠ” 학생이

λˆ„κ΅¬μΈμ§€λ₯Ό κ΅¬ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

ν•˜λ‚˜μ˜ μ •μ μœΌλ‘œ λΆ€ν„° λͺ¨λ“  μ •μ κΉŒμ§€μ˜ μ΅œλ‹¨κ±°λ¦¬λ₯Ό ꡬ할 수 μžˆλŠ” λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ μ΄μš©ν•˜λ©΄ 풀이가 κ°€λŠ₯ν•©λ‹ˆλ‹€.

λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ 톡해 도착 지점은 X번으둜 μ •ν•΄μ ΈμžˆμœΌλ‹ˆ

각 ν•™μƒμ˜ 집뢀터 xλ²ˆκΉŒμ§€ 그리고 λŒμ•„μ™€μ•Ό ν•˜λ‹ˆ xλ²ˆμ§€μ  λΆ€ν„° μ§‘κΉŒμ§€μ˜

μ΅œλ‹¨ 경둜λ₯Ό κ΅¬ν•˜μ—¬μ„œ κ·Έ 쀑 μ΅œμ†Ÿκ°’μ„ κ΅¬ν•˜λ©΄

정닡을 λ„μΆœν•  수 μžˆμŠ΅λ‹ˆλ‹€.

πŸ’» μ½”λ“œ

package problem.shortest.Q1238;

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;

public class Main_1238 {

    static class Node implements Comparable<Node>{
        int index;
        int distance;

        public Node(int index, int distance) {
            this.index = index;
            this.distance = distance;
        }

        @Override
        public int compareTo(Node target) {
            // μž‘μ€ 거리 λΉ„μš©μ΄ λ¨Όμ € μ˜€λ„λ‘
            return this.distance - target.distance;
        }
    }

    static final int INF = 987654321;
    static int N, M, start,end;
    static List<List<Node>> adList;
    static int[] distance;
    static boolean[] visited;
    static PriorityQueue<Node> priorityQueue;


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int answer = Integer.MIN_VALUE;

        String input[] = br.readLine().split(" ");

        N = Integer.parseInt(input[0]);
        M = Integer.parseInt(input[1]);
        end = Integer.parseInt(input[2]);

        adList = new ArrayList<>();
        // 인덱슀λ₯Ό 1λΆ€ν„° ν•˜κΈ° μœ„ν•΄μ„œ ν•œ 개λ₯Ό λ„£μ–΄λ‘ 
        adList.add(new <Node>ArrayList());
        for (int i = 1; i <= N; i++) {
            adList.add(new <Node>ArrayList());
        }



        for (int i = 0; i < M; i++) {
            String[] temp = br.readLine().split(" ");
            int u = Integer.parseInt(temp[0]);
            int v = Integer.parseInt(temp[1]);
            int cost = Integer.parseInt(temp[2]);
            adList.get(u).add(new Node(v, cost));
        }

        for(int i = 1; i<=N; i++) {
            int sum = solve(i,end);
            sum += solve(end,i);
            answer = Integer.max(sum,answer);
        }

        System.out.println(answer);
    }

    private static int solve(int start , int end) {
        priorityQueue = new PriorityQueue<>();
        visited = new boolean[N +1];
        distance = new int[N + 1];
        Arrays.fill(distance, INF);

        distance[start] = 0;
        priorityQueue.add(new Node(start, 0));

        while(!priorityQueue.isEmpty()) {
            Node current =  priorityQueue.poll();

            if(visited[current.index]){
                continue;
            }
            visited[current.index] = true;
            // μ—°κ²°λœ 정점듀을 확인
            for(Node destination : adList.get(current.index)) {
                //λͺ©μ μ§€ 직행 , ν˜„μž¬κΉŒμ§€ 거리 + ν˜„μž¬μ—μ„œ λͺ©μ μ§€ 거리
                if(distance[destination.index] > distance[current.index] + destination.distance) {
                    // μ΅œμ†Œ 거리 λΉ„μš© κ°±μ‹ 
                    distance[destination.index] = distance[current.index] + destination.distance;
                    priorityQueue.add(new Node(destination.index, distance[destination.index]));
                }
            }
        }
        return distance[end];
    }
}


Written on April 20, 2021