批处理作业调度程序(优化了一个细节)

王晓东书上的例子,但是我做了优化并没有使用f2[]数组,只是简单的用了一个f2和last变量。

#include <stdio.h>

int x[4] , f1 , f2 , f , bestf , n = 3;

int m[4][3] = {
{
0 , 0 , 0},
{
0 , 2 , 1},
{
0 , 3 , 1},
{
0 , 2 , 3}
};

void BackTrack(int i)
{
int last , j , temp;

if(i > n)
{

bestf
= f;
return ;
}
for(j = i ; j <= n ; j++)
{
f1
+= m[x[j]][1];
last
= f2; //保存上一个JOB在机器2完成时间
f2 = (f1 > f2 ? f1 : f2) + m[x[j]][2]; //当前JOB在机器2完成时间
f += f2;

if(f < bestf)
{
temp
= x[i]; x[i] = x[j]; x[j] = temp;
BackTrack(i
+ 1);
temp
= x[i]; x[i] = x[j]; x[j] = temp;
}

f1
-= m[x[j]][1];
f
-= f2;
f2
= last;
}
}

int main(void)
{
int i;

for(i = 0 ; i < 4 ; i++)
{
x[i]
= i;
}
bestf
= 10000;
f2
= f1 = f = 0;

BackTrack(
1);
printf(
"最小完成时间为:%d\n", bestf);
return 0;
}

 

算法就是回溯+简单的剪枝(f < bestf)

posted on 2010-04-23 16:15  DiaoCow  阅读(346)  评论(0编辑  收藏  举报

导航