Constructing Roads----poj2421

Source: Internet
Author: User

Topic Links:

Want to connect n villages together; for the minimum spanning tree, the difference is that the M-line segments are linked together, for the rest;

It's a little easier to feel kruskal.

#include <stdio.h>#include<string.h>#include<map>#include<iostream>#include<algorithm>#include<math.h>#defineN 110#defineINF 0XFFFFFFFusing namespacestd;intF[n];structnode{intX,y,d;} A[n*N];intCMP (node P,node q) {returnP.D <q.d;}intFind (intx) {    if(x!=f[x]) f[x]=Find (f[x]); returnf[x];}intMain () {intN, I, J, K, M, D, X, y, px, py, ans;  while(SCANF ("%d", &n)! =EOF) {memset (A,0,sizeof(a)); K= ans =0;  for(i=0; i<=n;i++) F[i]=i;  for(i=1; i<=n; i++)             for(j=1; j<=n; J + +) {scanf ("%d",&d); if(i<j) a[k].x= i, a[k].y = j, a[k++].d =D; } sort (A,a+k,cmp); scanf ("%d", &m);  for(i=0; i<m; i++) {scanf ("%d%d", &x, &y); PX=Find (x); PY=Find (y); if(px! =py) f[px]=py; }         for(i=0; i<k; i++) {px=Find (a[i].x); PY=Find (A[I].Y); if(px! =py) f[px]= py,ans+=A[I].D; } printf ("%d\n", ans); }    return 0;}

Constructing Roads----poj2421

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: 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.