Poj1861&zoj1542--network "minimum Spanning tree"

Source: Internet
Author: User

Links: http://poj.org/problem?id=1861

Minimum spanning tree bare topic, output spanning tree's longest edge, number of nodes, node coordinates. In addition OJ the sample output is wrong, the minimum spanning tree of 4 points can be 4 sides.


is mainly familiar with handwriting Kruskal

#include <cstring> #include <string> #include <fstream> #include <iostream> #include < iomanip> #include <cstdio> #include <cctype> #include <algorithm> #include <queue> #include <map> #include <set> #include <vector> #include <stack> #include <ctime> #include < cstdlib> #include <functional> #include <cmath>using namespace std; #define PI ACOs ( -1.0) #define MAXN 20010#define EPS 1e-7#define INF 0x7fffffff#define seed 131#define ll long long#define ull unsigned ll#define lson l,m,rt& Lt;<1#define rson m+1,r,rt<<1|1struct node{int U,v,dis;} Edge[maxn];int father[1010],point[1010][2];int N,m,ans,sum;bool CMP (node X,node y) {return x.dis<y.dis;}    int find (int x) {int t = x;    while (T!=father[t]) {t = father[t];    } int k = x;        while (k!=t) {int temp = father[k];        Father[k] = t;    K = temp; } return t;}    void Kruskal () {int i,j=0; for (i=1;i<=n;i++) father[i] = i;    for (i=0;i<m;i++) {int a = find (EDGE[I].U);        int b = Find (EDGE[I].V);            if (a!=b) {Father[a] = b;            Point[j][0] = edge[i].u;            POINT[J][1] = EDGE[I].V;            Sum+=edge[i].dis;            ans = Edge[i].dis;            j + +;        if (j>=n-1) break;    }}}int Main () {int i,j;        while (scanf ("%d%d", &n,&m)!=eof) {sum = 0;        for (i=0;i<m;i++) {scanf ("%d%d%d", &edge[i].u,&edge[i].v,&edge[i].dis);        } sort (edge,edge+m,cmp);        Kruskal ();        printf ("%d\n%d\n", ans,n-1);        for (i=0;i<n-1;i++) {printf ("%d%d\n", point[i][0],point[i][1]); }} return 0;}


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.