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

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.

• 本文已收录于以下专栏：

## Delphi7高级应用开发随书源码

• 2003年04月30日 00:00
• 676KB
• 下载

## 分支定界法

• jq0123
• 2006年05月30日 11:11
• 8477

## 分支定界-------ACM

• 2011年06月11日 23:50
• 406KB
• 下载

## 回溯法和分支定界法

• Mind_V
• 2017年06月26日 16:22
• 329

## 分支定界法

[cpp] view plain copy print?#pragma once  #include   #include   #include   #include   #include   #in...

## 分支定界算法的MATLAB实现

• 2017年10月26日 16:20
• 1.51MB
• 下载

## 分支定界求解装箱问题

• 2011年10月28日 10:42
• 2KB
• 下载

## 0/1背包问题 蛮力/动规/回溯/分支定界

举报原因： 您举报文章：分支定界之深度搜索定界 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)