POJ 3522 Slim Span (and set + enum + Kruskal)

Source: Internet
Author: User

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)

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.