?? 批處理作業調度問題.cpp
字號:
// 批處理作業調度問題
#include <iostream.h>
int Int_Max=10000; // ∞
const int n=3; // 作業個數
class Flowshop
{
friend Flow(int M[n][2], int []);
private:
void Backtrack(int i);
int m[n][2], // 各作業所需處理時間
*x, // 當前作業調度
*bestx, // 當前最優作業調度
*f2, // 機器2完成處理時間
f1, // 機器1完成處理時間
f, // 完成時間和
bestf; // 當前最優值
};
void Flowshop::Backtrack(int i)
{
void Swap(int &a, int &b);
if (i+1>n)
{
bestf=f;
for (int j=0; j<n; j++) bestx[j]=x[j];
return;
}
for (int j=i; j<n; j++)
{
f1+=m[x[j]][0];
f2[i+1]=((f2[i]>f1) ? f2[i] : f1)+m[x[j]][1];
f+=f2[i+1];
if (f<bestf)
{
Swap(x[i], x[j]);
Backtrack(i+1);
Swap(x[i], x[j]);
}
f1-=m[x[j]][0];
f-=f2[i+1];
}
}
int Flow(int M[n][2], int bestx[])
{
Flowshop X;
X.x=new int [n];
X.f2=new int [n];
X.bestx=bestx;
X.bestf=Int_Max;
X.f1=0;
X.f=0;
for (int i=0; i<n; i++)
{
X.m[i][0]=M[i][0];
X.m[i][1]=M[i][1];
X.x[i]=bestx[i];
X.f2[i]=0;
}
X.Backtrack(0);
delete [] X.x, X.f2;
return X.bestf;
}
void Swap(int &a, int &b)
{
int temp=a;
a=b;
b=temp;
}
void main()
{
int t[n][2]={{2,1},{3,1},{2,3}}, x[n]={0,1,2};
cout<<"\nx: t1,t2\n--------\n";
for (int i=0; i<n; i++) cout<<x[i]+1<<": "<<t[i][0]<<", "<<t[i][1]<<endl;
cout<<"\n\n BestF = "<<Flow(t, x);
cout<<"\n\n BestX = "<<x[0]+1;
for (i=1; i<n; i++) cout<<", "<<x[i]+1;
cout<<"\n\n";
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -