回溯法求批处理作业调度

package mouse;

import java.util.*;

public class FlowShop {
	static int n; // 作业数
	static int f1; // 机器1完成处理时间
	static int f; // 完成时间和
	static int bestf; // 当前最优值

	static int[][] m; // 各作业所需要的处理时间
	static int[] x; // 当前作业调度
	static int[] bestx; // 当前最优作业调度
	static int[] f2; // 机器2完成处理时间

	public static void trackback(int i) {

		// i用来指示到达的层数(第几步,从0开始),同时也指示当前执行完第几个任务
		if (i == n) { // 得出一组最优值
			for (int j = 0; j < n; j++) {
				bestx[j] = x[j];
			}
			bestf = f;
		} else {
			for (int j = i; j < n; j++) { // j用来指示选择了哪个任务(也就是执行顺序)
											// tb(0)进来了,不管怎么递归,就有j=0,1,2这三个过程,因此肯定能遍历完全

				f1 += m[x[j]][0]; // 选择第x[j]个任务来执行
				if (i > 0) { // 选择出的不是第一个任务
					f2[i] = ((f2[i - 1] > f1) ? f2[i - 1] : f1) + m[x[j]][1];
				} else {// 选择出的是第一个任务
					f2[i] = f1 + m[x[j]][1];
				}
				f += f2[i];
				if (f < bestf) {
					swap(x, i, j); // 关键:把选择出的任务j调到当前执行的位置i
					trackback(i + 1); // 选择下一个任务执行
					swap(x, i, j); // 递归后恢复原样
				}
				f1 -= m[x[j]][0]; // 递归后恢复原样
				f -= f2[i];
			}
		}
	}

	private static void swap(int[] x, int i, int j) {
		int temp = x[i];
		x[i] = x[j];
		x[j] = temp;
	}

	private static void test() {
		n = 3;
		int[][] testm = { { 2, 1 }, { 3, 1 }, { 2, 3 } };
		m = testm;

		int[] testx = { 0, 1, 2 };
		x = testx;

		bestx = new int[n];
		f2 = new int[n];

		f1 = 0;
		f = 0;
		bestf = Integer.MAX_VALUE;

		trackback(0); // 起点可变用trackback(0),如果从一定点开始,就要用trackback(1)
		System.out.println(Arrays.toString(bestx));
		System.out.println(bestf);
	}

	public static void main(String[] args) {
		test();
	}
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值