-
Notifications
You must be signed in to change notification settings - Fork 6
/
spfa.pas
executable file
·113 lines (102 loc) · 2.48 KB
/
spfa.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
program magic;
const
maxn=600*600;
var
dis,ci,a,head:array[0..660] of longint;
dui:array[0..600*600]of longint;
u:array[0..660]of boolean;
d,v,next:array[0..6000]of longint;
hp,num,n:longint;
procedure add(x,y,z:longint);
begin
inc(num);v[num]:=y;d[num]:=z;next[num]:=head[x];head[x]:=num;
end;
procedure print;
var
x,i:longint;
begin
for x:=0 to n do
begin
writeln(x);
i:=head[x];
while i<>0 do
begin
write(v[i],':',d[i],';');
i:=next[i];
end;
writeln;
end;
writeln;
end;
function can(cd:longint):boolean;
var
y,i,t,w:longint;
begin
fillchar(head,sizeof(head),0);
num:=0;
for i:=1 to n do
if a[i]-hp>=0 then
begin
y:=(a[i]-hp)div 15;
add(i-1,y,-1)
end;
for i:=1 to n do
add(i,i-1,0);
for i:=cd to n do
add(i-cd,i,1);
fillchar(dis,sizeof(dis),$7f);
fillchar(ci,sizeof(ci),0);
fillchar(u,sizeof(u),false);
dui[1]:=0; dis[0]:=0;ci[0]:=1;
t:=0;w:=1;
if cd=2 then begin writeln('cd=2');print;end;
if cd=1 then begin writeln('cd=1');print;end;
while t<>w do
begin
t:=t mod maxn +1;
i:=head[dui[t]];
while i<>0 do
begin
if dis[dui[t]]+d[i]<dis[v[i]] then
begin
dis[v[i]]:=dis[dui[t]]+d[i];
if not u[v[i]] then
begin
w:=w mod maxn +1;
inc(ci[v[i]]);
if ci[v[i]]>n then exit(false);
dui[w]:=v[i];u[v[i]]:=true;
end;
end;
i:=next[i];
end;
u[dui[t]]:=false;
end;
if w<=n*n then exit(true) else exit(false);
end;
procedure init;
var
i,l,r,mid,x:longint;
begin
readln(n,hp);
for i:=1 to n do
begin
a[i]:=a[i-1];
read(x);
a[i]:=a[i]+x;
end;
l:=0;r:=n+1;
while l<r do
begin
mid:=(l+r)>>1;
if can(mid) then l:=mid+1 else r:=mid;
end;
if r=n+1 then writeln('No upper bound.')
else writeln(r-1)
end;
begin
assign(input,'magic.in');assign(output,'magic.out');
reset(input);rewrite(output);
init;
close(input);close(output);
end.