-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11733.cpp
More file actions
90 lines (81 loc) · 1.37 KB
/
11733.cpp
File metadata and controls
90 lines (81 loc) · 1.37 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
79
80
81
82
83
84
85
86
87
88
89
90
#include<bits/stdc++.h>
using namespace std;
struct edge{
int x;
int y;
int w;
edge(int a,int b,int c){
x = a;
y = b;
w = c;
}
bool operator < (const edge& p) const{
return this->w < p.w;
}
};
int numOfVertices;
int numOfEdges;
int airportCost;
vector<edge>edges;
map<int,int>par;
vector<int>costs;
void clear(){
edges.clear();
par.clear();
costs.clear();
}
void makeSet(int x){
par[x] =x;
}
int Find(int x){
if(par[x] == x){
return x;
}
par[x] = Find(par[x]);
return par[x];
}
void Union(int a,int b){
int x = Find(a);
int y = Find(b);
if(x != y){
par[y] = x;
}
}
int main(){
int tt;
cin>>tt;
int cnt = 0;
while(tt--){
cin>>numOfVertices>>numOfEdges>>airportCost;
int ne = numOfEdges;
while(ne--){
int x,y,z;
cin>>x>>y>>z;
if(z < airportCost){
edges.push_back(edge(x,y,z));
}
}
for(int i = 1;i<=numOfVertices;i++){
makeSet(i);
}
int cunt = 0;
int s = 0;
sort(edges.begin(),edges.end()); // it prevents repetation sweet khushkal ....
for(int j = 0;j<(int)edges.size();j++){
int u = Find(edges[j].x);
int v= Find(edges[j].y);
if(u != v){
par[u] = v;
cunt++;
s += edges[j].w;
if(cunt == numOfVertices - 1){
break;
}
}
}
int air = numOfVertices - cunt;//Number of disjoint sets representatives
s += air*airportCost;
printf("Case #%d: %d %d\n", ++cnt, s, air);
clear();
}
}