#include "h1.h"
void timeQuickSort(Class c[], int n, int flag){ //快排,当flag==1时,按finishTime排序;当flag==0时,按
//startTime排序
qSort(c, 1, n, flag);
}
void qSort(Class c[], int low, int high, int flag){
if(low < high){
int pivotKey = partition(c, low, high, flag);
qSort(c, low, pivotKey-1, flag);
qSort(c, pivotKey+1, high, flag);
}
}
int partition(Class c[], int low, int high, int flag){
Class pivot = c[low];
while(low < high){
if(flag == 1){
while(low<high && c[high].finishTime>=pivot.finishTime){
high--;
}
}
else{
while(low<high && c[high].startTime>=pivot.startTime){
high--;
}
}
c[low] = c[high];
if(flag == 1){
while(low<high && c[low].finishTime<=pivot.finishTime){
low++;
}
}
else{
while(low<high && c[low].startTime<=pivot.startTime){
low++;
}
}
c[high] = c[low];
}
c[low] = pivot;
return low;
}
void classroomDistributionGreedy(Class c[], int n, int classroom[][MAXSIZE]){
int i, j, k, flag; //i denotes the i'th classroom, j denotes the j'th class.
flag = 1; //A flag to check whether a new classroom is needed.
k = 1; //The k'th class of a very classroom, initial value is 1.
for(i=1; i<=n; i++){
k=1;
if( flag ){
flag = 0;
for(j=1; j<=n; j++){
if( !c[j].flag && classroom[i][1]==0 ){ //Find the first class of a new classroom.
classroom[i][k] = j;
c[j].flag = 1; //The j'th class is distributed.
k++;
flag = 1;
}
else if(!c[j].flag && (c[classroom[i][k-1]].finishTime<=c[j].startTime)){
classroom[i][k] = j;
c[j].flag = 1;
k++;
flag = 1;
}
}
}
else{
break;
}
}
}
void printCourseDistributed(int classroom[][MAXSIZE]){
int i, j, flag;
flag = 1;
for(i=1; i<=6; i++){
if( flag ){
flag = 0;
printf("The courses of the %d 'th classroom:\n", i);
for(j=1; j<=6; j++){
if(classroom[i][j] == 0){
break;
}
else{
printf("%d ", classroom[i][j]);
flag = 1;
}
}
printf("\n");
}
else{
break;
}
}
}
void classroomDistribution(Class c[], int n, int classroom[][MAXSIZE]){
int i, k; //i denotes the i'th course. k denotes the k's classroom.
int position[MAXSIZE] = {0}; //The flag to record the last course's position added to each classroom.
for(i=1; i<=n; i++){
k = 1;
while(c[classroom[k][position[k]]].finishTime>c[i].startTime){
k++;
}
flag[k]++;
classroom[k][position[k]] = i;
}
}
int main(){
int i;
Class c[7];
int classroom[MAXSIZE][MAXSIZE] = {0};
for(i=0; i<=6; i++){
c[i].flag = 0;
}
c[6].startTime=1; c[6].finishTime=3;
c[5].startTime=2; c[5].finishTime=4;
c[4].startTime=3; c[4].finishTime=5;
c[3].startTime=4; c[3].finishTime=6;
c[2].startTime=5; c[2].finishTime=7;
c[1].startTime=6; c[1].finishTime=8;
c[0].startTime=0; c[0].finishTime=0;//Initiate class values.
//timeQuickSort(c, 6, 0); //Sort according to starting time of each course.
//classroomDistribution(c, 6, classroom);//Direct strategy.
timeQuickSort(c, 6, 1);//Sort according to finishing time of each course.
classroomDistributionGreedy(c, 6, classroom);//Greedy strategy
printCourseDistributed(classroom);
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
教室课程调度问题的两种解法(区间着色问题)
共40个文件
tlog:18个
pdb:2个
ipch:2个
4星 · 超过85%的资源 需积分: 50 22 下载量 33 浏览量
2012-11-17
09:33:41
上传
评论 2
收藏 1.18MB RAR 举报
温馨提示
问题描述:假如要用很多个教室对一组课程进行调度,每节课程都有其开始时间和结束时间,我们希望使用尽量少的时间来调度所有的课程,请给出调度算法?
资源推荐
资源详情
资源评论
收起资源包目录
ClassroomDistribution.rar (40个子文件)
ClassroomDistribution
ClassroomDistribution.sln 1KB
Debug
ClassroomDistribution.exe 30KB
ClassroomDistribution.ilk 320KB
ClassroomDistribution.pdb 419KB
ipch
classroomdistribution-68fc1eca
classroomdistribution-45132d9f.ipch 2.25MB
sortanalysis-9a634ab5
sortanalysis-b631a4d8.ipch 2.44MB
ClassroomDistribution.sdf 2.39MB
ClassroomDistribution.suo 15KB
ClassroomDistribution
ClassroomDistribution.vcxproj.user 143B
Debug
cl.command.1.tlog 954B
ClassroomDistribution.exe.embed.manifest 406B
link.8808.read.1.tlog 2B
ClassroomDistribution.exe.embed.manifest.res 472B
rc.command.1.tlog 904B
CL.read.1.tlog 2KB
vc100.idb 59KB
mt.read.1.tlog 610B
link-cvtres.read.1.tlog 2B
link.read.1.tlog 3KB
rc.read.1.tlog 582B
link.write.1.tlog 2KB
CL.write.1.tlog 758B
link.8808.write.1.tlog 2B
vc100.pdb 68KB
ClassroomDistribution.lastbuildstate 105B
link.8808-cvtres.read.1.tlog 2B
link.command.1.tlog 2KB
ClassroomDistribution.exe.intermediate.manifest 381B
mt.write.1.tlog 610B
link.8808-cvtres.write.1.tlog 2B
ClassroomDistribution.log 3KB
ClassroomDistribution_manifest.rc 232B
mt.command.1.tlog 586B
ClassroomDistribution.obj 14KB
rc.write.1.tlog 590B
link-cvtres.write.1.tlog 2B
ClassroomDistribution.vcxproj.filters 1KB
h1.h 736B
ClassroomDistribution.vcxproj 4KB
ClassroomDistribution.cpp 3KB
共 40 条
- 1
资源评论
- hj11201120652014-05-04挺不错的,可以用,能执行
dzyhenry
- 粉丝: 15
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功