-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1174.cpp
More file actions
78 lines (74 loc) · 1.78 KB
/
1174.cpp
File metadata and controls
78 lines (74 loc) · 1.78 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
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
#define inf 10000000
#define mem(a, b) memset(a, b, sizeof(a))
#define NN 2010
int prnt[NN + 7];
map<string, int> mp;
string s, ss;
char a[20];
char b[20];
struct data {
int u, v, w;
};
vector<data> e;
bool comp(data n, data m) { return n.w < m.w; }
int root(int n) {
if (prnt[n] == n) return n;
return root(prnt[n]);
}
int mst(int n) {
sort(e.begin(), e.end(), comp);
int i, j, k, node, dist;
for (i = 0; i <= n; i++) prnt[i] = i;
node = dist = 0;
for (i = 0; i < e.size() && node < n - 1; i++) {
int u = root(e[i].v);
int v = root(e[i].u);
if (u != v) {
prnt[u] = v;
node++;
dist += e[i].w;
}
}
return dist;
}
int main() {
// freopen("C:\\Users\\Maruf Tuhin\\Desktop\\in.txt","r",stdin);
// ios_base::sync_with_stdio(false);
int i, j, k, l, n, r, c;
int tc, t = 1;
scanf("%d", &tc);
while (tc--) {
scanf("%d%d", &n, &r);
k = 0;
for (i = 0; i < r; i++) {
scanf("%s%s%d", &a, &b, &l);
s.assign(a);
ss.assign(b);
if (mp.find(s) == mp.end()) mp[s] = k++;
if (mp.find(ss) == mp.end()) mp[ss] = k++;
data d;
d.u = mp[s];
d.v = mp[ss];
d.w = l;
e.push_back(d);
}
int ret = mst(n);
if (t != 1) printf("\n");
t++;
printf("%d\n", ret);
mp.clear();
e.clear();
}
return 0;
}