-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
72 lines (58 loc) Β· 1.97 KB
/
Main.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
package Graph.P14621;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static int N, M, u, v, d;
static char[] gender;
static int[] root;
static PriorityQueue<Node> pq = new PriorityQueue<Node>(((o1, o2) -> o1.d - o2.d));
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Graph/P14621/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
gender = new char[N+1];
root = new int[N+1];
st = new StringTokenizer(br.readLine());
for (int i = 1; i <= N; i++) {
gender[i] = st.nextToken().charAt(0);
root[i] = i;
}
while (M-- > 0) {
st = new StringTokenizer(br.readLine());
u = Integer.parseInt(st.nextToken());
v = Integer.parseInt(st.nextToken());
d = Integer.parseInt(st.nextToken());
if (gender[u] != gender[v]) pq.offer(new Node(u, v, d));
}
int dist = 0, cnt = 0;
while (!pq.isEmpty()) {
Node cur = pq.poll();
if (find(cur.fr) == find(cur.to)) continue;
merge(cur.fr, cur.to);
dist += cur.d;
cnt ++;
}
if (cnt == N-1) System.out.println(dist);
else System.out.println(-1);
}
static int find(int n) {
if (root[n] == n) return n;
return root[n] = find(root[n]);
}
static void merge(int a, int b) {
root[find(a)] = find(b);
}
static class Node {
int fr, to, d;
public Node(int fr, int to, int d) {
this.fr = fr;
this.to = to;
this.d = d;
}
}
}