算法导论15.1节 装配线调度问题

View Code
#include <stdio.h>

int l[2][6] = {0};
int f[2][6] = {0};
int f1, l1;

int fastWay(int a[][6], int t[][5], int *e, int *x, int n) 
{
        f[0][0] = e[0] + a[0][0];
        f[1][0] = e[1] + a[1][0];
        
        int j;
        for (j = 1; j < n; j++) {
                if (f[0][j-1] + a[0][j] <= f[1][j-1] + t[1][j-1] + a[0][j]) {
                        f[0][j] = f[0][j-1] + a[0][j];
                        l[0][j] = 1;
                }
                else {
                        f[0][j] = f[1][j-1] + t[1][j-1] + a[0][j];
                        l[0][j] = 2;
                }
                if (f[1][j-1] + a[1][j] <= f[0][j-1] + t[0][j-1] + a[1][j]) {
                        f[1][j] = f[1][j-1] + a[1][j];
                        l[1][j] = 2;
                }
                else {
                        f[1][j] = f[0][j-1] + t[0][j-1] + a[1][j];
                        l[1][j] = 1;
                }
        }
        if (f[0][n-1] + x[0] <= f[1][n-1] + x[1]) {
                f1 = f[0][n-1] + x[0];
                l1 = 1;
        }
        else {
                f1 = f[1][n-1] + x[1];
                l1 = 2;
        }
}


void printStation(int l[][6], int l1, int n)
{
        int i, j;
        i = l1;
        printf("line %d, station %d\n", i, n);
        for (j = n; j > 1; j--) {
                i = l[i-1][j-1];
                printf("line %d, station %d\n", i, j-1);
        }

}

int main()
{
        int e[2] = {2, 4};
        int x[2] = {3, 2};
        int a[2][6] = {7, 9, 3, 4, 8, 4,
                       8, 5, 6, 4, 5, 7};
        int t[2][5] = {2, 3, 1, 3, 4,
                       2, 1, 2, 2, 1};
        fastWay(a, t, e, x, 6);
        printStation(l, l1, 6);

        return 0;
}

 

posted on 2013-03-13 20:22  愤怒的屎壳螂  阅读(142)  评论(0编辑  收藏  举报

导航