标签:
水题spfa排序从大到小就行了=-=//
1 type
2 node=record
3 f,t,l:longint;
4 end;
5 var n,m,i,j,fx,fy,num,ans:longint;
6 b:array[0..20001] of node;
7 father:array[0..1001] of longint;
8 procedure qs(t,w:longint);
9 var mid,l,r:longint;
10 tem:node;
11 begin
12 l:=t; r:=w; mid:=b[(l+r) shr 1].l;
13 repeat
14 begin
15 while b[l].l>mid do inc(l);
16 while b[r].l<mid do dec(r);
17 if l<=r then
18 begin
19 tem:=b[l];
20 b[l]:=b[r];
21 b[r]:=tem;
22 inc(l);
23 dec(r);
24 end;
25 end;
26 until l>r;
27 if t<r then qs(t,r);
28 if l<w then qs(l,w);
29 end;
30 function findfather(x:longint):longint;
31 begin
32 if x=father[x] then exit(x)
33 else father[x]:=findfather(father[x]);
34 exit(father[x]);
35 end;
36 begin
37 readln(n,m);
38 for i:=1 to m do
39 with b[i] do
40 readln(f,t,l);
41 qs(1,m); num:=0;
42 for i:=1 to n do
43 father[i]:=i;
44 for i:=1 to m do
45 begin
46 fx:=findfather(b[i].f);
47 fy:=findfather(b[i].t);
48 if fx<>fy then
49 begin
50 father[fx]:=fy;
51 inc(ans,b[i].l);
52 inc(num);
53 end;
54 if num=n-1 then
55 begin
56 writeln(ans);
57 halt;
58 end;
59 end;
60 writeln(-1);
61 end.
P3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
标签:
原文地址:http://www.cnblogs.com/Kalenda/p/4816773.html