11.2.2 example 11-2 UVA 1395 Slim Span (maximum-minimum as small as possible spanning tree)

Source: Internet
Author: User

Main topic:

Give you n points (n<=100), and then, let you find a spanning tree, making the maximum-minimum value of the Benquan as small as possible in the spanning tree.

Problem Solving Ideas:

Or according to the idea of the minimum spanning tree, start with all the edges according to the weight size, from small to large sort. Then, for an interval [l,r], each time we enumerate, if this [l,r] makes all n points connected, then the definition of their thinness is:

Maximum value-the minimum value. This slender degree must be <=cost[r]-cost[l]. So, we enumerate the L in order, each time we establish the minimum spanning tree with the edge of the [l,m] interval, if we can't satisfy the N-point Unicom, then return-1. Other times, it returns the value of Max-min, which instructs to find the smallest maximum-minimum value.

Code:

# include<cstdio># include<iostream># include<vector># include<algorithm>using namespace std    ; # define MAX 123# define INF 99999999int n,m;struct edge{int u,v,cost;    BOOL operator < (const Edge & A) const {return cost > a.cost; }};int f[max];vector<edge>edge;void Init () {for (int i = 0;i<= n;i++) f[i] = i;}    int getf (int x) {if (f[x]==x) return x;        else {int t = GETF (f[x]);        F[X] = t;    return f[x]; }}int Same (int x,int y) {return getf (x) ==getf (y);}    int Kruskal (int x) {init ();    int cnt = 0;    int _min = inf, _max =-1;        for (int i = X;i < m;i++) {Edge e = edge[i];        int u = edge[i].u;        int v = EDGE[I].V;        int UU = GETF (u);        int vv = GETF (v);            if (UU!=VV) {F[uu] = VV;            cnt++;            _min = min (_min,e.cost);        _max = max (_max,e.cost);      }} if (cnt==n-1)  return _max-_min; else return-1;}        int main (void) {while (scanf ("%d%d", &n,&m)!=eof) {if (n==0&&m==0) break;        Edge.clear ();            for (int i = 0;i < m;i++) {int a,b,c; scanf ("%d%d%d", &a,&b,&c);        Edge.push_back (Edge) {a,b,c});        } sort (Edge.begin (), Edge.end ());        int ans = inf;            for (int i = 0;i < m;i++) {int tmp = Kruskal (i);            if (tmp==-1) break;        ans = min (ans,tmp);        } if (Ans==inf) puts ("-1");    else printf ("%d\n", ans); } return 0;}

    

11.2.2 example 11-2 UVA 1395 Slim Span (maximum-minimum as small as possible 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.