1395-slim Span (minimum spanning tree)

Source: Internet
Author: User

The problem with UVA is to make people bright, different from the naked tree water problem, the problem is slightly changed, not to seek the smallest spanning tree, but to find the most slender spanning tree.

Because there are many spanning trees, the difference between the maximum and minimum edges of each spanning tree is uncertain. So you can only enumerate all spanning trees.

Applying the minimum spanning tree template, we can enumerate the start position of the spanning tree, then push the end position backwards, and when n points are all connected, the edge set of the spanning tree is [l,r]. Because the edge is pre-ordered, then the slender value of the tree is E[r]-e[l].

So from small to large enumeration of all L, constantly update the answer, it is possible.

Can't help but say it again and check set, this is a very powerful data structure, its efficiency is very high, in the sense of the same, the time complexity of the find function can almost be regarded as a constant, and union is obviously a constant time.

The highlight of this is the short path compression (find function), which also uses a rank function for further optimization, but for a time-intensive game, it can be omitted and only a small amount of code is written.

See the code for details:

#include <bits/stdc++.h>using namespace Std;const int maxn = + 5;const int INF = 1000000000;const int MAXM = MAXN * (maxn-1)/2;int n,m,p[maxn];struct node{int a,b,v;} E[maxm];bool CMP (node A,node b) {return A.V < B.V;} int find (int x) {return p[x] = = x X:p[x] = find (P[x]);}    int solve () {int ans = INF; for (int l=0; l<m;        l++) {for (int i=1;i<=n;i++) p[i] = i;//Initialize and check set int cur = 1; for (int r=l; r<m;            r++) {int x = find (e[r].a), y = find (e[r].b);            if (x! = y) {p[x] = y; cur++;}            if (cur = = N) {ans = min (ans,e[r].v-e[l].v); }} if (cur! = n) break;    If the starting point L does not meet the requirements, then the next also does not meet. } return ans;        int main () {while (~scanf ("%d%d", &n,&m)) {if (!n &&!m) return 0;        for (int i=0;i<m;i++) scanf ("%d%d%d", &AMP;E[I].A,&AMP;E[I].B,&AMP;E[I].V);        Sort (e,e+m,cmp);        int ans = solve (); if (ans = = INF) printf (" -1\n");    else printf ("%d\n", ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

1395-slim Span (minimum spanning tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.