POJ 1789
题目大意: 有若干个节点,他们有一个字符串表示他们的状态(7位),每两个节点间的距离是他们状态不同处的个数,求一条路径连接所有节点。
解:最小生成树= =,可是我实在不想吐槽自己了。首先是堆写错,然后交上去还是wa,怕麻烦不想写对拍然后各种挂,最后写对拍后发现因为是多组询问,而我的ans没有清零导致wa的,浪费好多时间,不解释写完我要去k课本了= =..
View Code
1 program Truck_History;
2 const
3 maxn=2000;
4 bilibili=maxlongint;
5 type
6 data=record
7 dest, next, cost: longint;
8 end;
9 var
10 hptot, tot, ans, n: longint;
11 a: array[1..maxn]of string;
12 dist, vect, poss, heap: array[1..maxn]of longint;
13 edge: array[1..(maxn*maxn)] of data;
14 procedure add(x, y, z: longint);
15 begin
16 inc(tot);
17 with edge[tot] do
18 begin
19 dest := y;
20 cost := z;
21 next := vect[x];
22 vect[x] := tot;
23 end;
24 end;
25
26 procedure init;
27 var
28 i, j, k, w: longint;
29 begin
30 tot := 0;
31 hptot := 0;
32 ans := 0;
33 fillchar(vect, sizeof(vect), 0);
34 fillchar(poss, sizeof(poss), 0);
35 filldword(dist, sizeof(dist)>>2, bilibili);
36 for i := 1 to n do
37 begin
38 read(a[i]);
39 readln;
40 end;
41 for i := 1 to n-1 do
42 for j := i+1 to n do
43 begin
44 w := 0;
45 for k := 1 to 7 do
46 if a[i][k]<>a[j][k] then inc(w);
47 add(i, j, w);
48 add(j, i, w);
49 end;
50 end;
51
52 procedure up(x: longint);
53 var
54 i, tmp: longint;
55 begin
56 i := x;
57 tmp := heap[x];
58 while i>1 do
59 begin
60 if dist[tmp]<dist[heap[i >> 1]] then
61 begin
62 heap[i] := heap[i >> 1];
63 poss[heap[i]] := i;
64 i := i >> 1;
65 end
66 else break;
67 end;
68 heap[i] := tmp;
69 poss[tmp] := i;
70 end;
71
72 procedure down(x: longint);
73 var
74 i, j, tmp: longint;
75 begin
76 i := x;
77 tmp := heap[x];
78 while i << 1 <= hptot do
79 begin
80 j := i << 1;
81 if (j+1<=hptot)and(dist[heap[j]]>dist[heap[j+1]]) then inc(j);
82 if dist[tmp] > dist[heap[j]] then
83 begin
84 heap[i] := heap[j];
85 poss[heap[i]] := i;
86 i := j;
87 end
88 else break;
89 end;
90 heap[i] := tmp;
91 poss[tmp] := i;
92 end;
93
94 procedure prim;
95 var
96 j, i, u: longint;
97 begin
98 u := 1;
99 poss[u] := -1;
100 for j := 1 to n-1 do
101 begin
102 i := vect[u];
103 while i<>0 do
104 with edge[i] do
105 begin
106 if poss[dest]<>-1 then
107 if cost < dist[dest] then
108 begin
109 dist[dest] := cost;
110 if poss[dest]=0 then
111 begin
112 inc(hptot);
113 heap[hptot] := dest;
114 poss[dest] := hptot;
115 up(hptot);
116 end
117 else
118 begin
119 up(poss[dest]);
120 down(poss[dest]);
121 end;
122 end;
123 i := next;
124 end;
125 u := heap[1];
126 poss[u] := -1;
127 ans := ans + dist[u];
128 heap[1] := heap[hptot];
129 poss[heap[1]] := 1;
130 dec(hptot);
131 down(1);
132 end;
133 end;
134
135 procedure print;
136 begin
137 writeln('The highest possible quality is 1/', ans, '.');
138 end;
139
140 procedure main;
141 begin
142 while true do
143 begin
144 readln(n);
145 if n=0 then
146 begin
147 close(input);
148 close(output);
149 halt;
150 end;
151 init;
152 prim;
153 print;
154 end;
155 end;
156
157 begin
158 assign(input,'aaa.in'); reset(input);
159 assign(output,'aaa.out'); rewrite(output);
160 main;
161 close(input);
162 close(output);
163 end.
对拍君
1 const
2 maxn=2000;
3 var
4 a: array[1..maxn]of string;
5 g: array[1..maxn, 1..maxn]of longint;
6 dist: array[1..maxn]of longint;
7 n, ans: longint;
8 procedure init;
9 var
10 i, j, k, w: longint;
11 begin
12 filldword(dist, sizeof(dist)>>2 , maxlongint);
13 ans := 0;
14 readln(n);
15 if n=0 then
16 begin
17 close(input);
18 close(output);
19 halt;
20 end;
21 fillchar(g, sizeof(g), 0);
22 for i := 1 to n do
23 readln(a[i]);
24 for i := 1 to n-1 do
25 for j := i + 1 to n do
26 begin
27 w := 0;
28 for k := 1 to 7 do
29 if a[i][k]<>a[j][k] then inc(w);
30 g[i, j] := w;
31 g[j, i] := w;
32 end;
33 end;
34
35 procedure prim;
36 var
37 u, i, j, k, min, tmp: longint;
38 begin
39 u := 1;
40 dist[1] := -1;
41 for j := 1 to n-1 do
42 begin
43 min := maxlongint;
44 for k := 1 to n do
45 if (dist[k]<>-1)and(dist[k]>g[u, k]) then dist[k] := g[u, k];
46 for k := 1 to n do
47 if (dist[k]<>-1)and(min > dist[k]) then
48 begin
49 tmp := k;
50 min := dist[k];
51 end;
52 u := tmp;
53 dist[u] := -1;
54 ans := ans + min;
55 end;
56 end;
57
58 procedure print;
59 begin
60 writeln('The highest possible quality is 1/', ans, '.');
61 end;
62
63 procedure main;
64 begin
65 while true do
66 begin
67 init;
68 prim;
69 print;
70 end;
71 end;
72
73 begin
74 assign(input,'aaa.in'); reset(input);
75 assign(output,'wmz.out'); rewrite(output);
76
77 main;
78
79 close(input); close(output);
80 end.
生成君
1 var
2 tot, i, j, n, orz: longint;
3 begin
4 assign(output,'aaa.in'); rewrite(output);
5 randomize;
6 orz := random(10)+1;
7 for tot := 1 to 2 do
8 begin
9 n := random(10+1);
10 writeln(n);
11 for i := 1 to n do
12 begin
13 for j := 1 to 7 do
14 begin
15 write(chr(random(2)+97))
16
17 end;
18 writeln;
19 end;
20 end;
21 writeln(0);
22 close(output);
23 end.