forked from coderfreestyle/ACM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworking.cpp
More file actions
86 lines (75 loc) · 1.37 KB
/
Networking.cpp
File metadata and controls
86 lines (75 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
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
int set[60];
int myRank[60];
struct Edge
{
int from, to;
int length;
Edge() {}
Edge(int from, int to, int length) {
this->from = from;
this->to = to;
this->length = length;
}
bool operator < (const Edge& c) const {
return length < c.length;
}
};
int myFind(int x) {
if (x != set[x]) {
set[x] = myFind(set[x]);
return set[x];
}
else
return set[x];
}
void link(int x, int y) {
if (myRank[x] > myRank[y]) {
set[y] = x;
}
else {
set[x] = y;
if (myRank[x] == myRank[y])
myRank[y]++;
}
}
void merge(int x, int y) {
link(myFind(x), myFind(y));
}
int main() {
int n, numOfRoutes;
cin >> n;
while (n != 0) {
Edge edges[60 * 60];
cin >> numOfRoutes;
int tmp_i, tmp_j, tmpEdges = 0;
for (int i = 1; i<=n; i++) {
set[i] = i;
myRank[i] = 1;
}
for (int i = 0; i<numOfRoutes; i++) {
scanf("%d%d%d", &tmp_i, &tmp_j, &tmpEdges);
Edge edge_tmp(tmp_i, tmp_j, tmpEdges);
edges[i] = edge_tmp;
}
sort(edges, edges + numOfRoutes);
int cnt = 0;
for (int i = 0; i<numOfRoutes; i++) {
int setA = myFind(edges[i].from);
int setB = myFind(edges[i].to);
if (setA != setB) {
cnt += edges[i].length;
merge(edges[i].from, edges[i].to);
}
}
cout << cnt << endl;
cin >> n;
}
return 0;
}