Links: Click to open link
The topic is very long, but also has the picture, therefore does not copy pastes over, this question's effect is:
A tree t (connected without rings map) will use the N-1 bar to connect all of the original n vertices, the resulting spanning tree's maximum weight margin and the minimum weight of the margin ("slender value") as small as possible to find the smallest slim value;
Ideas:
enumeration with Kruskal;
First, the weights of each edge are sorted from small to large;
Enumerate each edge to generate the smallest spanning tree for the minimum edge, and calculate the slender value of such a spanning tree, enumerate all the circumstances to find a slim value;
The code resolves as follows:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <stack > #include <queue> #include <map> #include <set> #include <cmath> #include <algorithm># Define MAXN 1000005#define RST (n) memset (n, 0, sizeof (n)) using namespace std;typedef struct edge_{int x, y; int rank;} Edge; Edge Edge[maxn];int N, M, res, KC, father[maxn];int cmp (const void *a, const void *b) {Edge *P1 = (edge *) A; Edge *P2 = (edge *) b; return P1->rank-p2->rank;} int find (int x)//path compression look for parent Node {return x = = Father[x]? X:find (Father[x]);} int solve (int x) {int cnt = 0; for (int i=1; i<=n; i++) father[i]=i; for (int i=x; i<m; i++) {//kruskal try to generate the smallest spanning tree; int fx = find (edge[i].x); int fy = find (EDGE[I].Y); if (FX! = FY) {father[fx] = FY; cnt++; } if (cnt = = n-1) return edge[i].rank-edge[x].rank; } return MAXN;} int main () {while (~SCANF ("%d%d ", &n, &m) && n | | m) {res = MAXN; for (int i=0; i<m; i++) {scanf ("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].rank); } qsort (Edge, M, sizeof (EDGE), CMP); Weights from large to small, for (int i=0; i<=m-n+1; i++) {//from 0 to M (n-1) Range Enumeration res = solve (i) < res? Solve (i): Res Find the deserted value;} if (res = = MAXN) res =-1; Does not exist the words printf ("%d\n", res); } return 0;}
POJ 3522 Slim Span (and set + enum + Kruskal)