POJ 1062
POJ1062
题目大意:求从0到1的最短路。
解:将题意拆解见图的推理不难,故在此省略。卡住我的主要是等级问题,我也读不是很懂,最后上网搜题解才发现自己原来的判断有问题,并不是距离酋长为m的 一个2m区间,而直接是一个包含酋长的区间m(竟然有比酋长还高级的人)所以枚举区间直接ac,for a[1]-k to a[1] if i<=pow[x]<=i+k;
View Code
1 const
2 maxn=100;
3 bilibili=maxlongint >> 1;
4 type
5 data=record
6 dest, cost, next: longint;
7 end;
8 var
9 edge: array[1..(maxn+1)*(maxn+1)]of data;
10 vect, dist, pow: array[0..maxn]of longint;
11 i, ans, m, n, tot: longint;
12 procedure add(x, y, z: longint);
13 begin
14 inc(tot);
15 with edge[tot] do
16 begin
17 dest := y;
18 cost := z;
19 next := vect[x];
20 vect[x] := tot;
21 end;
22 end;
23
24 procedure init;
25 var
26 i, j, x, y, z, tmp: longint;
27 begin
28 ans := maxlongint;
29 tot := 0;
30 fillchar(vect, sizeof(vect), 0);
31 fillchar(pow, sizeof(pow), 0);
32 pow[0] := maxlongint;
33 readln(m, n);
34 for i := 1 to n do
35 begin
36 readln(x, y, tmp);
37 add(0, i, x);
38 pow[i] := y;
39 for j := 1 to tmp do
40 begin
41 readln(x, y);
42 add(x, i, y);
43 end;
44 end;
45 end;
46
47 procedure spfa(b, e, key: longint);
48 var
49 q: array[1..maxn+10]of longint;
50 visit: array[0..maxn]of boolean;
51 head, tail, i, u: longint;
52 begin
53 fillchar(visit, sizeof(visit), 0);
54 filldword(dist, sizeof(dist)>>2, bilibili);
55 dist[b] := 0;
56 head := 0;
57 tail := 1;
58 q[1] := b;
59 visit[b] := true;
60 while head<>tail do
61 begin
62 head := head mod (maxn+10)+1;
63 u := q[head];
64 i := vect[u];
65 while i<>0 do
66 with edge[i] do
67 begin
68 if ((pow[dest]>=key)and(pow[dest]<=key+m)) then
69 begin
70 if dist[u] + cost < dist[dest] then
71 begin
72 dist[dest] := dist[u] + cost;
73 if not visit[dest] then
74 begin
75 visit[dest] := true;
76 tail := tail mod (maxn+10) +1;
77 q[tail] := dest;
78 end;
79 end;
80 end;
81 i := next;
82 end;
83 visit[u] := false;
84 end;
85 if dist[1]<ans then ans := dist[1];
86 end;
87
88 procedure print;
89 begin
90 writeln(ans);
91 end;
92
93 begin
94 assign(input,'aaa.in'); reset(input);
95 init;
96 for i := pow[1]-m to pow[1] do spfa(0, 1, i);
97 print;
98 end.