Skip to content

Commit 4abd043

Browse files
authored
Create 2492.Minimum-Score-of-a-Path-Between-Two-Cities.cpp
1 parent d1aeb79 commit 4abd043

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
int Father[100005];
3+
int FindFather(int x)
4+
{
5+
if (Father[x]!=x)
6+
Father[x] = FindFather(Father[x]);
7+
return Father[x];
8+
}
9+
10+
void Union(int x, int y)
11+
{
12+
x = Father[x];
13+
y = Father[y];
14+
if (x<y) Father[y] = x;
15+
else Father[x] = y;
16+
}
17+
18+
public:
19+
int minScore(int n, vector<vector<int>>& roads)
20+
{
21+
for (int i=1; i<=n; i++)
22+
Father[i] = i;
23+
24+
for (auto road: roads)
25+
{
26+
int a = road[0], b = road[1], d = road[2];
27+
if (FindFather(a)!=FindFather(b))
28+
Union(a,b);
29+
}
30+
31+
int ret = INT_MAX;
32+
for (auto road: roads)
33+
{
34+
int a = road[0], b = road[1], d = road[2];
35+
if (FindFather(a)==FindFather(1))
36+
ret = min(ret, d);
37+
}
38+
39+
return ret;
40+
}
41+
};

0 commit comments

Comments
 (0)