forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3036 - IP-TV.cpp
79 lines (58 loc) · 1.43 KB
/
3036 - IP-TV.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <cstdio>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
struct edge{
int u,v,w;
edge(){}
bool operator < (edge X)const{
return w < X.w;
}
}L[50000];
int parent[2000],rank[2000];
void init(int V){
for(int i = 0;i < V;++i){
parent[i] = i;
rank[i] = 0;
}
}
int Find(int v){
if(parent[v] != v) parent[v] = Find(parent[v]);
return parent[v];
}
void Union(int u, int v){
int PU = Find(u),PV = Find(v);
if(rank[PU] > rank[PV]) parent[PV] = PU;
else{
parent[PU] = PV;
if(rank[PU] == rank[PV]) ++rank[PV];
}
}
int main(){
int T,V,E;
char name1[9],name2[9];
map<string,int> id;
scanf("%d",&T);
while(T--){
scanf("%d %d",&V,&E);
id.clear();
for(int i = 0,j = 0;i < E;++i){
scanf("%s %s %d",name1,name2,&L[i].w);
if(id.find(name1) == id.end()) id[name1] = j++;
if(id.find(name2) == id.end()) id[name2] = j++;
L[i].u = id[name1]; L[i].v = id[name2];
}
sort(L,L + E);
init(V);
int ans = 0;
for(int i = 0;i < E;++i){
if(Find(L[i].u) != Find(L[i].v)){
Union(L[i].u,L[i].v);
ans += L[i].w;
}
}
printf("%d\n",ans);
}
return 0;
}