zzulioj1202:作业调度方案
题目描述
我们现在要利用m台机器加工n个工件,每个工件都有m道工序,每道工序都在不同的指定的机器上完成。每个工件的每道工序都有指定的加工时间。
每个工件的每个工序称为一个操作,我们用记号j-k表示一个操作,其中j为1到n中的某个数字,为工件号;k为1到m中的某个数字,为工序号,例如2-4表示第2个工件第4道工序的这个操作。在本题中,我们还给定对于各操作的一个安排顺序。
例如,当n=3,m=2时,“1-1,1-2,2-1,3-1,3-2,2-2”就是一个给定的安排顺序,即先安排第1个工件的第1个工序,再安排第1个工件的第2个工序,然后再安排第2个工件的第1个工序,等等。
一方面,每个操作的安排都要满足以下的两个约束条件。
(1) 对同一个工件,每道工序必须在它前面的工序完成后才能开始;
(2) 同一时刻每一台机器至多只能加工一个工件。
另一方面,在安排后面的操作时,不能改动前面已安排的操作的工作状态。
由于同一工件都是按工序的顺序安排的,因此,只按原顺序给出工件号,仍可得到同样的安排顺序,于是,在输入数据中,我们将这个安排顺序简写为“1 1 2 3 3 2”。
还要注意,“安排顺序”只要求按照给定的顺序安排每个操作。不一定是各机器上的实际操作顺序。在具体实施时,有可能排在后面的某个操作比前面的某个操作先完成。
例如,取n=3,m=2,已知数据如下:
则对于安排顺序“1 1 2 3 3 2”,下图中的两个实施方案都是正确的。但所需要的总时间分别是10与12。
当一个操作插入到某台机器的某个空档时(机器上最后的尚未安排操作的部分也可以看作一个空档),可以靠前插入,也可以靠后或居中插入。为了使问题 简单一些,我们约定:在保证约束条件(1)(2)的条件下,尽量靠前插入。并且,我们还约定,如果有多个空档可以插入,就在保证约束条件(1)(2)的条 件下,插入到最前面的一个空档。于是,在这些约定下,上例中的方案一是正确的,而方案二是不正确的。
显然,在这些约定下,对于给定的安排顺序,符合该安排顺序的实施方案是唯一的,请你计算出该方案完成全部任务所需的总时间。
输入
第1行为两个正整数,用一个空格隔开:
m n
(其中m(〈20)表示机器数,n(〈20)表示工件数)
第2行: 2n个用空格隔开的数,为给定的安排顺序。
接下来的2n行,每行都是用空格隔开的m个正整数,每个数不超过20。
其中前n行依次表示每个工件的每个工序所使用的机器号,第1个数为第1个工序的机器号,第2个数为第2个工序机器号,等等。
后n行依次表示每个工件的每个工序的加工时间。
可以保证,以上各数据都是正确的,不必检验。
输出
只有一个正整数,为最少的加工时间。
样例输入 Copy
2 3
1 1 2 3 3 2
1 2
1 2
2 1
3 2
2 5
2 4
样例输出 Copy
10
import java.util.*; import java.util.concurrent.ConcurrentHashMap; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); int m = in.nextInt(); int n = in.nextInt(); int totalTime = 0; List<Integer> order = new ArrayList<Integer>(); //存放完整的顺序字符串 List<String> orderStr = new ArrayList<String>(); //存放每个工件的每个工序所使用的机器号 List<int[]> list_num = new ArrayList<int[]>(); //存放每个工件的每个工序所花费的时间 List<int[]> list_time = new ArrayList<int[]>(); //根据用户输入 拼接正确的顺序字符串存入orderStr集合中 for (int i = 0; i < m * n; i++) { int l = in.nextInt(); int count = 1; for (Integer integer : order) { if (l == integer) { count++; } } String s = l + "-" + count; orderStr.add(s); order.add(l); } //输入机器号 for (int i = 0; i < n; i++) { int num[] = new int[m+1]; for (int j = 1; j <= m; j++) { num[j] = in.nextInt(); } list_num.add(num); } //输入工序对应时间 for (int i = 0; i < n; i++) { int time[] = new int[m+1]; for (int j = 1; j <= m; j++) { time[j] = in.nextInt(); } list_time.add(time); } //用三个集合建立工件、机器编号、工序时间之间的关系 Map<Integer, Integer> tmp_g_num = new ConcurrentHashMap<Integer,Integer>(); Map<Integer, Integer> tmp_num = new ConcurrentHashMap<Integer,Integer>(); List<Integer> tmp_time = new ArrayList<Integer>(); //全部工件加工完成则循环结束 while (orderStr != null&&orderStr.size()!=0) { if(orderStr==null||orderStr.size()==0){ break; } //查找可以加工的工件以及可以使用的机器 for (int i = 0; i < orderStr.size(); i++) { //机器被全部占用则停止循环 if (tmp_num.size() == m) { break; } String s = orderStr.get(i); String[] strings = s.split("-"); //分别得到工件号以及加工到了哪一步 int k = Integer.parseInt(strings[0]); int l = Integer.parseInt(strings[1]); //根据加工到的具体步骤来得到所需要的机器号和加工时间 int[] nums = list_num.get(k - 1); int[] times = list_time.get(k - 1); //如果工件没有被加工并且机器没有被占用 if (!tmp_num.keySet().contains(nums[l])&&!(tmp_g_num.keySet().contains(k))) { int flag = 0; //查看工件的上一个工序是否已经完成 for (int j = 0; j < orderStr.size(); j++) { String s1 = orderStr.get(j); String[] strings1 = s1.split("-"); int k1 = Integer.parseInt(strings1[0]); int l1 = Integer.parseInt(strings1[1]); int[] nums1 = list_num.get(k1 - 1); int[] times1 = list_time.get(k1 - 1); if (k1==k&&l1<l){ flag = 1; break; } } //若工件的上一个工序已经完成 if (flag == 0) { tmp_g_num.put((Integer)k, (Integer)nums[l]); tmp_num.put((Integer)nums[l], (Integer)times[l]); tmp_time.add((Integer)times[l]); } } } //对这一轮可以同时加工的工件所需的时间进行排序 Collections.sort(tmp_time, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1 - o2; } }); //把最终时间加上这一轮里面的最短时间 totalTime += tmp_time.get(0); Integer leastTime = tmp_time.get(0); //根据之前建立的对应关系来删除各个集合中已经完成的工件 for (Integer integer : tmp_num.keySet()) { //如果机器对应的占用时间等于本轮加工的最短时间 if (leastTime == tmp_num.get(integer)) { //根据机器号来查找具体对应的哪一个工件 for (Integer integer1 : tmp_g_num.keySet()) { if (tmp_g_num.get(integer1) == integer) { //根据工件号来删除orderStr集合中具体的步骤 for (int i = 0; i < orderStr.size(); i++) { String s = orderStr.get(i); String s1 = s.split("-")[0]; if (integer1 == Integer.parseInt(s1)) { orderStr.remove(i); break; } } //删除该元素,表示工件没有正在被加工 tmp_g_num.remove(integer1); } } //删除该元素,表示机器不再被占用 tmp_num.remove(integer); } } //删除或修改本轮已经完成的工序对应的时间集合中的元素 for (int i=0;i<tmp_time.size();i++) { Integer a = tmp_time.get(i),b = tmp_time.get(i); a = a - leastTime; if (a == 0) { tmp_time.remove(i); i--; } else{ //修改剩余时间 tmp_time.remove(i); tmp_time.add(i,a); //修改tmp_num集合中对应的机器所剩余的使用时间 for (Integer integer : tmp_num.keySet()) { if(tmp_num.get(integer)==b){ tmp_num.put(integer,a); } } } } } System.out.println(totalTime); } }