ACM演算法集5

2021-04-30 14:09:54 字數 2618 閱讀 2255

acm演算法集5

五、資料結構相關演算法

1.鍊錶的定位函式

loc(i:integer):pointer;

procedure loc(l:linklist; i:integer):pointer;

var p:pointer;

j:integer;

begin

p:=l.head; j:=0;

if (i>=1) and (i<=l.len) then

while j

.單鏈表的插入操作

procedure insert(l:linklist; i:integer; x:datatype);

var p,q:pointer;

begin

p:=loc(l,i);

new(q);

q^.data:=x;

q^.next:=p^.next;

p^.next:=q;

inc(l.len);

end;

3

.單鏈表的刪除操作

procedure delete(l:linklist; i:integer);

var p,q:pointer;

begin

p:=loc(l,i-1);

q:=p^.next;

p^.next:=q^.next;

dispose(q);

dec(l.len);

end;

4.雙鏈表的插入操作(插入新結點q)

p:=loc(l,i);

new(q);

q^.data:=x;

q^.pre:=p;

q^.next:=p^.next;

p^.next:=q;

q^.next^.pre:=q;

5

.雙鏈表的刪除操作

p:=loc(l,i);

p^.pre^.next:=p^.next;

p^.next^.pre:=p^.pre;

dispose(p);

關鍵路徑(最長路經):

var a,b:array [1..10,1..10] of integer;

n,last,out:integer;

q,c:array [1..10] of integer;

o:set of 1..10;

procedure init;

var i,j:integer;

begin

readln(n);

for i:=1 to n do

for j:=1 to n do

read(a[i,j]);

last:=0;

o:=; out:=0;

b:=a;

end;

procedure sort;

var i,j:integer;

p:boolean;

begin

while out<>n do begin

for i:=1 to n do

if not (i in o) then begin

p:=true;

for j:=1 to n do

if a[j,i]=1 then begin

p:=false;

break;

end;

if p then begin

inc(last);

q[last]:=i;

inc(out);

o:=o+[i];

fillchar(a[i],sizeof(a[i]),0);

end;

end;

end;

end;

procedure work_1;

var i,j,t,k:integer;

begin

a:=b; c[1]:=0;

for i:=1 to n do begin

k:=0;

for j:=1 to i-1 do

if (a[q[j],q[i]]>0) and (a[q[j],q[i]]+c[q[j]]>k)

then k:=a[q[j],q[i]]+c[q[j]];

c[q[i]]:=k;

end;

end;

procedure work_2;

var i,j,k:integer;

begin

writeln(q[n]);

for i:=n-1 downto 1 do begin

k:=maxint;

for j:=i+1 to n do

if (a[q[i],q[j]]>0) and (c[q[j]]-a[q[i],q[j]]

ACM各類題集

基礎演算法 列舉 poj1573 flip game ac poj2965 the pilots brothers refrigerator 貪心 遞推 模擬 分治 二分 三分 搜尋 動態規劃 dp題集 數學 博弈類 poj1067ac hdu2516ac 斐波那契數列 acdream 無恥的出題人...

ACM之路 5 最短路演算法 Dijkstra演算法

簡介 dijkstra演算法是一種單源路徑演算法。時間複雜度為 o n 2 比floyd演算法 o n 3 快很多。當然,dijkstra演算法可以用堆優化後,演算法複雜度成了 o m n logn 複雜度更底了。本文只講解dijkstra的簡單演算法。問題 給予n個城市和m條道路,求從城市1到城市...

ACM實驗七 ACM程式設計基礎(5)

實驗專案 acm程式設計基礎 5 實驗目的 掌握c 程式設計基礎。實驗要求 使用vc 6.0實現實驗要求。實驗內容 1.編寫乙個函式實現如下功能 輸入 7 輸出 1 8 14 19 23 26 28 2 9 15 20 24 27 3 10 16 21 25 4 11 17 22 5 12 18 6...