Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:kruskal不行么?我老是WAIn Reply To:Re:kruskal不行么?我老是WA Posted by:dtzh420 at 2009-04-05 20:16:13 const fin='d:\program\a.in'; fout='d:\program\a.out'; var test,gold,n,m,r,i,p,q,tot:longint; a:array[1..50100,1..3]of longint; fa:array[0..25000]of longint; procedure swap(var a,b:longint); var t:longint; begin t:=a; a:=b; b:=t; end; procedure sort(l,r:longint); var i,j,mid:longint; begin i:=l; j:=r; mid:=a[(i+j)>>1,3]; while i<=j do begin while a[i,3]>mid do inc(i); while a[j,3]<mid do dec(j); if i<=j then begin swap(a[i,1],a[j,1]);swap(a[i,2],a[j,2]);swap(a[i,3],a[j,3]); inc(i); dec(j); end; end; if l<j then sort(l,j); if i<r then sort(i,r); end; function find(x:longint):longint; begin if fa[x]=-1 then exit(x); find:=find(fa[x]); fa[x]:=find; end; BEGIN assign(input,fin);reset(input);assign(output,fout);rewrite(output); readln(test); readln; for gold:=1 to test do begin readln(n,m,r); for i:=1 to r do readln(a[i,1],a[i,2],a[i,3]); sort(1,r); fillchar(fa,sizeof(fa),$ff); tot:=0; for i:=1 to r do begin p:=find(a[i,1]); q:=find(n+a[i,2]); if p<>q then begin fa[p]:=q; inc(tot,a[i,3]); end; end; tot:=(n+m)*10000-tot; if tot<0 then writeln(0) else writeln(tot); readln; end; close(input);close(output); END. Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator