分支定界法
来源:互联网 发布:js注释快捷键无效 编辑:程序博客网 时间:2024/05/09 14:30
原文见:http://it.pjschool.com.cn/Article/ArticleShow.asp?ArticleID=231
例1:设有A,B,C,D,E 5人从事j1,j2,j3,j4,j5 5项工作每人只能从事一项,它们的
效益表如下:
j1 j2 j3 j4 j5 A 13 11 10 4 7 B 13 10 10 8 5 C 5 9 7 7 4 D 15 12 10 11 5 E 10 11 8 8 4求最佳安排,使效益最高?
原文代码重写如下,希望增加点可读性。
program PlanJob;
const MAX_SIZE = 20;
type
TIntArray = array[1..MAX_SIZE] of Integer;
PNode = ^Node;
Node = record
Job2Man: TIntArray; // Job2Man[n] = m, job-n assign to person-m
Man2Job: TIntArray; // Man2Job[n] = m, person-n assign to job-m
UpperVal: Integer; // upper value
JobsDep: Integer; // jobs decided, as search depth
Next: PNode;
end;
var
CurNode: PNode; // Current node
NewNode: PNode; // New branch node
DelNode: PNode; // for delete
GoalNode: PNode; // the goal
GoalMaxVal: Integer; // goal max value
CurMan, CurJob: Integer; // Current Man and Job of current Node
Size: Integer; // Person number, also task number
Values: array[1..MAX_SIZE, 1..MAX_SIZE] of Integer;
function CalUpperValue(ANode: PNode): Integer;
var
Res: Integer;
Man, Job: Integer;
JobMaxVal: Integer;
begin
Res := 0;
with ANode^ do
begin
for Job := 1 to Size do
begin
if Job <= JobsDep then
begin
Man := Job2Man[Job];
Res := Res + Values[Man, Job];
Continue;
end;
// else find the max value for Job
JobMaxVal := 0;
for Man := 1 to Size do
begin
if (JobMaxVal < Values[Man,Job]) and (Man2Job[Man] = 0) then
JobMaxVal := Values[Man,Job];
end;
Res := Res + JobMaxVal;
end; // for Job
end; // with ANode^
CalUpperValue := Res;
end;
function InitNode(): PNode;
var
Man, Job: Integer;
FInput: Text;
Res: PNode;
begin
Assign(FInput, 'input.txt');
Reset(FInput);
Read(FInput, Size);
Readln(FInput);
for Man := 1 to Size do
begin
for Job := 1 to Size do
Read(FInput, Values[Man,Job]);
Readln(FInput);
end;
New(Res);
with Res^ do
begin
for Man := 1 to Size do
begin
Man2Job[Man] := 0;
Job2Man[Man] := 0;
end;
JobsDep := 0;
UpperVal := CalUpperValue(Res);
Next := nil;
end;
InitNode := Res;
end;
procedure Insert(ANode: PNode; From: PNode);
var
PrevNode, NextNode: PNode;
begin
NextNode := From;
repeat
PrevNode := NextNode;
NextNode := PrevNode^.Next;
until (NextNode = nil) or (ANode^.UpperVal >= NextNode^.UpperVal);
PrevNode^.Next := ANode;
ANode^.Next := NextNode;
end;
procedure PrintNode(ANode: PNode);
var
Man, Job: Integer;
AllJobAssigned: Boolean;
begin
AllJobAssigned := true;
for Job := 1 to Size do
begin
Man := ANode^.Job2Man[Job];
if 0 <> Man then
Writeln('Job ', Job, ' assign to man ',
Man, ', value ', Values[Man, Job])
else
AllJobAssigned := false;
end;
Writeln;
if AllJobAssigned then
Writeln('Value = ', ANode^.UpperVal)
else
Writeln('UpperVal = ', ANode^.UpperVal);
end;
begin
CurNode := InitNode();
GoalMaxVal := 0;
repeat
CurJob := CurNode^.JobsDep + 1;
for CurMan := 1 to Size do
begin
if CurNode^.Man2Job[CurMan] <> 0 then
Continue;
// New search branch
New(NewNode);
NewNode^ := CurNode^;
NewNode^.JobsDep := CurJob;
NewNode^.Man2Job[CurMan] := CurJob;
NewNode^.Job2Man[CurJob] := CurMan;
NewNode^.UpperVal := CalUpperValue(NewNode);
if NewNode^.UpperVal < GoalMaxVal then
Dispose(NewNode) // discard this branch if smaller than limit
else
Insert(NewNode, CurNode);
if CurJob < Size then Continue; // for CurMan
// all job assigned, update goal
if NewNode^.UpperVal > GoalMaxVal then
begin
GoalNode := NewNode;
GoalMaxVal := GoalNode^.UpperVal
end; // if
end; // for CurMan
DelNode := CurNode;
CurNode := CurNode^.Next;
Dispose(DelNode);
until (CurNode^.UpperVal <= GoalMaxVal)
or (CurNode = nil); // end of repeat
PrintNode(GoalNode);
Readln;
end.
5
13 11 10 4 7
13 10 10 8 5
5 9 7 7 4
15 12 10 11 5
10 11 8 8 4
output:
Job 1 assign to man 4, value 15
Job 2 assign to man 5, value 11
Job 3 assign to man 2, value 10
Job 4 assign to man 3, value 7
Job 5 assign to man 1, value 7
Value = 50
如果扩展为10*10,
input.txt:
10
13 11 10 4 7 13 11 10 4 7
13 10 10 8 5 13 10 10 8 5
5 9 7 7 4 5 9 7 7 4
15 12 10 11 5 15 12 10 11 5
10 11 8 8 4 10 11 8 8 4
13 11 10 4 7 13 11 10 4 7
13 10 10 8 5 13 10 10 8 5
5 9 7 7 4 5 9 7 7 4
15 12 10 11 5 15 12 10 11 5
10 11 8 8 4 10 11 8 8 4
运行约两分钟。
output :
Job 1 assign to man 9, value 15
Job 2 assign to man 5, value 11
Job 3 assign to man 2, value 10
Job 4 assign to man 8, value 7
Job 5 assign to man 6, value 7
Job 6 assign to man 4, value 15
Job 7 assign to man 10, value 11
Job 8 assign to man 7, value 10
Job 9 assign to man 3, value 7
Job 10 assign to man 1, value 7
Value = 100
- 分支定界法
- 分支定界法(
- 分支定界法
- 分支定界法
- 分支定界法总结
- 分支定界法 广度搜索定界 深度搜索定界
- (转)分支定界法
- 回溯法和分支定界法
- 分支定界法求解整数线性规划问题
- 递归分支定界
- 分支定界之深度搜索定界
- 用分支定界(branch and bound)法解装箱问题
- 0/1背包问题的分支定界法算法
- 五大常用算法之分支定界法
- 用分支定界(branch and bound)法解装箱问题
- 分支定界法——0-1背包问题
- 分支定界法——旅行商(TSP)问题
- FIFO分支定界算法初探
- 常用类之四---并查集(Union-Find Sets)
- 配置SQL Server 2000复制和同步
- 获取数据库中的所有表
- 专家教您如何在C语言中巧用正则表达式
- 后ERP时代的信息一体化平台软件:SRP介绍
- 分支定界法
- TCP/IP详解笔记 (1.概述
- 活得象个人样
- CenFun Music Player
- 如何用SQLDMO在ASP.NET页面下实现数据库的备份与恢复
- [转载]HTTP协议的运作方式
- 故障诊断概览
- 会计科目的意义
- 了解 Tapestry,第 1 部分