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)