You will be fine

<Algorithm> 130. 하나로(SWExpert)

by BFine
반응형

 

1. 하나로(SWExpert)

  • 우선순위 큐를 이용하는 Prim 알고리즘 문제
  • 주어진 조건이 특이하게 x좌표와 y좌표를 다른 줄에 배치해서 시간이 걸렸던 문제 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.stream.IntStream;
import static java.lang.Math.*;
 
public class Solution {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        /******************************
         * bfs를 이용하여 가중치를 비교하여
         * 더 최단거리의 가중치 일 경우
         * 값을 바꿔주는 Prim 알고리즘을
         * 이용하여 최소 스패닝 트리를 구한다.
         *******************************/
        for(int t = 1; t <= T ; t++) {
            
            int n = sc.nextInt();
 
            List<Integer> list = new LinkedList<>();
            int arr[][] = new int[n][n];
            Node[] nodes = new Node[n];
            
            for(int i = 0; i < n*2; i++) {
                int a = sc.nextInt();
                list.add(a);
            }
            
            for(int i = 0; i < n; i++) {
                nodes[i] = new Node(list.get(i),list.get(i+n));
            }
            List<Edge>[] map = new ArrayList[n];            
            IntStream.range(0, n).forEach(i->map[i]=new ArrayList<>());
            double e = sc.nextDouble();
            dist = new double[n];
            visited = new boolean[n];
            Arrays.fill(dist, Double.MAX_VALUE);
             
            for(int i = 0; i < n; i++) {
                Node a = nodes[i];
                for(int j=0; j < n; j++) {
                    if(i==j) continue;
                    Node b = nodes[j];
                    double dx = pow(abs(a.x-b.x),2);
                    double dy = pow(abs(a.y-b.y),2);
                    double len = sqrt(dx+dy);
                    double w = e*pow(len,2);
                    map[i].add(new Edge(j, w));
                }
            }
            
            dist[0= 0;
            prim(map);
            
             double ans = 0;
             
                for(int i = 0; i < n ; i++) {
                    ans += dist[i];
                }
                System.out.println("#"+t+" "+round(ans));
        }
    }
    static double dist[];
    static boolean visited[];
    public static void prim(List<Edge>[] map) {
        PriorityQueue<Edge> pq = new PriorityQueue<>(new Comparator<Edge>() {
            @Override
            public int compare(Edge o1, Edge o2) {
                return Double.compare(o1.w, o2.w);
            }
        });
        
        pq.add(new Edge(0, dist[0]));
        
        while (!pq.isEmpty()) {
            Edge eg = pq.poll();
            int v = eg.y;
            
            if(visited[v])continue;
            visited[v] = true;
            
            for(Edge e : map[v]) {
                if(!visited[e.y]) {
                    int destination = e.y;
                    double weight = e.w;
                    if(weight < dist[destination]) {
                        dist[destination] = weight;
                        pq.add(new Edge(destination, dist[destination]));
                    }
                }
            }
            
        }
        
    }
    
    static class Node{
        int x,y;
 
        public Node(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }
    }
    static class Edge{
        int y;
        double w;
        public Edge(int y, double w) {
            super();
            this.y = y;
            this.w = w;
        }
    
    }
}
 
 
cs

참고 & 출처 

 

반응형

블로그의 정보

57개월 BackEnd

BFine

활동하기