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

来源:互联网 发布:制作录音广告软件 编辑:程序博客网 时间:2024/05/22 05:01

代码:

#include <bits/stdc++.h>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<Node> pq;void initNode(Node &node, int n){    node.x = new int[n];    int i;    for(i=0; i<n; 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<n; 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<n; i++)        {            b[i][j] = M[i][j];            c[i] = i;        }        for(i=0; i<n-1; i++)            for(k=n-1; k>i; k--)                if(b[k][j]<b[k-1][j])                {                    swap(b[k][j], b[k-1][j]);                    swap(c[k], c[k-1]);                }        for(i=0; i<n; 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<n; 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<n; 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<n; 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<n; i++)                    bestx[i] = E.x[i];                delete []E.x;            }        }        else        {            int i;            for(i=E.s; i<n; i++)            {                swap(E.x[E.s], E.x[i]);                int f1, f2;                int bb = bound(E, f1, f2, y);                if(bb<bestc)                {                    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<n; 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<n1;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<n; i++)        printf("%d ", bestx[i]+1);    printf("\n");    return 0;}


原创粉丝点击