POJ-2395 Out of Hay---MST最大边
题目链接:
https://vjudge.net/problem/POJ-2395
题目大意:
求MST中的最大边,和POJ-2495类似
思路:
模板直接过
1 #include<iostream> 2 #include<vector> 3 #include<queue> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdio> 7 #include<set> 8 #include<map> 9 #include<cmath> 10 using namespace std; 11 typedef pair<int, int> Pair; 12 typedef long long ll; 13 const int INF = 0x3f3f3f3f; 14 int T, n, m; 15 const int maxn = 2e4 + 10; 16 struct edge 17 { 18 int v, u, w; 19 bool operator < (const edge a)const 20 { 21 return w < a.w; 22 } 23 }; 24 edge e[maxn]; 25 int pa[maxn]; 26 int Find(int x) 27 { 28 return x == pa[x] ? x : pa[x] = Find(pa[x]);//路径压缩 29 } 30 void kruskal() 31 { 32 for(int i = 1; i <= n; i++)pa[i] = i; 33 sort(e, e + m); 34 int ans = 0; 35 for(int i = 0; i < m; i++) 36 { 37 int v = e[i].v, u = e[i].u, w = e[i].w; 38 int x = Find(v), y = Find(u); 39 if(x != y) 40 { 41 pa[x] = y; 42 ans = max(ans, w); 43 } 44 } 45 cout<<ans<<endl; 46 } 47 int main() 48 { 49 cin >> n >> m; 50 for(int i = 0; i < m; i++)cin >> e[i].v >> e[i].u >> e[i].w; 51 kruskal(); 52 }
越努力,越幸运