1.Prim算法(POJ2560)
const maxn=101; var x,y,mindis:array[0..maxn] of extended; dis:array[0..maxn,0..maxn] of extended; vis:array[0..maxn] of boolean; n,i,j:longint; function calc(a,b:longint):extended; begin calc:=sqrt(sqr(x[a]-x[b])+sqr(y[a]-y[b])); end; procedure prim(root:longint); var i,j,k:longint; min,sum:extended; begin sum:=0; fillchar(vis,sizeof(vis),0); vis[root]:=true; for i:=1 to n do mindis[i]:=dis[i,root]; for i:=1 to n-1 do begin min:=1e100; for j:=1 to n do if (not vis[j])and(mindis[j]<min) then begin min:=mindis[j]; k:=j; end; vis[k]:=true; sum:=sum+min; for j:=1 to n do if (not vis[j])and(dis[j,k]<mindis[j]) then mindis[j]:=dis[j,k]; end; writeln(sum:0:2); end; begin readln(n); for i:=1 to n do readln(x[i],y[i]); for i:=1 to n-1 do for j:=i+1 to n do begin dis[i,j]:=calc(i,j); dis[j,i]:=dis[i,j]; end; Prim(1); end.
2.Kruskal算法(POJ2485)
type node=record u,v,w:longint; end; const oo=1000000; var e:array[0..260000] of node; f:array[0..510] of longint; n,d,datanum:longint; procedure sort(l,r:longint); var i,j,mid:longint; temp:node; begin i:=l; j:=r; mid:=e[(l+r)>>1].w; repeat while e[i].w<mid do inc(i); while e[j].w>mid do dec(j); if i<=j then begin temp:=e[i]; e[i]:=e[j]; e[j]:=temp; inc(i); dec(j); end; until i>j; if i<r then sort(i,r); if j>l then sort(l,j); end; procedure init; var i,j,weight,ecount:longint; begin readln; readln(n); ecount:=0; for i:=1 to n do begin for j:=1 to n do begin read(weight); inc(ecount); if weight=0 then weight:=oo; e[ecount].w:=weight; e[ecount].u:=i; e[ecount].v:=j; end; readln; end; sort(1,n*n); end; function getf(x:longint):longint; begin if f[x]<>x then f[x]:=getf(f[x]); exit(f[x]); end; procedure union(u,v:longint); begin f[u]:=v; end; function kruskal:longint; var i,count,max:longint; begin for i:=1 to n do f[i]:=i; count:=0; max:=-oo; for i:=1 to n*n do begin if count=n-1 then break; if (getf(e[i].u)<>getf(e[i].v)) then begin inc(count); if e[i].w>max then max:=e[i].w; union(getf(e[i].u),getf(e[i].v)); end; end; exit(max); end; begin readln(datanum); for d:=1 to datanum do begin init; writeln(kruskal); end; end.