算法导论示例-FastestWay(applet)

原创 2006年06月11日 15:10:00
/**
 * Introduction to Algorithms, Second Edition 
 * 15.1 Assembly-line scheduling
 * @author 土豆爸爸
 * 
 */
import java.util.ArrayList;
import java.util.List;

public class FastestWay {
    public static List<Station> findFastestWay(Assembly a1, Assembly a2) {
        assert(a1.stations.size() == a2.stations.size()); //两个装配线长度相同
        int n = a1.stations.size();
        List<Station> way1 = new ArrayList<Station>(n);
        List<Station> way2 = new ArrayList<Station>(n);
        a1.stations.get(0).f = a1.e + a1.stations.get(0).a; //计算f(1,1)
        a2.stations.get(0).f = a2.e + a2.stations.get(0).a; //计算f(2,1)
        
        for(int j = 1; j < n; j++) {
            Station s1 = a1.stations.get(j);    //第1条装配线第j个工位
            Station s10 = a1.stations.get(j-1); //第1条装配线第j-1个工位
            Station s2 = a2.stations.get(j);    //第2条装配线第j个工位
            Station s20 = a2.stations.get(j-1); //第2条装配线第j-1个工位
            int t11 = s10.f + s1.a;        //从第1条装配线的j-1工位到第1条装配线j工位所需要时间
            int t21 = s20.f + s20.t + s1.a;//从第2条装配线的j-1工位到第1条装配线j工位所需要时间
            int t22 = s20.f + s2.a;        //从第1条装配线的j-1工位到第1条装配线j工位所需要时间
            int t12 = s10.f + s10.t + s2.a;//从第2条装配线的j-1工位到第1条装配线j工位所需要时间    
            if(t11 < t21) {
                s1.f = t11;
                way1.add(s10);
            } else {
                s1.f = t21;
                way1.add(s20);
            }
            
            if(t22 < t12) {
                s2.f = t22;
                way2.add(s20);
            } else {
                s2.f = t12;
                way2.add(s10);
            }         
        }
        
        //出口判断
        Station last1 = a1.stations.get(n - 1);
        Station last2 = a1.stations.get(n - 1);
        if(last1.f + a1.x <= last2.f + a2.x) {
            way1.add(last1);
            return way1;
        } else {
            way2.add(last2);
            return way2;
        }
    }
}

/**
 * 装配线
 */
class Assembly {
    public int e; //上线时间
    public int x; //出线时间
    public List<Station> stations;
    
    public Assembly(int e, int x) {
        this.e = e;
        this.x = x;
    }
}

/**
 * 工位 
 */
class Station {
    public Assembly assembly;
    public int a; //工作时间
    public int t; //从上一工位到当前工位的转换时间
    public int f; //到达当前工位最短时间
    public Station(Assembly assembly, int a, int t) {
        this.assembly = assembly;
        this.a = a;
        this.t = t;
    }
}

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

public class FastestWayTest extends TestCase {
    public void testFastestWay() {
        Assembly a1 = new Assembly(2, 3);
        List<Station> s1 = new ArrayList<Station>();
        s1.add(new Station(a1, 7, 2));
        s1.add(new Station(a1, 9, 3));
        s1.add(new Station(a1, 3, 1));
        s1.add(new Station(a1, 4, 3));
        s1.add(new Station(a1, 8, 4));
        s1.add(new Station(a1, 4, 0));
        a1.stations = s1;
        
        Assembly a2 = new Assembly(4, 2);
        List<Station> s2 = new ArrayList<Station>();
        s2.add(new Station(a2, 8, 2));
        s2.add(new Station(a2, 5, 1));
        s2.add(new Station(a2, 6, 2));
        s2.add(new Station(a2, 4, 2));
        s2.add(new Station(a2, 5, 1));
        s2.add(new Station(a2, 7, 0));
        a2.stations = s2;
        
        int[] answer = {7, 5, 3, 4, 5, 4};
        List<Station> way = FastestWay.findFastestWay(a1, a2);
        
        Station prev = way.get(0);
        int sum = prev.assembly == a1 ? a1.e : a2.e;
        for(int i = 0; i < way.size(); i++) {
            Station station = way.get(i);
            assertEquals(answer[i], station.a);
            if(station.assembly == prev.assembly) {
                sum += station.a;
            } else {
                sum += station.a + prev.t;
            }
            prev = station;
        }
        sum += prev.assembly == a1 ? a1.x : a2.x;
        assertEquals(38, sum);
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

链式哈希表(Hash Table)--算法导论示例

In computer science, a hash table is an associative array data structure that associates keys with v...

算法导论之红黑树

红黑树定义:是一棵二叉搜索树,它在每个结点上增加了一个存储位来表示结点的颜色,可以是红色也可以是黑色。通过对任意一条从根到叶子的简单路径上各个结点的颜色进行约束,红黑树确保没有一条路径会比其他路径长2...

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

//15.1装配线调度问题 #include using namespace std; #define I 2 #define J 6 int a[I+1][J+1],e[I+1],x[I+1]...

[算法导论]分治法---最大子数组

分治策略---最大自子数组 一、分治策略的三个步骤 1、分解:将问题划分为一些子问题,子问题的形式与原问题一样,只是规模更小 2、解决:递归地求解出子问题。如果子问题的规模足够小...

算法导论笔记(三) : 快速排序

1 快速排序简介 2 快速排序过程 3 代码实现 4 性能分析

算法导论-----最大优先队列

#define INIT_NUMBER 2 int reserve_number = 100; int heap_size; int length; typedef struct pair { ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)