You are on page 1of 9

1.

Chng trnh bi ton Knapsack 0-1 gii bng phng php quy hoch
ng
Program The_Bag;
uses crt;
const
max = 100;
var
W, V: Array[1..max] of word;
F: array[0..max, 0..max] of word;
n, M: word;
fin,fout:text;
procedure Nhap_DL;
var
i: Integer;
begin
Assign(fin, 'D:\chtr\BAG.INP'); Reset(fin);
Assign(fout, 'd:\chtr\BAG.OUT'); Rewrite(fout);
ReadLn(fin,n, M);
for i := 1 to n do ReadLn(fin,W[i], V[i]);
end;
procedure Lap_Bang_PA;
var i, j: Integer;
begin
FillChar(F, SizeOf(F), 0);
for i := 1 to n do
for j := 0 to M do
if f[i,j]<=m-w[i] then
begin
F[i, j] := F[i - 1, j];

if (j >= W[i]) and (F[i, j] < F[i - 1, j - W[i]] + V[i]) then


F[i, j] := F[i - 1, j - W[i]] + V[i];
end;
end;
procedure Truy_viet;
begin
WriteLn(fout,'Tong gia tri toi da cua cac vat trong tui:',F[n, M]);
Writeln(fout,'Cac vat duoc chon:');
while n <> 0 do
begin
if F[n, M] <> F[n - 1, M] then
begin
Write(fout,n, ' ');
M := M - W[n];
end;
Dec(n);
end;
Close(fin); Close(fout);
readln
end;
BEGIN
Nhap_DL;
Lap_Bang_PA;
Truy_viet;
END.
2. Chng trnh gii bi ton Knapsack bng phng php tham lam

Program Greedy3;
type hang = record
ten:byte;
tluong,gtri: word;
dgia: real;
end;
nhan = record
ten:byte;
sluong: byte;
end;
t_hang = array[1..100] of hang;
var sp:t_hang;
tui: word;
fin,fout: text;
xd: array[1..100] of nhan;
n,i,j: integer;
procedure nhap;
var gt,tl: word;
t: byte;
tg:hang;
begin
assign(fin,'d:\chtr\bag.inp');
reset(fin);
readln(fin,n,tui);
for i:= 1 to n do
begin
t:=i;
sp[i].ten:=t;
readln(fin,tl,gt);

sp[i].tluong:=tl;
sp[i].gtri:=gt;
sp[i].dgia:=sp[i].gtri/sp[i].tluong;
end;
for i:=1 to n-1 do
for j:=i+1 to n do
if (sp[i].dgia<sp[j].dgia) then
begin
tg:=sp[i];
sp[i]:=sp[j];
sp[j]:=tg;
end;
close(fin);
end;
procedure thamlam;
var sl, tlduoc,tltui : integer;
gtduoc:real;
begin
i:=1; j:=1; tltui:=tui;
gtduoc:=0; tlduoc:=0;
assign(fout,'d:\chtr\bag.out');
rewrite(fout);
while i<=n do
begin
if sp[i].tluong <= tltui then
begin
sl:= tltui div sp[i].tluong;
tlduoc:=tlduoc+sl*sp[i].tluong;
gtduoc:=gtduoc+ sl* sp[i].gtri;

tltui:=tltui - sl*sp[i].tluong;
xd[j].ten:=sp[i].ten;
xd[j].sluong:=sl; j:=j+1;
end;
i:=i+1;
end;
writeln(fout,' trong luong tui la ', tui);
writeln(fout,' trong luong lay duoc la ', tlduoc);
writeln(fout,' gia tri toi da cua cac san pham trong tui:',gtduoc:3:3);
i:=1;
writeln(fout,'cac san pham duoc chon:');
while i<j do
begin
writeln(fout,'san pham:',xd[i].ten,' so luong duoc chon:',xd[i].sluong);
i:=i+1;
end;
close(fout);
readln;
end;
BEGIN
nhap;
thamlam;
END.

3. Chng trnh gii bi ton xp ba l 0-1 bng gii thut di truyn


Program The_Bag_GA;
{ Thut ton di truyn cho bi ton xp ba l 0-1}
uses crt;
const
max = 100;
type Bool=array[1..max]of boolean;
vecto=array[1..max]of word;
var
W, V: Array[1..max] of word;
F: array[0..max, 0..max] of word;
n, M: word;
fin,fout:text;
procedure Nhap_DL;
var
i: Integer;
begin
Assign(fin, 'D:\chtr\BAG.INP'); Reset(fin);
Assign(fout, 'd:\chtr\BAG.OUT'); Rewrite(fout);
ReadLn(fin,n, M);
for i := 1 to n do ReadLn(fin,W[i], V[i]);
end;
procedure sinh_ngau_nhien(var B:Bool; dimB:byte);
var i,j:byte;
begin
randomize;
for i:=1 to dimB do
begin
j:=random(dimB);

B[j]:=not B[j];
end;
end;
function f_mtieu(x:vecto; dimx:byte):word;
var i,j:byte; fmt,Wsum:word;
begin
for i:=1 to dimx do
begin
Wsum:=Wsum+x[i]*w[i];
fmt:=x[i]*v[i];
if (Wsum>M) then fmt:=fmt-7*(Wsum-M);
end;
f_mtieu:=fmt;
end;
procedure lai_cheo_1diem(ct1,ct2:bool; var ct3:bool; dim,cp:byte);
var i:byte;
begin
for i:=1 to dim do
if i>=cp then ct3[i]:=ct1[i] else ct3[i]:=ct2[i];
end;
procedure GA;
var
i, j: Integer; B1,B2,B:bool; dim,cp:byte
begin
sinh_ngau_nhien(B1,n);
sinh_ngau_nhien(B2,n);
lai_cheo_1diem(B1,B2,B,dim,cp);
FillChar(F, SizeOf(F), 0);
for i := 1 to n do

for j := 0 to M do
if f[i,j]<=m-w[i] then
begin
F[i, j] := F[i - 1, j];
if (j >= W[i]) and (F[i, j] < F[i - 1, j - W[i]] + V[i]) then
F[i, j] := F[i - 1, j - W[i]] + V[i];
end;
end;
procedure Tim;
var i,d:byte; x:vecto;
begin
d:=0;
WriteLn(fout,'Tong gia tri toi da cua cac vat trong tui:',F[n, M]);
Writeln(fout,'Cac vat duoc chon:');
while n <> 0 do
begin
if F[n, M] <> F[n - 1, M] then
begin
d:=d+1;
x[d]:=n;
M := M - W[n];
end;
Dec(n);
end;
for i:=d downto 1 do Write(fout,x[i], ' ');
Close(fin); Close(fout);
end;
begin
clrscr;

Nhap_DL;
GA;
Tim;
readln
end.

You might also like