主题:谁能帮下忙把下面的pascal转成C或C++,多谢了!
例1:设有5人从事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.
效益表如下:
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.