UVa 1395 Slim Span (minimum spanning tree)

Source: Internet
Author: User

Test instructions: Given the graph of n nodes, the spanning tree with the lowest weight of the minimum edge is calculated for the maximum edge weight value.

Analysis: This is similar to the minimum spanning tree, from small to large enumeration of the left endpoint, for each left endpoint, and then enumerate the right endpoint, constantly update the minimum value. It's a very simple question.

#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;const int maxn = + 5;co    NST int INF = 0x3f3f3f3f;int p[maxn];struct node{int u, V, W;    BOOL operator < (const node& p) Const {return W < P.W; }};node a[10000];int Find (int x) {return x = = P[x]? x:p[x] = Find (P[x]);}    int main () {int n, m;        while (scanf ("%d%d", &n, &m)) {if (!m &&!n) break;        for (int i = 0; i < m; ++i) scanf ("%d%d%d", &a[i].u, &AMP;A[I].V, &AMP;A[I].W);                Sort (A, a+m);        int ans = INF;            for (int l = 0; l < m; ++l) {//enum left endpoint int cnt = n;            for (int i = 0; I <= N; ++i) p[i] = i;                for (int r = l; r < m; ++r) {//enum right endpoint int x = Find (A[R].U);                int y = Find (A[R].V);                    if (x! = y) {p[x] = y;                    --cnt; if (1 = = cnt) {ans = min (ans, A[R].W-A[L].W);//update minimum value break;    }}}} printf ("%d\n", INF = = ans -1:ans); } return 0;}

The code is as follows:

UVa 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.