-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS1250.java
More file actions
115 lines (83 loc) · 2.7 KB
/
S1250.java
File metadata and controls
115 lines (83 loc) · 2.7 KB
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
import java.io.*;
import java.util.*;
class Edge implements Comparable<Edge>{
public int end;
public double wight;
public Edge(int end, double wight) {
this.end = end;
this.wight = wight;
}
@Override
public int compareTo(Edge o) {
if(this.wight < o.wight)
return -1;
else if (this.wight == o.wight) return 0;
else return 1;
}
}// 시작 섬, 끝 섬, 길이 저장용 클래스
public class S1250 {
static double E; // 환경 부담금
static long[] arrX; // x좌표 저장
static long[] arrY; // y좌표 저장
static boolean[] visited; // 유니온용 배열
static long dis;
static ArrayList<Edge>[] list;
static int n;
static double w;
public static void main(String[] args) throws IOException{
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(buffer.readLine());
int t = 0;
while(T>t) {
n = Integer.parseInt(buffer.readLine());
arrX = new long[n];
arrY = new long[n];
String[] input = buffer.readLine().split(" ");
for(int i = 0; i <n; i++){
arrX[i] = Long.parseLong(input[i]);
}
input = buffer.readLine().split(" ");
for(int i = 0; i <n; i++){
arrY[i] = Long.parseLong(input[i]);
}
E = Double.parseDouble(buffer.readLine());
list = new ArrayList[n];
setList();
//입력 -완-
visited = new boolean[n];
prim(1);
t++;
System.out.println("#" + t + " " + Math.round(w*E));
}
}
private static void setList() {
for (int i = 0; i < n; i++) {
list[i] = new ArrayList<>();
}
for(int i = 0; i <n; i++ ){
for (int j = 0; j < n; j++) {
if(i == j) continue;
long dis = (arrX[i]-arrX[j])*(arrX[i]-arrX[j]) + (arrY[i]-arrY[j]) * (arrY[i]-arrY[j]);
list[i].add(new Edge(j,dis));
// list[j].add(new Edge(i,dis));
}
}
}
private static void prim(int start) {
PriorityQueue<Edge> pq = new PriorityQueue<>();
pq.offer(new Edge(start,0));
w = 0;
while(!pq.isEmpty()){
Edge dummy= pq.poll();
if(!visited[dummy.end]) {
visited[dummy.end] = true;
w += dummy.wight;
for (Edge edge : list[dummy.end]) {
if (!visited[edge.end]) {
pq.offer(edge);
}
}
}
}
}
}