作业批处理

来源:互联网 发布:通信算法工程师 工作 编辑:程序博客网 时间:2024/06/06 06:47
#include<iostream>
using namespace std;
int x[100], bestx[100], m[100][100];
int f1 = 0, f2 = 0, cf = 0, bestf = 10000, n;
void Backtrack(int t)
{
 int tempf, j;
 if (t>n) //到达叶子结点,搜索到最底部
 {
  if (cf<bestf)
  {
   for (int i = 1; i <= n; i++)
    bestx[i] = x[i];
   bestf = cf;
  }
 }
 else    //非叶子结点
 {
  for (j = t; j <= n; j++)
  {
   f1 += m[x[j]][1];
   tempf = f2;//保存上一个作业在机器2的完成时间
   f2 = (f1>f2 ? f1 : f2) + m[x[j]][2];//保存当前作业在机器2的完成时间
   cf += f2;               //在机器2上的完成时间和
   if (cf<bestf)
   {
    swap(x[t], x[j]);  //交换两个作业的位置
    Backtrack(t + 1);
    swap(x[t], x[j]);
   }
   f1 -= m[x[j]][1];
   cf -= f2;
   f2 = tempf;
  }
 }
}
int main()
{
 int i, j;
 cout << "请输入作业数:" << endl;
 cin >> n;
 cout << "请输入在各机器上的处理时间" << endl;
 for (i = 1; i <= 2; i++)
 for (j = 1; j <= n; j++)
  cin >> m[j][i];
 for (i = 1; i <= n; i++)
  x[i] = i;
 Backtrack(1);
 cout << "调度作业顺序:" << endl;
 for (i = 1; i <= n; i++)
  cout << bestx[i] << ' ';
 cout << endl;
 cout << "处理时间:" << endl;
 cout << bestf;
 system("pause");
 return 0;
}
/*
测试数据:
3
2 3 2
1 1 3
3
2 5 4
3 2 1
*/
原创粉丝点击