一
function gcd(a
begin
if b=
else gcd:=gcd (b
end ;
function lcm(a
begin
if a
lcm:=a;
while lcm mod b>
end;
A
function prime (n: integer): Boolean;
var I: integer;
begin
for I:=
if n mod I=
prime:=false; exit;
end;
prime:=true;
end;
B
procedure getprime;
var
i
p:array[
begin
fillchar(p
p[
i:=
while i<
if p[i] then begin
j:=i*
while j<
p[j]:=false;
inc(j
end;
end;
inc(i);
end;
l:=
for i:=
if p[i] then begin
inc(l);pr[l]:=i;
end;
end;{getprime}
function prime(x:longint):integer;
var i:integer;
begin
prime:=false;
for i:=
if pr[i]>=x then break
else if x mod pr[i]=
prime:=true;
end;{prime}
二
A
procedure prim(v
var
lowcost
i
begin
for i:=
lowcost[i]:=cost[v
closest[i]:=v
end;
for i:=
{尋找離生成樹最近的未加入頂點k}
min:=maxlongint;
for j:=
if (lowcost[j]
min:=lowcost[j];
k:=j;
end;
lowcost[k]:=
{生成樹中增加一條新的邊k到closest[k]}
{修正各點的lowcost和closest值}
for j:=
if cost[k
lowcost[j]:=cost[k
closest[j]:=k;
end;
end;
end;{prim}
B
按權值遞增順序刪去圖中的邊
function find(v:integer):integer; {返回頂點v所在的集合}
var i:integer;
begin
i:=
while (i<=n) and (not v in vset[i]) do inc(i);
if i<=n then find:=i else find:=
end;
procedure kruskal;
var
tot
begin
for i:=
p:=n
sort;
{對所有邊按權值遞增排序
while p>
i:=find(e[q]
if i<>j then begin
inc(tot
vset[i]:=vset[i]+vset[j];vset[j]:=[];
dec(p);
end;
inc(q);
end;
writeln(tot);
end;
A
var
a:array[
b:array[
mark:array[
procedure bhf;
var
best
begin
fillchar(mark
mark[
repeat
best:=
for i:=
If mark[i] then {對每一個已計算出最短路徑的點}
for j:=
if (not mark[j]) and (a[i
if (best=
best:=b[i]+a[i
end;
if best>
b[best_j]:=best;mark[best_j]:=true;
end;
until best=
end;{bhf}
B
procedure floyed;
begin
for I:=
for j:=
if a[I
for k:=
for i:=
for j:=
if a[i
a[i
p[I
end;
end;
C
var
a:array[
b
mark:array[
procedure dijkstra(v
begin
fillchar(mark
for i:=
d[i]:=a[v
if d[i]<>
end;
mark[v
repeat {每循環一次加入一個離
min:=maxint; u:=
for i:=
if (not mark[i]) and (d[i]
u:=i; min:=d[i];
end;
if u<>
mark[u]:=true;
for i:=
if (not mark[i]) and (a[u
d[i]:=a[u
pre[i]:=u;
end;
end;
until u=
end;
Procedure Longlink;
Var
T:array[
Begin
Fillchar(t
For k:=
For I:=
For j:=
End;
A
procedure dfs ( now
begin
for i:=
if a[now
c[i]:=color;
dfs(I
end;
end;
B 寬度優先(種子染色法)
幾個定義
a
b
c
d
若 Ee[j] = El[j]
求解方法
a
b
c
找入度為
例 尋找一數列
Euler回路(DFS)
定義
Hamilton回路
定義
一筆畫
充要條件
x[I]
procedure bellman
begin
From:http://tw.wingwit.com/Article/program/sjjg/201311/23620.html