Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

전공공부

[BOJ_1227] 발전소 설치 본문

Study/Problem Solving

[BOJ_1227] 발전소 설치

monitor 2024. 1. 28. 12:27

설명


인접리스트 형태로 풀기 위해서 Map이라는 새로운 객체로 각 i,j 번째 행렬의 위치의 노드 위치 값을 저장하고 이를 이용해서 Dijkstra를 구현하였는데 

double dist = Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2));

//double dist = Math.sqrt(Math.abs(distX) * Math.abs(distX) + Math.abs(distY) * Math.abs(distY));

 

위에 보이는 해당 코드 형태 때문에 계속 틀렸었습니다. 약간 어이 없는 문제인데 컴퓨터의 부동소수점 표현 방식에 따른 정밀도 문제로 인해서 그렇다고 합니다.

 

적어도 PS를 할 때는 API를 적극 이용하여야 겠습니다.

 

코드


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

public class Main {
    static int N,W;
    static double M;
    static List<Data>[] graph;

    static class Map{
        int x;
        int y;
        public Map(int x,int y){
            this.x = x;
            this.y = y;
        }
    }
    static class Data implements Comparable<Data>{
        int to;
        double weight;
        public Data(int to, double weight){
            this.to = to;
            this.weight = weight;
        }
        @Override
        public int compareTo(Data o) {
            return Double.compare(this.weight, o.weight);
        }
    }
public static void main(String[] args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine()," ");

        N = Integer.parseInt(tk.nextToken());
        W = Integer.parseInt(tk.nextToken());

        tk = new StringTokenizer(in.readLine()," ");

        M = Double.parseDouble(tk.nextToken());

        graph = new ArrayList[N + 1];

        for (int i = 0; i <= N; i++){
            graph[i] = new ArrayList<Data>();
        }
        List<Map> map = new ArrayList<>();
        for (int i = 0; i < N; i++){
            tk = new StringTokenizer(in.readLine()," ");
            map.add(new Map(Integer.parseInt(tk.nextToken()),Integer.parseInt(tk.nextToken())));
        }

        for (int i = 1; i <= N; i++){
            for (int j = 1; j <= N; j++) {
                if(i != j){
                    int distX = map.get(i - 1).x - map.get(j - 1).x;
                    int distY = map.get(i - 1).y - map.get(j - 1).y;
                    double dist = Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2));
                    graph[i].add(new Data(j, dist));
                    graph[j].add(new Data(i, dist));
                }
            }
        }
        for (int i = 0; i < W; i++){
            tk = new StringTokenizer(in.readLine()," ");
            int from = Integer.parseInt(tk.nextToken());
            int to = Integer.parseInt(tk.nextToken());
            graph[from].add(new Data(to, 0));
            graph[to].add(new Data(from, 0));
        }

        double res = dijkstra();
        System.out.println((long) (res *1000));
    }

    private static double dijkstra() {
        PriorityQueue<Data> q = new PriorityQueue<>();
        q.offer(new Data(1, 0));

        boolean[] visited = new boolean[N + 1];
        double[] dist = new double[N + 1];
        Arrays.fill(dist, Double.MAX_VALUE);
        dist[1] = 0;

        while (!q.isEmpty()) {
            Data now = q.poll();
            if (visited[now.to]) continue;            
            visited[now.to] = true;

            for (Data next : graph[now.to]) {
                if (!visited[next.to] && dist[next.to] > dist[now.to] + next.weight) {
                    dist[next.to] = dist[now.to] + next.weight;
                    q.offer(new Data(next.to, dist[next.to]));
                }
            }
        }
        return dist[N];
    }
}

 

 

1277번: 발전소 설치

첫 줄에는 발전소의 수 N(1 ≤ N ≤ 1,000)과 현재 남아있는 전선의 수 W(1≤ W ≤ 10,000)가 주어진다. 두 번째 줄에는 제한 길이 M(0.0 < M < 200,000.0)가 주어진다. 다음 N개의 줄에는 1번 발전소부터 N번 발

www.acmicpc.net

 

'Study > Problem Solving' 카테고리의 다른 글

[BOJ_10159] 저울  (0) 2024.02.04
[BOJ_2224] 명제증명  (0) 2024.02.02
[BOJ_14938] 서강그라운드  (1) 2024.01.27
[BOJ_11265] 끝나지 않는 파티  (1) 2024.01.23
[BOJ_11403] 경로 찾기  (0) 2024.01.20