๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

[Algorithm/Java][๋ฐฑ์ค€] 1916๋ฒˆ ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ

๋ฐ˜์‘ํ˜•

[BOJ] 1916๋ฒˆ ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ

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

๋ฌธ์ œ ์ ‘๊ทผ

์–‘์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง„ ๊ฐ„์„ ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ทธ๋ž˜ํ”„์—์„œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์—ฌ์„œ ๋‹ค์ต์ŠคํŠธ๋ผ์™€ ๋ฒจ๋งŒํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2๊ฐœ๋ฅผ ๋ชจ๋‘ ์ ์šฉํ•ด์„œ ๊ตฌํ˜„ํ–ˆ๋‹ค.
๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•ด์„œ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์˜€๊ณ , ๋‹ค์ต์ŠคํŠธ๋ผ์™€ ๋ฒจ๋งŒํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํฌ์ŠคํŠธ์— ์ •๋ฆฌํ–ˆ๋‹ค.

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๋‹ค์ต์ŠคํŠธ๋ผ & ๋ฒจ๋งŒํฌ๋“œ

Code

๋‹ค์ต์ŠคํŠธ๋ผ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

class Node implements Comparable<Node> {
    int to;
    int cost;

    public Node(int to, int cost) {
        this.to = to;
        this.cost = cost;
    }

    @Override
    public int compareTo(Node o) {
        return cost - o.cost;
    }
}

public class BOJ1916 {
    static int[] distance;
    static ArrayList<ArrayList<Node>> list; // ์ธ์ ‘๋ฆฌ์ŠคํŠธ
    static boolean[] visited;
    static int start, end;
    static int n,m;
    public static void main(String[] args) throws IOException {
        initD();
        System.out.println(dijkstra(start, end));
    }

    public static void initD() throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        m = Integer.parseInt(br.readLine());
        distance = new int[n+1];
        list = new ArrayList<>();
        for(int i = 0; i<=n; i++){
            list.add(i, new ArrayList<>());
        }

        for(int i = 0; i<m; i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            int from = Integer.parseInt(st.nextToken());
            int to = Integer.parseInt(st.nextToken());
            int cost = Integer.parseInt(st.nextToken());
            list.get(from).add(new Node(to, cost));
        }
        StringTokenizer st = new StringTokenizer(br.readLine());
        start = Integer.parseInt(st.nextToken());
        end = Integer.parseInt(st.nextToken());
    }
    public static int dijkstra(int start, int end){
                // ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜
        PriorityQueue<Node> pq = new PriorityQueue<>();
                // ์ตœ๋‹จ๊ฒฝ๋กœ ํ…Œ์ด๋ธ” ์ดˆ๊ธฐํ™”
                Arrays.fill(distance, Integer.MAX_VALUE);
        pq.offer(new Node(start, 0));
        visited = new boolean[n + 1];
        distance[start] = 0;
        while(!pq.isEmpty()){
            Node node = pq.poll();
            int cur = node.to;
            if(cur == end) break;
            if(visited[cur]) continue;
            visited[cur] = true;
            for (Node next : list.get(cur)) {
                if(!visited[next.to] && distance[next.to] > distance[cur] + next.cost){
                    distance[next.to] = distance[cur] + next.cost;
                    pq.add(new Node(next.to, distance[next.to]));
                }
            }
        }
        return distance[end];
    }
}

๋ฒจ๋งŒํฌ๋“œ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

class Bus{
    int from;
    int to;
    int cost;
    public Bus(int from, int to, int cost) {
        this.from = from;
        this.to = to;
        this.cost = cost;
    }
}
public class BOJ1916 {
    static Bus[] buses;  // ๊ฐ„์„ ๋“ค์˜ ์ •๋ณด๋ฅผ ์ €์žฅ
    static int[] distance;
    static boolean[] visited;
    static int start, end;
    static int n,m;
    public static void main(String[] args) throws IOException {
        initB();
        System.out.println(bellmanFord(start));
    }
    public static void initB() throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        m = Integer.parseInt(br.readLine());
        distance = new int[n + 1];
        visited = new boolean[n + 1];
        buses = new Bus[m];
        for(int i = 0; i<m; i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            int from = Integer.parseInt(st.nextToken());
            int to = Integer.parseInt(st.nextToken());
            int cost = Integer.parseInt(st.nextToken());
            Bus bus = new Bus(from, to, cost);
            buses[i] = bus;
        }
        StringTokenizer st = new StringTokenizer(br.readLine());
        start = Integer.parseInt(st.nextToken());
        end = Integer.parseInt(st.nextToken());
    }

    public static int bellmanFord(int start){
                // ์ตœ๋‹จ ๊ฒฝ๋กœ ํ…Œ์ด๋ธ” ์ดˆ๊ธฐํ™”
        Arrays.fill(distance, Integer.MAX_VALUE);
        distance[start] = 0;
        for(int i = 1; i<=n; i++){
            for(int j = 0; j<m; j++){
                Bus bus = buses[j];
                if(distance[bus.from] == Integer.MAX_VALUE) continue;
                if(distance[bus.to] > distance[bus.from] + bus.cost){
                    distance[bus.to] = distance[bus.from] + bus.cost;
                }
            }
        }
        return distance[end];
    }
}
๋ฐ˜์‘ํ˜•