-
Notifications
You must be signed in to change notification settings - Fork 1
/
2003 S5 - Trucking Troubles.cpp
141 lines (105 loc) · 2.79 KB
/
2003 S5 - Trucking Troubles.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
2003 S5 - Trucking Troubles
Difficulty: Easy/Medium
This problem is like textbook level Kruskal's
Quite literaly, all you have to do is just use kruskal's
But instead of sorting least to greatest weight
You do greatest to least since you're trying to get maximum weight
Note that you also don't need to construct a full tree, you just need enough to connect all the destinations
*/
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <string.h>
int parent [100001];
int size [100001];
//Sort greatest to least
struct reverseSort{
bool operator()(std::vector<int> const& a, std::vector<int> const& b){
return a[0] > b[0];
}
};
//Union Find
int root(int city){
if (parent[city] == city){
return city;
}
else{
return root(parent[city]);
}
}
void Union (int x, int y){
int parX = root(x);
int parY = root(y);
if (size[parX] > size[parY]){
parent[parY] = parX;
size[parX] += size[parY];
}
else{
parent[parX] = parY;
size[parY] += size[parX];
}
}
int find(int x, int y){
int parX = root(x);
int parY = root(y);
if (parX == parY){
return true;
}
else{
return false;
}
}
int main(){
int c, r, d;
std::cin >> c >> r >> d;
int required [10001]; //Keep track of what cities are required
std::vector<std::vector<int>> roads; //2-D vector holding roads
for (int i = 0; i < r; i++){
int x, y, w;
std::cin >> x >> y >> w;
roads.push_back({w, x, y}); //road is a vector where {weight, city x, city y}
}
memset(required, 0, sizeof(required)); //default required
memset(size, 1, sizeof(size)); //default size
//init parent array
for (int i = 1; i <= 100000; i++){
parent[i] = i;
}
//Get required cities
int total = 1;
required[1] = 1;
for (int i = 0; i < d; i++){
int city;
std::cin >> city;
required[city] = 1;
total++;
}
//Sort edges
std::sort(roads.begin(), roads.end(), reverseSort());
int max = 2e7; //answer variable
for (auto road: roads){
//If we've connected all the required cities
if (total == 0){
break;
}
//No cycle
if (!find(road[1], road[2])){
Union(road[1], road[2]);
//If a required city, subtract from the total, mark as found
if (required[road[1]]){
total--;
required[road[1]] = 0;
}
if (required[road[2]]){
total--;
required[road[2]] = 0;
}
//Update max
max = std::min(max, road[0]);
}
}
std::cout << max << '\n';
return 0;
}