POJ 1258-agri-net (Kruskal)

Source: Internet
Author: User

Title Link: agri-net


Minimum spanning tree water problem, the array opened and the title of the same, but is re, has filled a 0, fortunately this problem with Kuruskal knocked over, found a point problem, previously written in the Library Lu Carl template a bit of a problem, write a step useless operation, has been amended.

Test instructions: A farmer wants to choose the mayor .... Build a road so that it can reach all points, the shortest distance.

scanf input

796K 32MS

CIN input

832K 125MS

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include < algorithm>const int N = 11000;using namespace std;int n,ans,l;struct node{int u,v,w;}    Edge[10000];int father[n];int cmp (const void *a,const void *b) {node *x = (struct node *) A;    Node *y = (struct node *) b; return x->w-y->w;} int find (int x) {return (father[x]==x)? ( x): Find (Father[x]);}    void Kruskal () {ans = 0;     int UU,VV;    for (int i = 0;i<n;i++) father[i] = i;         for (int i = 0;i<l;i++) {UU = find (EDGE[I].U);         VV = find (EDGE[I].V);             if (UU!=VV) {Father[uu] = VV;         Ans + = EDGE[I].W; }} Cout<<ans<<endl;}    int main () {int A;        while (cin>>n) {L = 0;                for (int i = 0;i<n;i++) {for (int j = 0;j<n;j++) {cin>>a; EDGE[L].U = i; EDGE[L].V = j;              EDGE[L].W = A;  l++;        }} qsort (Edge,l,sizeof (edge[0]), CMP);    Kruskal (); } return 0;}


832K 125MS

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.