# 分支定界之深度搜索定界

3632人阅读 评论(0)

3  12
11 7 5  5  4 7
11 7 5  5  4 7

11 11 4
7 7 7 5
7 5 5 5 4
Time=26

{
http://it.pjschool.com.cn/Article/ArticleShow.asp?ArticleID=231

2. 2 深度搜索定界

3  6
11 7 5  5  4 7

7 7

5 5 4

11

time=14

}
program Jobs;

const
MAXN = 100;
MAXM = 1000;

var
JobReqTime: array[1..MAXM] of Integer;

// jobs count on processor-I
JobNums, BestJobNums: array[1..MAXN] of Integer;

// TimeBlocks[Proc, Seq]: the Seq block time of Proc
// TimeBlocks[2, 4] = 12 means processor-2's 4th job requires 12 time.
TimeBlocks, BestTmBlocks: array[1..MAXN, 1..MAXM] of Integer;

MaxTimeTotal: Integer;        // = ProcUsedTime[1]
ProcUsedTime: array[1..MAXN] of Integer;
JobDone: array[1..MAXM] of Boolean;
Least: Integer;
I, J: Integer;
FreeProc: Integer;
ProcNum, JobNum: Integer;     // Number of Processors and Jobs
Rest: Integer;

procedure print;
var
I, J: Integer;
begin
for I := 1 to ProcNum do
begin
for J := 1 to BestJobNums[I] do
Write(BestTmBlocks[I, J], ' ');
Writeln;
end ;
Writeln('Time=', MaxTimeTotal);
end;

var
F: Text;
I, J: Integer;
temp: Integer;
begin
Assign(F, 'input.txt');
Reset(F);
Rest := 0;
for I := 1 to JobNum do
begin
Inc(Rest, JobReqTime[I]);
end;
Close(F);

Least := Trunc(Rest div ProcNum); { 确定下界 }

// Sort requested times, decsending.
for I := 1 to JobNum - 1 do
for J := I + 1 to JobNum do
if JobReqTime[I] < JobReqTime[J] then
begin
temp := JobReqTime[I];
JobReqTime[I] := JobReqTime[J];
JobReqTime[J] := temp;
end;
end;

{
Processor can reject job if required time too much.
1. ProcUsedTime[] should less than MaxTimeTotal
2. ProcUsedTime[] should not ascend
}
function ProcRejectJob(AProc, AJob: Integer): Boolean;
var
NewUsedTime: Integer;
begin
NewUsedTime := ProcUsedTime[AProc] + JobReqTime[AJob];
if 1 = AProc then
ProcRejectJob := NewUsedTime >= MaxTimeTotal
else
ProcRejectJob := NewUsedTime > ProcUsedTime[AProc - 1];
end;

begin
Inc(JobNums[AProc]);
TimeBlocks[AProc, JobNums[AProc]] := JobReqTime[AJob];
Inc(ProcUsedTime[AProc], JobReqTime[AJob]);
Dec(Rest, JobReqTime[AJob]);
end;

procedure RemoveJobFromProc(AJob, AProc: Integer);
begin
Dec(JobNums[AProc]);
Dec(ProcUsedTime[AProc], JobReqTime[AJob]);
Inc(Rest, JobReqTime[AJob]);
end;

{ 从AJob..JobNum中选取作业放在处理机AProc上 }
procedure SearchFrom(AJob, AProc: Integer);
var
I: Integer;
begin
if MaxTimeTotal = Least then Exit;

for I := AJob to JobNum do
begin
if JobDone[I] then Continue;
if ProcRejectJob(AProc, I) then Continue;

JobDone[I] := True;
// try to add more jobs
SearchFrom(I + 1, AProc);
RemoveJobFromProc(I, AProc);
JobDone[I] := False;
end;  // for I

if Rest = 0 then
begin
BestJobNums := JobNums;
BestTmBlocks := TimeBlocks;
MaxTimeTotal := ProcUsedTime[1];  // ProcUsedTime[1] is always the largest
end
else if Rest > (ProcNum - AProc) * ProcUsedTime[AProc] then
Exit       // stop this search branch
else if AProc < ProcNum then
SearchFrom(1, AProc + 1);

end;

begin

{ 确定上界 }
FillChar(ProcUsedTime, SizeOf(ProcUsedTime), 0);
FillChar(BestTmBlocks, SizeOf(BestTmBlocks), 0);
FillChar(BestJobNums, SizeOf(BestJobNums), 0);
for I := 1 to JobNum do
begin
// find the most free proccessor
FreeProc := 1;
for J := 2 to ProcNum do
if ProcUsedTime[J] < ProcUsedTime[FreeProc] then FreeProc := J;

// assign job-I to most free proc
ProcUsedTime[FreeProc] := ProcUsedTime[FreeProc] + JobReqTime[I];
BestJobNums[FreeProc] := BestJobNums[FreeProc] + 1;
BestTmBlocks[FreeProc, BestJobNums[FreeProc]] := JobReqTime[I];
end;

MaxTimeTotal := 0;
for I := 1 to ProcNum do
if ProcUsedTime[I] > MaxTimeTotal then
MaxTimeTotal := ProcUsedTime[I];

// start to search
FillChar(JobDone, SizeOf(JobDone), False);
FillChar(ProcUsedTime, SizeOf(ProcUsedTime), 0);
FillChar(TimeBlocks, SizeOf(TimeBlocks), 0);
FillChar(JobNums, SizeOf(JobNums), 0);
SearchFrom(1, 1);

print;
end.

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：1054643次
• 积分：14997
• 等级：
• 排名：第752名
• 原创：375篇
• 转载：49篇
• 译文：19篇
• 评论：394条
文章分类
文章存档
评论排行
最新评论