POJ 1125

POJ1125   

题目大意:在一幅有向图中,求在某个点出发,到达最远的点最少的时间是多少?

解:暴力最短路即可…….

View Code
  1 const
2 maxn=100;
3 bilibili=maxlongint >> 1;
4 type
5 data=record
6 dest, next, cost: longint;
7 end;
8 var
9 edge: array[1..maxn*maxn]of data;
10 dist, vect, heap, poss: array[1..maxn]of longint;
11 hptot, tot, n, who, ans: longint;
12 procedure print;
13 begin
14 if ans=bilibili then
15 writeln('disjoint')
16 else writeln(who, ' ', ans);
17 end;
18
19 procedure add(x, y, z: longint);
20 begin
21 inc(tot);
22 with edge[tot] do
23 begin
24 dest := y;
25 cost := z;
26 next := vect[x];
27 vect[x] := tot;
28 end;
29 end;
30
31 procedure init;
32 var
33 tmp, i, j, x, y: longint;
34 begin
35 fillchar(vect, sizeof(vect), 0);
36 tot := 0;
37 ans := bilibili;
38 readln(n);
39 if n=0 then
40 begin
41 close(input); close(output);
42 halt;
43 end;
44 for i := 1 to n do
45 begin
46 read(tmp);
47 for j := 1 to tmp do
48 begin
49 read(x, y);
50 add(i, x, y);
51 end;
52 readln;
53 end;
54 end;
55
56 procedure up(x: longint);
57 var
58 i, tmp: longint;
59 begin
60 i := x;
61 tmp := heap[x];
62 while i>1 do
63 begin
64 if dist[tmp]<dist[heap[i >> 1]] then
65 begin
66 heap[i] := heap[i >> 1];
67 poss[heap[i]] := i;
68 i := i >> 1;
69 end
70 else break;
71 end;
72 heap[i] := tmp;
73 poss[tmp] := i;
74 end;
75
76 procedure down(x: longint);
77 var
78 i, j, tmp: longint;
79 begin
80 i := x;
81 tmp := heap[x];
82 while i << 1 <= hptot do
83 begin
84 j := i << 1;
85 if (j+1<=hptot)and(dist[heap[j]] > dist[heap[j+1]]) then inc(j);
86 if dist[tmp]>dist[heap[j]] then
87 begin
88 heap[i] := heap[j];
89 poss[heap[i]] := i;
90 i := j;
91 end
92 else break;
93 end;
94 heap[i] := tmp;
95 poss[tmp] := i;
96 end;
97
98 procedure dijkstra(b: longint);
99 var
100 i, j, u: longint;
101 begin
102 fillchar(poss, sizeof(poss), 0);
103 filldword(dist, sizeof(dist)>>2, bilibili);
104 hptot := 1;
105 dist[b] := 0;
106 heap[1] := b;
107 poss[b] := 1;
108 repeat
109 u := heap[1];
110 heap[1] := heap[hptot];
111 poss[heap[1]] := 1;
112 dec(hptot);
113 down(1);
114 i := vect[u];
115 while i<>0 do
116 with edge[i] do
117 begin
118 if dist[u] + cost < dist[dest] then
119 begin
120 dist[dest] := dist[u] + cost;
121 if poss[dest]=0 then
122 begin
123 inc(hptot);
124 heap[hptot] := dest;
125 poss[dest] := hptot;
126 up(hptot);
127 end
128 else
129 up(poss[dest]);
130 end;
131 i := next;
132 end;
133 until hptot<1;
134 end;
135
136 procedure main;
137 var
138 i, j, tmp: longint;
139 begin
140 while true do
141 begin
142 init;
143 for i := 1 to n do
144 begin
145 tmp := 0;
146 dijkstra(i);
147 for j := 1 to n do
148 begin
149 if tmp<dist[j] then tmp := dist[j];
150 if tmp=bilibili then break;
151 end;
152 if ans>tmp then
153 begin
154 ans := tmp;
155 who := i;
156 end;
157 end;
158 print;
159 end;
160 end;
161
162 begin
163 //assign(input,'aaa.in'); reset(input);
164 main;
165 end.



原文地址:https://www.cnblogs.com/wmzisfoolish/p/2435155.html