BZOJ P1083 繁忙的都市
传送门 : http://www.lydsy.com/JudgeOnline/problem.php?id=1083
比较简单的题 直接用Kruskal做一遍最小生成树,将最后加进去的边输出即可。
下面附上代码:
1 //By Neil 2 var 3 n,m :longint; 4 c :array[0..10000] of longint; 5 l,r :array[0..10000] of longint; 6 f :array[0..10005] of longint; 7 min :longint; 8 i,j :longint; 9 x,y :longint; 10 11 procedure swap(var a,b:longint); 12 var 13 temp :longint; 14 begin 15 temp:=a; 16 a:=b; 17 b:=temp; 18 end; 19 procedure qsort(x,y:longint); 20 var 21 i,j,k :longint; 22 begin 23 i:=x; 24 j:=y; 25 k:=c[ (i+j) div 2]; 26 repeat 27 while c[i]<k do inc(i); 28 while c[j]>k do dec(j); 29 if i<=j then 30 begin 31 swap(l[i],l[j]); 32 swap(r[i],r[j]); 33 swap(c[i],c[j]); 34 inc(i); 35 dec(j); 36 end; 37 until i>j; 38 if i<y then qsort(i,y); 39 if j>x then qsort(x,j); 40 end; 41 42 function getfather(x:longint):longint; 43 begin 44 if f[x]=x then exit(x); 45 f[x]:=getfather(f[x]); 46 exit(f[x]); 47 end; 48 49 procedure init; 50 begin 51 read(n,m); 52 for i:=1 to n do 53 f[i]:=i; 54 for i:=1 to m do 55 read(l[i],r[i],c[i]); 56 qsort(1,m); 57 end; 58 59 procedure main; 60 var 61 x,y :longint; 62 a,b :longint; 63 i :longint; 64 begin 65 for i:=1 to m do 66 begin 67 a:=l[i]; 68 b:=r[i]; 69 x:=getfather(a); 70 y:=getfather(b); 71 if x<>y then 72 begin 73 f[x]:=y; 74 min:=c[i]; 75 end; 76 end; 77 78 79 writeln(n-1,' ',min); 80 end; 81 82 begin 83 init; 84 main; 85 end.