[TYVJ1307 联络员]

【题目】:联络员

【来源】:Tyvj1307

【关键字】:图论 必连边

//================================================================================================

【分析】:克鲁斯卡尔,先将必连边加入生成树中,再用剩下的边构建生成树.也可以先将必连边的全加入答案,然后改为零.在对所有边找最小生成树.

【小结】:_______________________________________________________

//================================================================================================

【代码】:

ContractedBlock.gif ExpandedBlockStart.gif View Code
  1 program project1;
2 type
3 rec = record
4 x, y, d: longint;
5 end;
6 var
7 n, m, ans, tot, t, k: longint;
8 e: array[0..10010] of rec;
9 f: array[0..2010] of longint;
10 //a: array[0..2010,0..2010,1..2] of longint;
11 function get(k: longint):longint;
12 begin
13 if f[k] = k then exit(k);
14 f[k] := get(f[k]);
15 get := f[k];
16 end;
17 procedure init;
18 var
19 i, p, x, y, d, xx, yy: longint;
20 begin
21 readln(n,m);
22 tot := 0;
23 for i := 1 to n do f[i] := i;
24 for i := 1 to m do
25 begin
26 readln(p,x,y,d);
27 case p of
28 1:begin
29 inc(ans,d);
30 xx := get(x);
31 yy := get(y);
32 if xx <> yy then
33 begin
34 f[xx] := yy;
35 inc(t);
36 end;
37 end;
38 2:begin
39 inc(tot);
40 e[tot].x := x;
41 e[tot].y := y;
42 e[tot].d := d;
43 end;
44 end;
45 end;
46 //for i := 1 to tot do writeln(e[i].x,' ',e[i].y,' ',e[i].d);
47 //readln(k);
48 end;
49 procedure qs(l, r: longint);
50 var
51 i, j, mid: longint;
52 t: rec;
53 begin
54 i := l;
55 j := r;
56 mid := e[(l+r) div 2].d;
57 repeat
58 while e[i].d < mid do inc(i);
59 while e[j].d > mid do dec(j);
60 if i <= j then
61 begin
62 t := e[i];
63 e[i] := e[j];
64 e[j] := t;
65 inc(i);
66 dec(j);
67 end;
68 until i > j;
69 if l < j then qs(l,j);
70 if i < r then qs(i,r);
71 end;
72 procedure kurs;
73 var
74 i, x, y: longint;
75 begin
76 //for i := 1 to tot do writeln(e[i].x,' ',e[i].y,' ',e[i].d);
77 // read(k);
78 i := 1;
79 while t < n-1 do
80 begin
81 x := get(e[i].x);
82 //writeln(x);
83 //readln(k);
84 y := get(e[i].y);
85 //writeln(y);
86 //readln(k);
87 if x <> y then
88 begin
89 inc(ans,e[i].d);
90 f[x] := y;
91 inc(t);
92 end;
93 inc(i);
94 end;
95 end;
96 begin
97 init;
98 //writeln(tot);
99 qs(1,tot);
100 kurs;
101 writeln(ans);
102 //read(k);
103 end.



转载于:https://www.cnblogs.com/procedure2012/archive/2011/10/20/2218411.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值