300大分等你拿!(300分)

  • 主题发起人 主题发起人 lctuchen
  • 开始时间 开始时间
真是个急性子,都跟你说了效率不好,还想改改.先拿去用吧,不过,依你这么急的性子,肯定等不急.
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils, Controls;
const
MaxRoom = 30;
MaxBed = 50;
MaxPatient = 100;
type
TPatient = record
RoomTime: Integer;
BedTime: Integer
end;

TNode = record
Patient: Integer;
do
ne: set of 1..MaxPatient;
RoomLastTime: array [1..MaxRoom] of Integer;
Room: Integer;
UseRoomTime: Integer;
MinRoom: Integer;
BedLastTime: array [1..MaxBed] of Integer;
Bed: Integer;
UseBedTime: Integer;
MinBed: Integer;
LastTime: Integer
end;

var
Stack: array [1..MaxPatient] of TNode;
Top: Integer;
NewNode: TNode;
LastTime: Integer;
Best: array [1..MaxPatient+1] of TNode;
i: Integer;
RoomNum: 1..MaxRoom;
RoomReadyTime: Integer;
BedNum: 1..MaxBed;
BedReadyTime: Integer;
RoomToBedTime: Integer;
PatientNum: 1..MaxPatient;
Patients: array [1..MaxPatient] of Tpatient;
procedure InputParams;
var
i: Integer;
begin
Write('手术室数目: ');
ReadLn(RoomNum);
Write('恢复床位数目: ');
ReadLn(BedNum);
Write('手术室准备时间: ');
ReadLn(RoomReadyTime);
Write('恢复床位准备时间: ');
ReadLn(BedReadyTime);
Write('移送病人时间: ');
ReadLn(RoomToBedTime);
Write('病人数目: ');
ReadLn(PatientNum);
for i:=1 to PatientNumdo
begin
Write('病人', i:2, '手术时间: ');
ReadLn(Patients.RoomTime);
Write('病人', i:2, '恢复时间: ');
ReadLn(Patients.BedTime)
end
end;

procedure Init;
var
i: Integer;
begin
LastTime:=MaxInt;
Top:=1;
Stack[1].Patient:=0;
Stack[1].Done:=[];
for i:=1 to RoomNumdo
Stack[1].RoomLastTime:=0;
Stack[1].Room:=0;
Stack[1].UseRoomTime:=0;
Stack[1].MinRoom:=1;
for i:=1 to BedNumdo
Stack[1].BedLastTime:=0;
Stack[1].Bed:=0;
Stack[1].UseBedTime:=0;
Stack[1].MinBed:=1;
Stack[1].LastTime:=0
end;

function GetMinRoom(Node: TNode): Integer;
var
i: Integer;
begin
Result:=1;
for i:=2 to RoomNumdo
if Node.RoomLastTime<Node.RoomLastTime[Result] then
Result:=i
end;

function GetMinBed(Node: TNode): Integer;
var
i: Integer;
begin
Result:=1;
for i:=2 to BedNumdo
if Node.BedLastTime<Node.BedLastTime[Result] then
Result:=i
end;

function MaxPatientBedTime(Node: TNode): Integer;
var
i: Integer;
begin
Result:=0;
for i:=1 to PatientNumdo
if not (i in Node.Done) then
if Patients.BedTime>Result then
Result:=Patients.BedTime
end;

procedure Print;
type
TTimeBand = record
StartTime: Integer;
EndTime: Integer;
User: Integer
end;
var
List: array [1..MaxBed] of TTimeBand;
Len: Integer;
i, j, k: Integer;
t: TTimeBand;
function FormatTimeStr(Time: Integer): string;
var
h, m, s, ms: Word;
begin
h:=7;
m:=0;
s:=0;
ms:=0;
m:=m+Time;
h:=h+m div 60;
m:=m mod 60;
Result:=TimeToStr(EncodeTime(h, m, s, ms))
end;

begin
for i:=1 to RoomNumdo
begin
Len:=0;
FillChar(List, SizeOf(List), 0);
for j:=2 to PatientNum+1do
if Best[j].Room=i then
begin
Inc(Len);
List[Len].StartTime:=Best[j].UseRoomTime;
List[Len].EndTime:=List[Len].StartTime+RoomReadyTime;
List[Len].User:=0;
Inc(Len);
List[Len].StartTime:=List[Len-1].EndTime;
List[Len].EndTime:=Best[j].RoomLastTime;
List[Len].User:=Best[j-1].Patient
end;
for j:=1 to Len-1do
for k:=j+1 to Lendo
if List[j].StartTime>List[k].StartTime then
begin
t:=List[j];
List[j]:=List[k];
List[k]:=t
end;
if Len>0 then
begin
WriteLn(i:2, '号手术室: ');
for j:=1 to Lendo
begin
Write('从', FormatTimeStr(List[j].StartTime), '到', FormatTimeStr(List[j].EndTime), ': ');
if List[j].User=0 then
WriteLn('准备')
else
WriteLn(List[j].User, '号病人')
end;
ReadLn
end
end;
for i:=1 to BedNumdo
begin
Len:=0;
FillChar(List, SizeOf(List), 0);
for j:=2 to PatientNum+1do
if Best[j].Bed=i then
begin
Inc(Len);
List[Len].StartTime:=Best[j].UseBedTime;
List[Len].EndTime:=List[Len].StartTime+BedReadyTime;
List[Len].User:=0;
Inc(Len);
List[Len].StartTime:=List[Len-1].EndTime;
List[Len].EndTime:=Best[j].BedLastTime;
List[Len].User:=Best[j-1].Patient
end;
for j:=1 to Len-1do
for k:=j+1 to Lendo
if List[j].StartTime>List[k].StartTime then
begin
t:=List[j];
List[j]:=List[k];
List[k]:=t
end;
if Len>0 then
begin
WriteLn(i:2, '号恢复床: ');
for j:=1 to Lendo
begin
Write('从', FormatTimeStr(List[j].StartTime), '到', FormatTimeStr(List[j].EndTime), ' ');
if List[j].User=0 then
WriteLn('准备')
else
WriteLn(List[j].User, '号病人')
end;
ReadLn
end
end
end;

begin
InputParams;
Init;
while Top>0do
begin
while Stack[Top].Patient<PatientNumdo
begin
Inc(Stack[Top].Patient);
if not (Stack[Top].Patient in Stack[Top].Done) then
begin
NewNode:=Stack[Top];
NewNode.Patient:=0;
NewNode.Done:=Stack[Top].Done+[Stack[Top].Patient];
NewNode.Room:=Stack[Top].MinRoom;
NewNode.UseRoomTime:=Stack[Top].RoomLastTime[NewNode.Room];
NewNode.RoomLastTime[NewNode.Room]:=NewNode.UseRoomTime
+RoomReadyTime+Patients[Stack[Top].Patient].RoomTime;
if NewNode.RoomLastTime[NewNode.Room]<LastTime then
begin
NewNode.MinRoom:=GetMinRoom(NewNode);
//NewNode.Bed:=GetBed(NewNode);
NewNode.Bed:=Stack[Top].MinBed;
if Stack[Top].BedLastTime[NewNode.Bed]<=(NewNode.RoomLastTime[NewNode.Room]+RoomToBedTime-BedReadyTime) then
NewNode.UseBedTime:=NewNode.RoomLastTime[NewNode.Room]+RoomToBedTime-BedReadyTime
else
NewNode.UseBedTime:=Stack[Top].BedLastTime[NewNode.Bed];
NewNode.BedLastTime[NewNode.Bed]:=NewNode.UseBedTime
+BedReadyTime+Patients[Stack[Top].Patient].BedTime;
NewNode.MinBed:=GetMinBed(NewNode);
if NewNode.BedLastTime[NewNode.Bed]>NewNode.LastTime then
NewNode.LastTime:=NewNode.BedLastTime[NewNode.Bed];
if (NewNode.LastTime<LastTime) and (NewNode.BedLastTime[NewNode.MinBed]+MaxPatientBedTime(NewNode)<LastTime) then
begin
if Top=PatientNum then
begin
LastTime:=NewNode.LastTime;
for i:=1 to PatientNumdo
Best:=Stack;
Best[PatientNum+1]:=NewNode;
WriteLn('总时间已减少为: ', LastTime, '分钟')
end
else
begin
Inc(Top);
Stack[Top]:=NewNode
end
end
end
end
end;
Dec(Top)
end;
if LastTime<MaxInt then
Print
else
begin
WriteLn('无解');
ReadLn
end
end.
 
后退
顶部