-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliens.cpp
111 lines (86 loc) · 2.09 KB
/
aliens.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
107
108
109
110
111
#include <limits.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <bitset>
//#include <unordered_map>
using namespace std;
typedef pair<int, int> ii;
#define traverse(c, it) \
for (vector<pair<ii, int> >::iterator it = c.begin(); it != c.end(); it++)
int T[100010];
void makeSet(int N) {
memset(T, 0, sizeof(T));
for(int i = 0; i < N; i++) {
T[i] = i;
}
}
int findSet(int id) {
if(id != T[id]) {
T[id] = findSet(T[id]);
}
return T[id];
}
void unionSet(int n1, int n2) {
T[findSet(n1)] = findSet(n2);
}
int N, M, K;
vector<pair<ii, int> > edgeList;
bool cmp(pair<ii, int> p1, pair<ii, int> p2) {
if(p1.second < p2.second)
return true;
return false;
}
int kruskal() {
makeSet(N);
int mstCost = 0;
int numSets = N;
traverse(edgeList, it) {
pair<ii, int> current = *it;
int start = current.first.first;
int end = current.first.second;
if(numSets == K) {
break;
}
if(findSet(start) != findSet(end)) {
mstCost += current.second;
unionSet(start, end);
numSets--;
}
}
return mstCost;
}
int main() {
cin >> N >> M >> K;
//cout << N << " " << M << " " << K << endl;
edgeList.clear();
int start, end, cost;
for(int i = 0; i < M; i++) {
cin >> start >> end >> cost;
//start--; end--;
//cout << start << endl;
//cost--;
if(start != end) {
edgeList.push_back(make_pair(make_pair(start, end), cost));
edgeList.push_back(make_pair(make_pair(end, start), cost));
}
}
//cout << "ok" << endl;
//traverse(edgeList, it) {
// cout << (*it).first.first << endl;
//}
sort(edgeList.begin(), edgeList.end(), cmp);
//traverse(edgeList, it) {
// cout << (*it).first.first << endl;
//}
//cout << "ok" << endl;
cout << kruskal() << endl;
return 0;
}