POJ 2485,1258
POJ 2485,1258
题目大意:水水的最小生成树模型= =;
解:prim,堆都不用了,1a有木有,纯当英语阅读了。
2485
1 const
2 maxn=111;
3 var
4 g: array[1..maxn, 1..maxn]of longint;
5 dist: array[1..maxn]of longint;
6 n, ans: longint;
7 procedure init;
8 var
9 i, j: longint;
10 begin
11 ans := 0;
12 filldword(dist, sizeof(dist)>>2, maxlongint);
13 n := 0;
14 readln(n);
15 if n=0 then
16 begin
17 close(input);
18 close(output);
19 halt;
20 end;
21 for i := 1 to n do
22 begin
23 for j := 1 to n do
24 read(g[i, j]);
25 readln;
26 end;
27 end;
28
29 procedure prim;
30 var
31 i, u, j, tot, min: longint;
32 begin
33 u := 1;
34 dist[u] := -1;
35 for i := 1 to n-1 do
36 begin
37 for j := 1 to n do
38 if (dist[j]<>-1)and(g[u, j]<dist[j]) then
39 dist[j] := g[u, j];
40 min := maxlongint;
41 for j := 1 to n do
42 if (dist[j]<>-1)and(min>dist[j]) then
43 begin
44 tot := j;
45 min := dist[j];
46 end;
47 ans := ans + min;
48 u := tot;
49 dist[u] := -1;
50 end;
51 end;
52
53 procedure print;
54 begin
55 writeln(ans);
56 end;
57
58 procedure main;
59 begin
60 while not(eof) do
61 begin
62 init;
63 prim;
64 print;
65 end;
66 end;
67
68 begin
69 //assign(input,'aaa.in'); reset(input);
70 main;
71 //close(input);
72 end.