๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”Ž Algorithm/๐Ÿ’ป ๋ฌธ์ œ ํ’€์ด

[๋ฐฑ์ค€] - 1277. ๋ฐœ์ „์†Œ ์„ค์น˜

by Seongpyo Hong 2021. 3. 24.

๋ฌธ์ œ

 

ํ’€์ด

1๋ฒˆ ๋ฐœ์ „์†Œ๋ถ€ํ„ฐ N๋ฒˆ์งธ ๋ฐœ์ „์†Œ๊นŒ์ง€์˜ ์ตœ์†Œ ๋น„์šฉ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. ๋จผ์ € ๊ฐ ๋ฐœ์ „์†Œ์˜ ์ขŒํ‘œ๋ฅผ ์ €์žฅ(plant)ํ•˜๊ณ , ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์ €์žฅ(connected)ํ•˜์˜€๋‹ค. ๋‹ค์Œ์œผ๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „, 1๋ฒˆ๋ถ€ํ„ฐ ๊ฐ ์ •์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” distance ๋ฐฐ์—ด์„ ์—ฐ๊ฒฐ๋œ ๊ฒฝ์šฐ์—๋Š” 0, ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” INF๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ์—ˆ๋‹ค.

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ๊ฐ€์ค‘์น˜๋ฅผ ๋”ํ•ด์ฃผ๋Š” ๋ถ€๋ถ„์€ ๊ฐ ์ขŒํ‘œ๊ฐ„์˜ ๊ฑฐ๋ฆฌ์ด๊ธฐ ๋–„๋ฌธ์— ๋‘ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๊ณต์‹์„ ํ†ตํ•ด ๊ตฌํ–ˆ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ ์ธ์ ‘ํ•œ ํ–‰๋ ฌ์—์„œ ์ฐพ๋Š” ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ์™€ ๋‹ฌ๋ฆฌ ๋ชจ๋“  ๋ฐœ์ „์†Œ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์†ํ•ด์„œ ๊ฐฑ์‹ ํ•ด ์ค„ ํ•„์š”๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 1~N๊นŒ์ง€ ์ž๊ธฐ ์ž์‹ ์„ ์ œ์™ธํ•˜๊ณ  ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐฑ์‹ ํ•ด์ฃผ์—ˆ๋‹ค.

ํ’€๋ฉด์„œ ํ—ค๋งธ๋˜ ๋ถ€๋ถ„์€ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ์‹œ์ž‘ ์ง€์ ์„ ๋ฐฉ๋ฌธ์ฒดํฌ ํ•œ ํ›„ ๋‹ค์Œ ์ ๋ถ€ํ„ฐ ์ฐพ์•˜๋Š”๋ฐ ์ด๋Ÿด ๊ฒฝ์šฐ ๋‹ค์Œ์˜ ๋ฐ˜๋ก€๊ฐ€ ์กด์žฌํ•œ๋‹ค.

1 => (0,0)
2 => (-1, -1)
3 => (1,1)

1๊ณผ 2๊ฐ€ ์—ฐ๊ฒฐ

ํ•ด๋‹น ๋ฐ˜๋ก€์—์„œ 1์„ ๋จผ์ € ๋ฐฉ๋ฌธ์ฒดํฌํ•˜๊ณ  ๋‹ค์Œ ๋ฐœ์ „์†Œ๋ถ€ํ„ฐ ํ™•์ธํ•˜๊ฒŒ ๋˜๋ฉด ์ตœ๋‹จ๋น„์šฉ์€ 2-3์„ ์ž‡๋Š” ์„ ๋ถ„์˜ ๊ธธ์ด๊ฐ€ ๋œ๋‹ค. ํ•˜์ง€๋งŒ 1์—์„œ 3์œผ๋กœ ๋ฐ”๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์— 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋ฐ”๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ์•„๋†“๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•˜๋‹ค. ๋”ฐ๋ผ์„œ 1๋ฒˆ ๋ฐœ์ „์†Œ์— ๋Œ€ํ•ด ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ๋”ฐ๋กœ ํ•˜์ง€ ์•Š์Œ์œผ๋กœ์จ  1๋ฒˆ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ๋ฐœ์ „์†Œ๊นŒ์ง€์˜ ์ง์ ‘ ์ž‡๋Š” ๊ฑฐ๋ฆฌ๊ฐ€ ๊ณ ๋ ค๋  ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋‹ค.

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

public class Solution1277 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static int N, W;
    static double M, INF = 200001;
    static Point[] plant;
    static boolean[][] connected;

    public static void main(String[] args) throws IOException {
        String[] input = br.readLine().split(" ");
        N = Integer.parseInt(input[0]);
        W = Integer.parseInt(input[1]);
        String MLine = br.readLine();
        M = Double.parseDouble(MLine);

        plant = new Point[N + 1];
        connected = new boolean[N + 1][N + 1];

        for (int i = 1; i < N + 1; i++) {
            String[] line = br.readLine().split(" ");
            plant[i] = new Point(Integer.parseInt(line[0]), Integer.parseInt(line[1]));
        }

        for (int i = 0; i < W; i++) {
            String[] line = br.readLine().split(" ");
            connected[Integer.parseInt(line[0])][Integer.parseInt(line[1])] = true;
            connected[Integer.parseInt(line[1])][Integer.parseInt(line[0])] = true;
        }

        System.out.println(dijstra());
    }

    private static long dijstra() {
        double[] distance = new double[N + 1];
        for (int i = 1; i < N + 1; i++) {
            distance[i] = INF;
        }
        distance[1] = 0;
        for (int i = 2; i < N + 1; i++) {
            if (connected[1][i]) distance[i] = 0;
        }

        boolean[] visited = new boolean[N + 1];
        for (int i = 0; i < N; i++) {
            double minDist = INF;
            int cur = 0;
            for (int j = 1; j < N + 1; j++) {
                    if (!visited[j] && minDist >= distance[j]) {
                        minDist = distance[j];
                        cur = j;
                    }
            }
            if (cur == N) break;
            visited[cur] = true;
            for (int j = 1; j < N + 1; j++) {
                if (j == cur) continue;
                int next = j;
                if (distance[next] > distance[cur] + getDistance(cur, next)) {
                    distance[next] = distance[cur] + getDistance(cur, next);
                }
            }
        }
        return (long) (distance[N] * 1000);
    }

    private static double getDistance(int cur, int next) {
        if (connected[cur][next]) return 0;

        Point src = plant[cur];
        Point dest = plant[next];
        double dist = Math.pow(src.x - dest.x, 2) + Math.pow(src.y - dest.y, 2);
        return Math.sqrt(dist);
    }

    static class Point {
        int x;
        int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}

๋Œ“๊ธ€