-
Notifications
You must be signed in to change notification settings - Fork 103
/
BLINNET.cpp
55 lines (50 loc) · 1013 Bytes
/
BLINNET.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
/*
USER: zobayer
TASK: BLINNET
ALGO: kruskal
*/
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair< int, int > pii;
typedef pair< int, pii > edge;
const int MAX = 10001;
vector< edge > edges;
int parent[MAX];
int find(int u) {
if(u != parent[u]) parent[u] = find(parent[u]);
return parent[u];
}
int main() {
int t, i, n, e, u, v, w, cost, sz;
char dump[20];
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d", &t);
while(t--) {
edges.clear();
scanf("%d", &n);
for(i = 1; i <= n; i++) {
parent[i] = i;
scanf("%s%d", dump, &e);
while(e--) {
scanf("%d%d", &v, &w);
if(v > i) edges.push_back(edge(w, pii(i, v)));
}
}
sort(edges.begin(), edges.end());
sz = edges.size();
for(cost=i=0; i < sz; i++) {
u = find(edges[i].second.first);
v = find(edges[i].second.second);
w = edges[i].first;
if(u != v) {
cost += w;
parent[u] = parent[v];
}
}
printf("%d\n", cost);
}
return 0;
}