-
Notifications
You must be signed in to change notification settings - Fork 1
/
UVa 11733 - Airports.cpp
99 lines (84 loc) · 2.23 KB
/
UVa 11733 - Airports.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
//Question link: https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=2833
#include <iostream>
#include <vector>
using namespace std;
typedef struct{
int v1, w, v2; //Vertice 1 -- weight -- Vertice 2
} Edge;
int f(vector<int> &v, int curr){ //Find
if(v[curr] == curr){
return v[curr];
}
return v[curr] = f(v, v[curr]);
}
bool u(vector<int> &v, int a, int b){ //Union
int root1 = f(v, a), root2 = f(v, b);
if(root1 == root2){
return false;
}
v[root2] = root1;
return true;
}
int HoarePartition(Edge *vet, int l, int r){
Edge p = vet[l], temp;
int i = l, j = r+1;
do{
do{
i = i+1;
}while((vet[i].w < p.w) && (i < r));
do{
j = j-1;
}while(vet[j].w > p.w);
swap(vet[i],vet[j]);
}while(i < j);
swap(vet[i],vet[j]);
swap(vet[j],vet[l]);
return j;
}
void quickSort(Edge *vet, int l, int r){
int s;
if(l < r){
s = HoarePartition(vet, l, r);
quickSort(vet, l, s-1);
quickSort(vet, s+1, r);
}
}
int main(){
int testCases, vertices, edges, airportCost, airports, totalCost, c = 1;
vector<int> v;
Edge *e;
scanf("%d", &testCases);
while(testCases--){
scanf("%d %d %d", &vertices, &edges, &airportCost);
e = (Edge*) malloc(sizeof(Edge)*edges);
v.resize(vertices+1, -1);
airports = totalCost = 0;
for(int i = 0; i < edges; i++){
scanf("%d %d %d", &e[i].v1, &e[i].v2, &e[i].w);
}
for(int i = 1; i <= vertices; i++){
v[i] = i;
}
quickSort(e, 0, edges-1);
for(int i = 0; i < edges; i++){
if(u(v, e[i].v1, e[i].v2)){
if(airportCost > e[i].w){
totalCost += e[i].w;
}else{
airports++;
totalCost += airportCost;
}
}
}
for(int i = 1; i <= vertices; i++){
if(v[i] == i){
airports++;
totalCost += airportCost;
}
}
printf("Case #%d: %d %d\n", c, totalCost, airports);
v.clear();
c++;
}
return 0;
}