Skip to content

Commit 6200467

Browse files
authored
Merge pull request #519 from jeeneee/woojin
중량제한
2 parents 1c5e782 + 77eb4ef commit 6200467

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
struct Edge {
8+
int a, b, c;
9+
Edge(int a, int b, int c) : a(a), b(b), c(c) {}
10+
bool operator<(const Edge& rhs) const {
11+
return c > rhs.c;
12+
}
13+
};
14+
vector<int> parent;
15+
vector<Edge> edges;
16+
int n, m, src, dest;
17+
18+
void input() {
19+
ios::sync_with_stdio(0);
20+
cin.tie(0);
21+
22+
cin >> n >> m;
23+
for (int i = 0; i < m; i++) {
24+
int a, b, c;
25+
cin >> a >> b >> c;
26+
edges.push_back(Edge(a, b, c));
27+
}
28+
cin >> src >> dest;
29+
}
30+
31+
int find(int u) {
32+
if (u == parent[u]) return u;
33+
return parent[u] = find(parent[u]);
34+
}
35+
36+
void merge(int u, int v) {
37+
u = find(u);
38+
v = find(v);
39+
parent[u] = v;
40+
}
41+
42+
int calc() {
43+
parent.resize(n + 1);
44+
for (int i = 1; i <= n; i++) parent[i] = i;
45+
46+
sort(edges.begin(), edges.end());
47+
for (auto &e : edges) {
48+
merge(e.a, e.b);
49+
if (find(src) == find(dest))
50+
return e.c;
51+
}
52+
return -1;
53+
}
54+
55+
int main() {
56+
input();
57+
cout << calc() << endl;
58+
59+
return 0;
60+
}

0 commit comments

Comments
 (0)