ACM算法集1

ACM算法集1

一、數論算法
1
.求兩數的最大公約數
function gcd(a,b:integer):integer;
begin
if b=0 then gcd:=a
else gcd:=gcd (b,a mod b);
end ;
2
.求兩數的最小公倍數
function lcm(a,b:integer):integer;
begin
if a<b then swap(a,b);
lcm:=a;
while lcm mod b>0 do inc(lcm,a);
end;
3
.素數的求法
A.
小範圍內判斷一個數是否爲質數:
function prime (n: integer): Boolean;
var I: integer;
begin
for I:=2 to trunc(sqrt(n)) do
if n mod I=0 then begin
prime:=false; exit;
end;
prime:=true;
end;
B.
判斷longint範圍內的數是否爲素數(包含求50000以內的素數表):
procedure getprime;
var
i,j:longint;
p:array[1..50000] of boolean;
begin
fillchar(p,sizeof(p),true);
p[1]:=false;
i:=2;
while i<50000 do begin
if p[i] then begin
j:=i*2;
while j<50000 do begin
p[j]:=false;
inc(j,i);
end;
end;
inc(i);
end;
l:=0;
for i:=1 to 50000 do
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:=1 to l do
if pr[i]>=x then break
else if x mod pr[i]=0 then exit;
prime:=true;
end;{prime}

二、圖論算法
1
.最小生成樹
A.Prim
算法:
procedure prim(v0:integer);
var
lowcost,closest:array[1..maxn] of integer;
i,j,k,min:integer;
begin
for i:=1 to n do begin
lowcost[i]:=cost[v0,i];
closest[i]:=v0;
end;
for i:=1 to n-1 do begin
{
尋找離生成樹最近的未加入頂點k}
min:=maxlongint;
for j:=1 to n do
if (lowcost[j]<min) and (lowcost[j]<>0) then begin
min:=lowcost[j];
k:=j;
end;
lowcost[k]:=0; {
將頂點k加入生成樹
}
{
生成樹中增加一條新的邊k
closest[k]}
{
修正各點的lowcostclosest
}
for j:=1 to n do
if cost[k,j]<lwocost[j] then begin
lowcost[j]:=cost[k,j];
closest[j]:=k;
end;
end;
end;{prim}
B.Kruskal
算法:(貪心
)
按權值遞增順序刪去圖中的邊,若不形成迴路則將此邊加入最小生成樹。

function find(v:integer):integer; {
返回頂點v所在的集合}
var i:integer;
begin
i:=1;
while (i<=n) and (not v in vset[i]) do inc(i);
if i<=n then find:=i else find:=0;
end;
procedure kruskal;
var
tot,i,j:integer;
begin
for i:=1 to n do vset[i]:=[i];{
初始化定義n個集合,第I個集合包含一個元素
I}
p:=n-1; q:=1; tot:=0; {p
爲尚待加入的邊數,q爲邊集指針
}
sort;
{
對所有邊按權值遞增排序,存於e[I]中,e[I].v1e[I].v2爲邊I所連接的兩個頂點的序號,e[I].len爲第I條邊的長度
}
while p>0 do begin
i:=find(e[q].v1);j:=find(e[q].v2);
if i<>j then begin
inc(tot,e[q].len);
vset[i]:=vset[i]+vset[j];vset[j]:=[];
dec(p);
end;
inc(q);
end;
writeln(tot);
end;
2.
最短路徑

A.
標號法求解單源點最短路徑:
var
a:array[1..maxn,1..maxn] of integer;
b:array[1..maxn] of integer; {b[i]
指頂點i到源點的最短路徑}
mark:array[1..maxn] of boolean;
procedure bhf;
var
best,best_j:integer;
begin
fillchar(mark,sizeof(mark),false);
mark[1]:=true; b[1]:=0;{1
爲源點
}
repeat
best:=0;
for i:=1 to n do
If mark[i] then {
對每一個已計算出最短路徑的點
}
for j:=1 to n do
if (not mark[j]) and (a[i,j]>0) then
if (best=0) or (b[i]+a[i,j]<best) then begin
best:=b[i]+a[i,j]; best_j:=j;
end;
if best>0 then begin
b[best_j]:=best
mark[best_j]:=true;
end;
until best=0;
end;{bhf}
B.Floyed
算法求解所有頂點對之間的最短路徑:

procedure floyed;
begin
for I:=1 to n do
for j:=1 to n do
if a[I,j]>0 then p[I,j]:=I else p[I,j]:=0; {p[I,j]
表示Ij的最短路徑上j的前驅結點}
for k:=1 to n do {
枚舉中間結點
}
for i:=1 to n do
for j:=1 to n do
if a[i,k]+a[j,k]<a[i,j] then begin
a[i,j]:=a[i,k]+a[k,j];
p[I,j]:=p[k,j];
end;
end;
C. Dijkstra
算法:

var
a:array[1..maxn,1..maxn] of integer;
b,pre:array[1..maxn] of integer; {pre[i]
指最短路徑上I的前驅結點}
mark:array[1..maxn] of boolean;
procedure dijkstra(v0:integer);
begin
fillchar(mark,sizeof(mark),false);
for i:=1 to n do begin
d[i]:=a[v0,i];
if d[i]<>0 then pre[i]:=v0 else pre[i]:=0;
end;
mark[v0]:=true;
repeat {
每循環一次加入一個離1集合最近的結點並調整其他結點的參數
}
min:=maxint; u:=0; {u
記錄離1集合最近的結點
}
for i:=1 to n do
if (not mark[i]) and (d[i]<min) then begin
u:=i; min:=d[i];
end;
if u<>0 then begin
mark[u]:=true;
for i:=1 to n do
if (not mark[i]) and (a[u,i]+d[u]<d[i]) then begin
d[i]:=a[u,i]+d[u];
pre[i]:=u;
end;
end;
until u=0;
end;
3.
計算圖的傳遞閉包

Procedure Longlink;
Var
T:array[1..maxn,1..maxn] of boolean;
Begin
Fillchar(t,sizeof(t),false);
For k:=1 to n do
For I:=1 to n do
For j:=1 to n do T[I,j]:=t[I,j] or (t[I,k] and t[k,j]);
End;

4
.無向圖的連通分量
A.
深度優先
procedure dfs ( now,color: integer);
begin
for i:=1 to n do
if a[now,i] and c[i]=0 then begin {
對結點I染色}
c[i]:=color;
dfs(I,color);
end;
end;
B
寬度優先(種子染色法)


5
.關鍵路徑
幾個定義: 頂點1爲源點,n爲匯點。
a.
頂點事件最早發生時間Ve[j], Ve [j] = max{ Ve [j] + w[I,j] },其中Ve (1) = 0;
b.
頂點事件最晚發生時間 Vl[j], Vl [j] = min{ Vl[j] – w[I,j] },其中
Vl(n) = Ve(n);
c.
邊活動最早開始時間 Ee[I], 若邊I<j,k>表示,則
Ee[I] = Ve[j];
d.
邊活動最晚開始時間 El[I], 若邊I<j,k>表示,則
El[I] = Vl[k] – w[j,k];
Ee[j] = El[j] ,則活動j爲關鍵活動,由關鍵活動組成的路徑爲關鍵路徑。

求解方法:
a.
從源點起topsort,判斷是否有迴路並計算Ve;
b.
從匯點起topsort,
Vl;
c.
Ee
El;

6
.拓撲排序

找入度爲0的點,刪去與其相連的所有邊,不斷重複這一過程。
尋找一數列,其中任意連續p項之和爲正,任意q 項之和爲負,若不存在則輸出NO.

7.
迴路問題

Euler
迴路(DFS)
定義:經過圖的每條邊僅一次的迴路。(充要條件:圖連同且無奇點)

Hamilton
迴路
定義:經過圖的每個頂點僅一次的迴路。
一筆畫
充要條件:圖連通且奇點個數爲0個或2個。
9
.判斷圖中是否有負權迴路 Bellman-ford 算法
x[I],y[I],t[I]
分別表示第I條邊的起點,終點和權。共n個結點和m條邊。
procedure bellman-ford
begin
for I:=0 to n-1 do d[I]:=+infinitive;
d[0]:=0;
for I:=1 to n-1 do
for j:=1 to m do {
枚舉每一條邊}
if d[x[j]]+t[j]<d[y[j]] then d[y[j]]:=d[x[j]]+t[j];
for I:=1 to m do
if d[x[j]]+t[j]<d[y[j]] then return false else return true;
end;
10
.第n最短路徑問題

*
第二最短路徑:每舉最短路徑上的每條邊,每次刪除一條,然後求新圖的最短路徑,取這些路徑中最短的一條即爲第二最短路徑。
*
同理,第n最短路徑可在求解第n-1最短路徑的基礎上求解。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章