-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11085.cpp
More file actions
77 lines (62 loc) · 1.38 KB
/
11085.cpp
File metadata and controls
77 lines (62 loc) · 1.38 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
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
typedef struct {
int start, end, width;
} road;
bool roadsort (road r1, road r2) {
return r1.width < r2.width;
}
int* par;
int* sz;
int find (int x);
void merge (int a, int b);
int main () {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int p, w;
cin >> p >> w;
int c, v;
cin >> c >> v;
par = new int[p];
sz = new int[p];
for (int i = 0; i < p; i++) {
par[i] = i;
sz[i] = 1;
}
vector<road> roads;
road temproad;
int temp1, temp2, temp3;
for (int i = 0; i < w; i++) {
cin >> temp1 >> temp2 >> temp3;
temproad.start = temp1;
temproad.end = temp2;
temproad.width = temp3;
roads.push_back(temproad);
}
sort(roads.begin(), roads.end(), roadsort);
int minwidth = 0;
for (int i = w - 1; i >= 0; i--) {
merge(roads[i].start, roads[i].end);
if (find(c) == find(v)) {
minwidth = roads[i].width;
break;
}
}
cout << minwidth;
}
int find (int x) {
if (x == par[x])
return x;
return par[x] = find(par[x]);
}
void merge (int a, int b) {
a = find(a); b = find(b);
if (a == b)
return;
if (sz[a] < sz[b]) swap(a, b);
par[b] = a;
sz[a] += sz[b];
}