51nod 1212--Non-graph minimum spanning tree

Source: Internet
Author: User
The least spanning tree of the graph without graphs


The undirected graph of the N-point M-edge, with a weighted value for each edge, to find the smallest spanning tree of the graph.
Input line 1th: 2 number n,m The middle is separated by a space, N is the number of points, and M is the number of edges. (2 <= N <=, 1 <= M <= 50000)
Section 2-m + 1 lines: 3 s E W per line, representing 2 vertices and weights for M-bars. (1 <= S, E <= n,1 <= W <= 10000) Output outputs the sum of the weights of all edges of the minimum spanning tree. Sample Input

9
1 2 4
2 3 8
3 4 7
4 5 9 5 6 6
7 2
7 8 1
8 9 7 2
8 each
3 9
2 7 9 6
  
   3 6 4
4 6
1 8 8
  
Sample Output
37

Problem Solving Ideas:

is actually a minimum spanning tree template problem, with the Kruskal algorithm to write. The information of each road is stored, and the weights of the edges are sorted from small to large, each time the least-weighted edge is added to the tree, and the two vertices of the edge are added to a set, if the two vertices themselves are in a set (that is, adding this edge to form a loop), discard this edge to determine the next one. The minimum spanning tree construct is completed until the N-1 bar Edge is selected for the join tree. See the code specifically.


The code is as follows:


#include <stdio.h> #include <string.h> #include <algorithm> #define INF 0x3f3f3f3f//define Maxima INF using

namespace Std;
int n, m, Sum, ans;     int f[1010];
    Array F determines the set state of the point struct node {int x;       int y;
The two vertices of the storage edge and the weight int z;

} q[50010];  BOOL CMP (Node A, Node B) {return a.z < b.z;  The weights are arranged from small to large} int getf (int v) {if (v = f[v]) f[v] = GETF (F[v]);
Find parent node return F[V];
    } int Lian (int t1, int t2) {int v1 = GETF (t1);
    int v2 = GETF (t2);
        if (v1! = V2) {//If two vertices are not in the same set, this edge is added to the tree f[v1] = v2;
    return 1;
} return 0; } void Kruskal () {for (int i = 0; i < m; i++) {if (Lian (q[i].x,q[i].y)) {ans+
            +;
            sum = sum + q[i].z;
        if (ans = = n-1)//When the n-1 edge is selected, the minimum spanning tree construction completes the break;
            }}} int main () {while (~scanf ("%d%d", &n,&m)) {for (int i = 0; i < m; i++) scanf ("%d%d%d ", &q[i].x,&q[i].y,&q[i].z);
        Sort (q,q+m,cmp);
        sum = ans = 0; for (int i = 1; I <= n; i++) f[i] = i;
        Initializing the parent node is itself Kruskal ();
    printf ("%d\n", sum);
} 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.