zoj3204 Connect them minimum spanning tree MST

Source: Internet
Author: User
This problem, there are several places, to see:
#include <stdio.h> #include <stdlib.h> typedef struct {int x, y;
int W;
}node;
    typedef struct {int x1;
int x2;
}xx;
XX tt[1005];
NODE node[5005];
int a[105][105],k,pre[105],temp[1000];
    int find (int x) {while (x!=pre[x]) x=pre[x];  
return x; } int cmp (const void *a,const void *b) {if (((node*) a)->w! = ((node*) b)->w) return ((node*) a)-& Gt;w > ((node*) b)->w?
    1:-1; else if ((node*) a)->x! = ((node*) b)->x) return ((node*) a)->x > ((node*) b)->x?
       1:-1; else return ((node*) a)->y > ((node*) b)->y?   
1:-1;  } int cmp2 (const void *a,const void *b) {if (((xx*) a)->x1! = ((xx*) b)->x1) return ((xx*) a)->x1 > ((xx*) b)->x1?
    1:-1; else return ((xx*) a)->x2 > ((xx*) b)->x2?
    1:-1;
    
    } int Kruskal () {int i,v1,v2,q=0;    
    Qsort (node,k,sizeof (node), CMP); for (i=0;i<k;i++) {v1=find (node[i].x);
        V2=find (NODE[I].Y);
            if (v1!=v2&&node[i].w!=0) {tt[q].x1=node[i].x;
            TT[Q].X2=NODE[I].Y;
            PRE[V2]=V1;
        q++;
}} return q;
   } int main () {int t,n,i,j,num;
   scanf ("%d", &t);  
      while (t--) {k=0;
      
      scanf ("%d", &n);
      
      for (i=1;i<=n;i++) pre[i]=i;
      for (i=1;i<=n;i++) for (j=1;j<=n;j++) scanf ("%d", &a[i][j]);
            for (i=1;i<=n;i++) for (j=i+1;j<=n;j++) {node[k].x=i;
            Node[k].y=j;
            NODE[K].W=A[I][J];
        k++;
      } Num=kruskal ();
         if (num==n-1) {qsort (tt,num,sizeof (xx), CMP2);
         for (i=0;i<num-1;i++) printf ("%d%d", tt[i].x1,tt[i].x2);
      printf ("%d%d\n", tt[num-1].x1,tt[num-1].x2);
    } else printf (" -1\n");
  } return 0;
 }

This code is AC, if the 14th row node size is defined as nodes node[1005] .... Yes, I didn't think about it at first, it was small, but I'm not going to give it to WA right away. Originally thought of using multi-level sorting can be finished output, very proud, but this just started the WA let me suddenly no feeling ... Damn wa .....

Also, the last qsort can be too, but time-consuming.

Well, if the array was opened directly at that point, it would not be so tangled, alas alas alas ... However, finally AC, the minimum spanning tree will be the first to come to an over, although with only Kruskal, go back to practice prime, next DP ...

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.