Topic Links:
http://poj.org/problem?id=2421
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