-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVA-11733.cpp
More file actions
82 lines (62 loc) · 1.32 KB
/
UVA-11733.cpp
File metadata and controls
82 lines (62 loc) · 1.32 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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<int> vi;
//const int max = 1e8;
vector<pair<int,pii>> edges;
vi ancestral;
vi indexes;
int ans = 0;
int airports = 0;
int t, n, m, a;
int x, y, c;
int first;
int find(int x){
if(x == ancestral[x]) return x;
return ancestral[x] = find(ancestral[ancestral[x]]);
}
void unite(int x, int y){
int px = find(x);
int py = find(y);
ancestral[px] = py;
}
void kruskal(int k){
long long aeroportos = n;
long long custo = 0;
sort(edges.begin(), edges.end());
for(int i = 0; i < edges.size(); i++){
if (edges[i].first >= a) continue;
int from = edges[i].second.first;
int to = edges[i].second.second;
if(find(from) != find(to)){
unite(from, to);
aeroportos--;
custo += edges[i].first;
}
}
cout << "Case #" << k+1 << ": " << custo + aeroportos * a << " " << aeroportos << endl;
}
int main (){
ios::sync_with_stdio(0); cin.tie(nullptr);
cin >> t;
for(int k = 0; k < t; k++){
first = 1;
ans = 0;
airports = 0;
edges.clear();
ancestral.clear();
indexes.clear();
cin >> n >> m >> a;
ancestral.resize(n+1);
for(int i = 0; i < n; i++){
ancestral[i] = i;
}
for(int i = 0; i < m; i++){
cin >> x >> y >> c;
edges.push_back({c, {x-1, y-1}});
}
kruskal(k);
}
return 0;
}