-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighSpeedNetwork.cpp
More file actions
69 lines (51 loc) · 1.18 KB
/
highSpeedNetwork.cpp
File metadata and controls
69 lines (51 loc) · 1.18 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
// Solution to this: https://www.iarcs.org.in/inoi/contests/aug2005/Advanced-2.php
#include <iostream>
#include <vector.h>
#include <algorithm>
#include <string.h>
using namespace std;
vector <int> set;
int find (int ind)
{
int c = set[ind];
if (c == ind) {
return ind;
} else {
set[ind] = find(c);
return set[ind];
};
};
int main ()
{
int n;
cin >> n;
int matr[n][n];
vector<pair<int, pair<int, int>>> pr;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matr[i][j];
}
}
for (int i = 1; i < n; i++) {
for (int j = i+1; j < n; j++) {
pr.push_back(make_pair(matr[i][j], make_pair(i, j)));
}
}
sort(pr.begin(), pr.end());
set = vector<int> (n);
for (int i = 0; i < n; ++i) {
set[i] = i;
}
int weight = 0;
for (pair<int, pair<int, int>> p:pr) {
int x = p.second.first;
int y = p.second.second;
int cost = p.first;
int c1=find(x), c2=find(y);
if (c1 != c2) {
weight = weight + cost;
set[c2] = c1;
};
}
cout << weight << endl;
}