-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1251_2.java
More file actions
124 lines (97 loc) · 2.51 KB
/
Solution1251_2.java
File metadata and controls
124 lines (97 loc) · 2.51 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
116
117
118
119
120
121
122
123
124
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Solution1251_2 {
public static class pair{
int x;
int y;
public pair(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
public static class Edge implements Comparable<Edge>{
int a;
int b;
double val;
public Edge(int a, int b, double val) {
super();
this.a = a;
this.b = b;
this.val = val;
}
@Override
public int compareTo(Edge o) {
return (this.val >= o.val)? 1:-1;
}
}
static int[] p;
static public double Dist(pair a, pair b) {
int x = Math.abs(a.x-b.x);
int y = Math.abs(a.y-b.y);
return x*x+y*y;
}
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int TC = Integer.parseInt(bf.readLine());
for(int tc=1;tc<=TC;tc++) {
int N = Integer.parseInt(bf.readLine()); //¼¶ÀÇ °³¼ö
StringTokenizer xx = new StringTokenizer(bf.readLine()," ");
StringTokenizer yy = new StringTokenizer(bf.readLine()," ");
double E = Double.parseDouble(bf.readLine()); //¼¼À²
pair isl[] = new pair[N];
p = new int[N];
for(int i=0;i<N;i++) {
isl[i] = new pair(Integer.parseInt(xx.nextToken()),Integer.parseInt(yy.nextToken()));
makeSet(i);
}
Edge[] ed = new Edge[N*(N-1)/2];
// LinkedList<Edge> ed = new LinkedList<Solution1251_2.Edge>();
int idx = 0;
for(int i=0;i<N;i++) {
pair p1 = isl[i];
for(int j=i+1;j<N;j++) {
if(i==j) continue;
pair p2 = isl[j];
System.out.println(Dist(p1,p2));
ed[idx++]=new Edge(i,j,Dist(p1,p2));
}
}
Arrays.sort(ed);
double sum=0;
for(int i=0;i<ed.length;i++) {
Edge e = ed[i];
if(findSet(e.a) != findSet(e.b)) {
union(e.a,e.b);
sum+=E*e.val;
}
}
System.out.println(tc+" "+Math.round(sum));
} // end of tc
} //end of main
public static void makeSet(int x) {
p[x]=x;
}
public static int findSet(int x) {
if(p[x]==x) {
return x;
} else {
p[x]=findSet(p[x]);
return p[x];
}
}
public static void union(int x,int y) {
int px = findSet(x);
int py = findSet(y);
if(px!=py) {
p[px]=py;
}
}
}