-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmain.cpp
61 lines (51 loc) · 1.19 KB
/
main.cpp
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
// Authored by : tony9402
// Co-authored by : -
// Link : http://boj.kr/a20ee766193d402682304dd53ce97459
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node{
ll w;
int a, b;
Node(ll w=0, int a=0, int b=0):w(w),a(a),b(b){}
bool operator<(const Node& o) const { return w < o.w; }
};
vector<Node> v;
int uf[1005], group[1005];
int find(int x) {
if(uf[x] < 0)return x;
return uf[x] = find(uf[x]);
}
bool merge(int a, int b){
a = find(a);
b = find(b);
if(a == b)return false;
uf[b] = a;
return true;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
// Initial
for(int i=0;i<1005;i++) uf[i] = -1;
int N, M; cin >> N >> M;
for(int i=1;i<=N;i++) {
char x;cin >> x;
group[i] = x == 'M';
}
for(int i=0;i<M;i++){
int a, b, c; cin >> a >> b >> c;
if(group[a] == group[b])continue;
v.emplace_back(c, a, b);
}
sort(v.begin(), v.end());
ll ans = 0;
for(int i=0;i<(int)v.size();i++){
if(merge(v[i].a, v[i].b)) ans += v[i].w;
}
for(int i=1;i<=N;i++) {
if(find(1) != find(i)) ans = -1;
}
cout << ans;
return 0;
}