-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsp14124.java
72 lines (60 loc) · 1.48 KB
/
Csp14124.java
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
import java.util.*;
public class Csp14124 {
static PriorityQueue<Edge> list = new PriorityQueue<Edge>(200001);// 所有的边
static int pre[];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();// 路口数
int m = sc.nextInt();// 道路数
int a, b, c;
for (int i = 0; i < m; i++) {
list.add(new Edge(sc.nextInt(), sc.nextInt(), sc.nextInt()));
}
pre = new int[n + 1];
for (int i = 0; i < pre.length; i++) {
pre[i] = i;
}
int cost=0;
Edge edge = new Edge(0, 0, 0);
while (!list.isEmpty()) {
edge = list.poll();
int s = find(edge.start);//这里一定要
int e = find(edge.end);
// System.out.println(edge.start+" "+edge.end+" "+edge.len);
if(s!=e){
if (s < e) {
pre[e] = s;
} else {
pre[s] = e;
}
cost+=edge.len;
}
}
System.out.println(cost);
}
public static int find(int x) {// 并查集 find[i]相同就代表是同一个子树
int fx = x;
while(pre[fx] != fx){
fx = pre[fx];
}
int i = x,j;
while(pre[i]!=fx){
j = pre[i];
pre[i] = fx;
i = j;
}
return fx;
}
}
class Edge implements Comparable<Edge> {
int start, end, len;
public Edge(int s, int e, int l) {
start = s;
end = e;
len = l;
}
@Override
public int compareTo(Edge o) {
return this.len - o.len;
}
}