批处理作业问题 分支限界法 C语言,批处理作业调度问题(分支限界法)

代码:

#include

using namespace std;

const int MAX=100;

const int MACHINE=2;

int n;

int M[MAX][MACHINE];

int b[MAX][MACHINE];

int a[MAX][MACHINE];

int y[MAX][MACHINE];

int bestx[MAX];

int bestc;

struct Node

{

int s;

int f1;

int f2;

int sf2;

int bb;

int *x;

bool operator < (const Node &node) const

{

return bb > node.bb;

}

};

priority_queue pq;

void initNode(Node &node, int n)

{

node.x = new int[n];

int i;

for(i=0; i

node.x[i] = i;

node.s = 0;

node.f1 = 0;

node.f2 = 0;

node.sf2 = 0;

node.bb = 0;

}

void newNode(Node &node, Node E, int Ef1, int Ef2, int Ebb, int n)

{

node.x = new int[n];

int i;

for(i=0; i

node.x[i] = E.x[i];

node.f1 = Ef1;

node.f2 = Ef2;

node.sf2 = E.sf2 + Ef2;

node.bb = Ebb;

node.s = E.s + 1;

}

void swap(int &a, int &b)

{

int temp = a;

a = b;

b = temp;

}

void sort()

{

int *c = new int[n];

int i, j, k;

for(j=0; j<2; j++)

{

for(i=0; i

{

b[i][j] = M[i][j];

c[i] = i;

}

for(i=0; i

for(k=n-1; k>i; k--)

if(b[k][j]

{

swap(b[k][j], b[k-1][j]);

swap(c[k], c[k-1]);

}

for(i=0; i

a[c[i]][j] = i;

}

delete []c;

}

int bound(Node E, int &f1, int &f2, int y[MAX][MACHINE])

{

int k, j;

for(k=0; k

for(j=0; j<2; j++)

y[k][j] = 0;

for(k=0; k<=E.s; k++)

for(j=0; j<2; j++)

y[a[E.x[k]][j]][j] = 1;

f1 = E.f1 + M[E.x[E.s]][0];

f2 = ((f1>E.f2)?f1:E.f2) + M[E.x[E.s]][1];

int sf2 = E.sf2 + f2;

int s1 = 0, s2 = 0;

int k1 = n - E.s, k2 = n - E.s;

int f3 = f2;

for(j=0; j

if(!y[j][0])

{

k1--;

if(k1 == n-E.s-1)

f3 = (f2>f1+b[j][0])?f2:f1+b[j][0];

s1 += f1 + k1 * b[j][0];

}

for(j=0; j

if(!y[j][1])

{

k2--;

s1 += b[j][1];

s2 += f3 + k2 * b[j][1];

}

return sf2 + (s1>s2?s1:s2);

}

int flowShop()

{

sort();

Node E;

initNode(E, n);

bestc = 1e6;

while(E.s<=n)

{

if(E.s==n)

{

if(E.sf2 < bestc)

{

bestc = E.sf2;

int i;

for(i=0; i

bestx[i] = E.x[i];

delete []E.x;

}

}

else

{

int i;

for(i=E.s; i

{

swap(E.x[E.s], E.x[i]);

int f1, f2;

int bb = bound(E, f1, f2, y);

if(bb

{

Node node;

newNode(node, E, f1, f2, bb, n);

pq.push(node);

}

swap(E.x[E.s], E.x[i]);

}

delete []E.x;

}

if(pq.empty())

break;

E = pq.top();

pq.pop();

}

return bestc;

}

void init(int n1, int M1[3][2])

{

n = n1;

int i, j;

for(i=0; i

for(j=0; j<2; j++)

M[i][j] = M1[i][j];

}

int main()

{

int n1;

int M1[MAX][MACHINE];

cin>>n1;

for(int i=0;i

for(int j=0;j<2;j++)

cin>>M1[i][j];

init(n1, M1);

int best = flowShop();

printf("最少完成时间和:%d\n", best);

printf("最优调度:\n");

for(int i=0; i

printf("%d ", bestx[i]+1);

printf("\n");

return 0;

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值