procedure2012
It's not worth it to know you're not worth it!

【题目】:Highways

【来源】:PKU 1751

【关键字】:图论 最小生成树

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

【分析】:先把已有边权改为零,再裸prim。

【小结】:prim把kurscal鄙视了.......

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

【代码】:

View Code
 1 program project1;
2 type
3 rec = record
4 x, y: longint;
5 end;
6 var
7 n, m, p, tot: longint;
8 b: array[0..1000] of boolean;
9 dis: array[0..1000,0..1000] of real;
10 pre: array[0..1000] of longint;
11 v: array[0..1000] of real;
12 a, ans: array[0..1000] of rec;
13 function d(x, y: longint):real;
14 begin
15 d := sqrt(sqr(a[x].x-a[y].x)+sqr(a[x].y-a[y].y));
16 end;
17 procedure init;
18 var
19 i, j, x, y: longint;
20 begin
21 readln(n);
22 for i := 1 to n do readln(a[i].x,a[i].y);
23 for i := 1 to n do
24 for j := 1 to n do
25 dis[i,j] := d(i,j);
26 readln(m);
27 for i := 1 to m do
28 begin
29 readln(x,y);
30 dis[x,y] := 0;
31 dis[y,x] := 0;
32 end;
33 end;
34 procedure prim;
35 var
36 i, j, t: longint;
37 min, temp: real;
38 begin
39 fillchar(b,sizeof(b),false);
40 fillchar(v,sizeof(v),100);
41 v[1] := 0;
42 for i := 1 to n do
43 begin
44 min := maxlongint;
45 for j := 1 to n do
46 if (not b[j]) and (v[j] < min) then
47 begin
48 min := v[j];
49 t := j;
50 end;
51 b[t] := true;
52 inc(tot);
53 ans[tot].x := pre[t];
54 ans[tot].y := t;
55 for j := 1 to n do
56 if not b[j] then
57 begin
58 temp := dis[t,j];
59 if v[j] > temp then begin v[j] := temp; pre[j] := t; end;
60 end;
61 end;
62 end;
63 procedure print;
64 var
65 i: longint;
66 begin
67 //for i := 1 to n do writeln(pre[i],'##');
68 for i := 1 to tot do
69 if dis[ans[i].x,ans[i].y] <> 0 then
70 writeln(ans[i].x,' ',ans[i].y);
71 end;
72 begin
73 init;
74 prim;
75 print;
76 //read(n);
77 end.



posted on 2011-10-20 01:39  procedure2012  阅读(199)  评论(0编辑  收藏  举报