【题目】:佳佳的魔法药水
【来源】:NDK
【关键字】:图论 构图
//================================================================================================
【分析】:和最优贸易类似,都是转换成图,由最短路来做.只不过这里的边实际是指向合成的魔药.只有更新部分有些区别.
【小结】:一类题的做法
//================================================================================================
【代码】:
View Code
1 const maxn = 1001;
2 var
3 a : array [0..maxn,0..maxn] of longint;
4 dist,t : array [0..maxn] of longint;
5 v : array [0..maxn] of boolean;
6 n : longint;
7
8 procedure init;
9 var i , x , y , d : longint;
10 begin
11 readln(n);
12 for i := 0 to n-1 do read(dist[i]); readln;
13 fillchar(a,sizeof(a),60);
14 while not eof do
15 begin
16 readln(x,y,d);
17 a[x,y] := d;
18 a[y,x] := d;
19 end;
20 for i := 0 to n-1 do t[i] := 1;
21 end;
22
23 procedure dijstra;
24 var i , j , min , p : longint;
25 begin
26 for i := 0 to n-1 do
27 begin
28 min := maxlongint;
29 for j := 0 to n-1 do
30 if not v[j] and (dist[j] < min) then
31 begin
32 min := dist[j];
33 p := j;
34 end;
35 v[p] := true;
36 if p = 0 then break;
37 for j := 0 to n-1 do
38 if v[j] and (a[p,j] <= maxn+1) then
39 if (min + dist[j] < dist[a[p,j]]) then
40 begin
41 dist[a[p,j]] := min + dist[j];
42 t[a[p,j]] := t[p]*t[j];
43 end else
44 if (min + dist[j] = dist[a[p,j]]) then
45 t[a[p,j]] := t[a[p,j]] + t[p]*t[j];
46 end;
47 writeln(dist[0],' ',t[0]);
48 end;
49
50 begin
51 init;
52 dijstra;
53 end.