-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCobbledStreets.cpp
106 lines (90 loc) · 2.35 KB
/
CobbledStreets.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct Edge { long long start, end, wt; } Edge;
typedef struct Subset { long long parent, rank; } Subset;
class DisjointSet {
vector<Subset> subsets;
public:
DisjointSet(long long V) {
for(long long i = 0; i < V; i++) {
Subset aSubset;
aSubset.parent = i;
aSubset.rank = 0;
subsets.push_back(aSubset);
}
}
void Union(long long x, long long y) {
long long xRt = Find(x);
long long yRt = Find(y);
if(subsets[xRt].rank < subsets[yRt].rank)
subsets[xRt].parent = yRt;
else if(subsets[xRt].rank > subsets[yRt].rank)
subsets[yRt].parent = xRt;
else {
subsets[yRt].parent = xRt;
subsets[xRt].rank++;
}
}
long long Find(long long x) {
if(subsets[x].parent != x)
subsets[x].parent = Find(subsets[x].parent);
return subsets[x].parent;
}
};
class Graph {
long long vertices;
vector<Edge> edges;
public:
Graph(long long V) : vertices(V) { }
void addEdge(long long start, long long end, long long wt) {
Edge aEdge;
aEdge.start = start;
aEdge.end = end;
aEdge.wt = wt;
edges.push_back(aEdge);
}
long long getNodes() const { return vertices; }
long long getEdges() const { return edges.size(); }
vector<Edge> getEdgesArr() { return edges; }
};
long long mstWt(Graph& gg);
bool myComp(Edge a1, Edge a2) { return a1.wt < a2.wt; }
int main() {
long long T;
cin >> T;
while(T--) {
long long price, N, E;
cin >> price >> N >> E;
Graph gg(N);
for(long long i = 0; i < E; i++) {
long long start, end, edgeWt;
cin >> start >> end >> edgeWt;
gg.addEdge(start-1, end-1, edgeWt);
}
long long ans = price * mstWt(gg);
cout << ans << endl;
}
return 0;
}
long long mstWt(Graph& gg) {
const long long V = gg.getNodes();
long long minWt = 0;
vector<Edge> edges = gg.getEdgesArr();
sort(edges.begin(), edges.end(), myComp);
DisjointSet ds(V);
long long nEdges = 0;
for(long long i = 0; nEdges < V-1; ) {
Edge nextEdge = edges[i++];
long long xSubset = ds.Find(nextEdge.start);
long long ySubset = ds.Find(nextEdge.end);
if(xSubset != ySubset) { // adding this edge won't cause cycle
minWt += nextEdge.wt;
nEdges++;
ds.Union(xSubset, ySubset);
}
}
return minWt;
}