๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/ShortestPath

[๋ฐฑ์ค€] 16118๋ฒˆ ๋‹ฌ๋น›์—ฌ์šฐ (Java)

์ ์ด 2022. 2. 8. 16:47
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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

 

16118๋ฒˆ: ๋‹ฌ๋น› ์—ฌ์šฐ

์ฒซ ์ค„์— ๋‚˜๋ฌด ๊ทธ๋ฃจํ„ฐ๊ธฐ์˜ ๊ฐœ์ˆ˜์™€ ์˜ค์†”๊ธธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ •์ˆ˜ N, M(2 ≤ N ≤ 4,000, 1 ≤ M ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ ์ค„์— ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ a, b, d(1 ≤ a, b ≤ N, a ≠ b

www.acmicpc.net


ํ’€์ด

๋ชจ๋“  ์ •์ ์— ๋Œ€ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ

 

๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ณ ๋ ค์‚ฌํ•ญ์€ ๋Š‘๋Œ€์˜ ๊ฒฝ์šฐ ํ™€์ˆ˜/์ง์ˆ˜๋ฒˆ์งธ๋กœ ๊ฑด๋„œ์„ ๊ฒฝ์šฐ๋ฅผ ๊ฐ๊ฐ ์ €์žฅํ•ด ์ค„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•จ!

์†๋„๊ฐ€ /2๊ฐ€ ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์†Œ์ˆ˜์  ๋ฐฉ์ง€๋ฅผ ์œ„ํ•ด ์ฒ˜์Œ ์ž…๋ ฅ ๋ฐ›์„ ์‹œ weight์— *2 ๋ฅผ ๊ณฑํ•˜์—ฌ ์ €์žฅ!

 

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ PriorityQueue๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„

์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.util.*;
import java.io.*;

public class Main {
    static class Edge implements Comparable<Edge>{
        int end, weight, dir;
        public Edge(int end, int weight) {
            this.end = end;
            this.weight = weight;
        }
        public Edge(int end, int weight, int dir) {
            this.end = end;
            this.weight = weight;
            this.dir = dir;
        }
        @Override
        public int compareTo(Edge o) {
            return Integer.compare(this.weight, o.weight);
        }
    }
    static int N, M;
    static List<Edge>[] graph;
    static int[] disFox;
    static int[][] disWolf; // 1: ํ™€,์ง / 2: ๊ฑฐ๋ฆฌ
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        graph = new ArrayList[N];
        for(int i =0 ; i < N ; i++){
            graph[i] = new ArrayList<>();
        }

        disFox = new int[N];
        disWolf = new int[2][N];

        for(int i =0 ; i < M ; i++){
            st = new StringTokenizer(br.readLine());
            int to = Integer.parseInt(st.nextToken())-1;
            int from = Integer.parseInt(st.nextToken())-1;
            int dis = Integer.parseInt(st.nextToken());

            graph[to].add(new Edge(from, dis*2));   // ์ •์ˆ˜์˜ ๋‚˜๋ˆ—์…ˆ ๊ฒฐ๊ณผ๋ฅผ ์œ„ํ•ด *2
            graph[from].add(new Edge(to, dis*2));
        }

        Arrays.fill(disFox, Integer.MAX_VALUE);
        Arrays.fill(disWolf[0], Integer.MAX_VALUE);
        Arrays.fill(disWolf[1], Integer.MAX_VALUE);

        findFoxPath();
        findWolfPath();

        int cnt = 0;
        for(int i =0 ; i < N ; i++){
            if(disFox[i] < Integer.min(disWolf[0][i], disWolf[1][i])) cnt++;
        }
        System.out.println(cnt);
    }


    private static void findWolfPath() {
        PriorityQueue<Edge> pq = new PriorityQueue<>();
        pq.add(new Edge(0, 0, 0));

        disWolf[0][0] = 0;

        while(!pq.isEmpty()){
            Edge cur = pq.poll();

            if(disWolf[cur.dir][cur.end] < cur.weight) continue;    // ์ด๋ฏธ ๋น„๊ต ๊ฐ’๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ

            for(Edge nEdge : graph[cur.end]){
                int nNode = nEdge.end;
                int nWeight = cur.weight;
                int nDir = 0;

                if(cur.dir == 0){   // ํ˜„์žฌ ํ™€์ˆ˜๋ฒˆ์งธ๋กœ ๊ฑด๋„œ๋‹ค๋ฉด
                    nWeight += nEdge.weight / 2;    // ์†๋„ ๋‘๋ฐฐ
                    nDir = 1;       // ๋‹ค์Œ ์ง์ˆ˜
                } else{     // ํ˜„์žฌ ์ง์ˆ˜๋ฒˆ์งธ๋กœ ๊ฑด๋„œ๋‹ค๋ฉด
                    nWeight += nEdge.weight * 2;    // ์†๋„ 1/2๋ฐฐ
                    nDir = 0;   // ๋‹ค์Œ ํ™€์ˆ˜
                }

                if(disWolf[nDir][nNode] > nWeight){
                    disWolf[nDir][nNode] = nWeight;
                    pq.add(new Edge(nNode, nWeight, nDir));
                }
            }
        }
    }

    // ๊ธฐ๋ณธ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜
    private static void findFoxPath() {
        PriorityQueue<Edge> pq = new PriorityQueue<>();
        pq.add(new Edge(0, 0));

        disFox[0] = 0;

        while(!pq.isEmpty()){
            Edge cur = pq.poll();

            if(disFox[cur.end] < cur.weight) continue;

            for(Edge nEdge : graph[cur.end]){
                int nNode = nEdge.end;
                int nWeight = cur.weight + nEdge.weight;

                if(disFox[nNode] > nWeight){
                    disFox[nNode] = nWeight;
                    pq.add(new Edge(nNode, nWeight));
                }
            }
        }
    }
}
๋ฐ˜์‘ํ˜•