[pta]5-3求解多机调度问题(贪心法)

设有n个独立的作业{1,2,…,n},由m台相同的机器{1,2, …,m}进行加工处理,作业i所需的处理时间为ti(1≤i≤n),每个作业均可在任何一台机器上加工处理,但未完工前不允许中断,任何作业也不能拆分成更小的子作业。
多机调度问题要求给出一种作业调度方案,使所给的n个作业在尽可能短的时间内由m台机器加工处理完成。

输入格式:

第一行输入作业数n和机器数m,接着的n行中输入每个作业的编号和所需处理时长。

输出格式:

输出n行,每行输出机器编号、作业号、执行时间、占用时间段

输入样例1:

7 3
1 2
2 14
3 4
4 16
5 6
6 5
7 3

输出样例1:

1 4 16 0-16
2 2 14 0-14
3 5 6 0-6
3 6 5 6-11
3 3 4 11-15
2 7 3 14-17
3 1 2 15-17

代码解析:

#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
#define N 100

//问题表示
int n; //作业数量
int m; //机器数量

struct NodeType
{
    int no;                            //作业序号
    int t;                            //执行时间
    int mno;                        //机器序号
    bool operator<(const NodeType &s) const 
    {    return t>s.t;  }                //按t越小越优先出队
};
struct NodeType A[N]; //作业数组

void solve()                            //求解多机调度问题
{
    NodeType e;
    if (n<=m) //如果作业数量小于等于机器数量,直接返回
        return;
    sort(A,A+n); //对作业按照执行时间进行排序
    priority_queue<NodeType> qu;        //小根堆
    for (int i=0;i<m;i++) //将前m个作业分配给机器
    {
        A[i].mno=i+1;
        printf("%d %d %d %d-%d\n", //输出分配结果
                A[i].mno,A[i].no,A[i].t,0,A[i].t);
        qu.push(A[i]); //将作业加入小根堆
    }
    for (int j=m;j<n;j++) //处理剩余的作业
    {
        e = qu.top(); //取出当前最早完成作业的机器
        qu.pop();
        printf("%d %d %d %d-%d\n",e.mno,A[j].no,A[j].t,e.t,e.t+A[j].t); //输出分配结果
        e.t=e.t+A[j].t; //更新作业的完成时间
        qu.push(e); //将作业加入小根堆
    }
}

int main()
{
    cin>>n>>m; //输入作业数量和机器数量
    for(int i=0;i<n;i++)
      cin>>A[i].no>>A[i].t; //输入每个作业的序号和执行时间
    solve(); //调用求解函数
    return 0;
}

 

### 使用回溯法求解流水作业调度问题 #### 流水作业调度问题描述 给定 n 个作业要在两台器上加工处理,每个作业都必须先由器 M1 加工,然后再由器 M2 加工。设第 i 个作业需占用器 M1 的时间为 ai , 占用器 M2 的时间为 bi 。现在要找出这 n 个作业的一个最优排列方案使得总完成时间最短。 #### 回溯法原理 回溯法是一种通过构建解空间树来尝试所有可能解决方案的方法,在每一步选择中做出尽可能好的决策并继续前进;如果发现当前路径无法得到更优的结果,则返回到前一状态重新选择其他可能性直到找到全局最优解[^1]。 #### 算法实现思路 为了应用回溯法于上述问题之中: - 定义一个函数 `backtrack` 接受参数为当前正在考虑安排哪个工作以及已经计算出来的部分成本。 - 对每一个未被分配的工作位置 k (k=0, ..., n),假设将其放在当前位置 j 上面,并更新相应的最早可用时间和累积花费。 - 如果所有的任务都被成功放置完毕,则记录下此时的成本作为候选答案之一。 - 当遍历结束之后比较各个保存下来的可行解取其中最小者即为我们所寻求的最佳排序方式。 下面是具体的 Python 实现代码: ```python import sys def flow_shop_scheduling(n, a, b): best_order = [] min_cost = float(&#39;inf&#39;) def backtrack(j, current_a_time, current_b_time, order, visited): nonlocal best_order, min_cost if j == n: cost = max(current_a_time + sum([a[i] for i in range(n)]), current_b_time + sum([b[i] for i in range(n)])) if cost < min_cost: min_cost = cost best_order[:] = order[:] return for i in range(n): if not visited[i]: next_a_time = current_a_time + a[i] next_b_time = max(current_b_time, next_a_time) + b[i] new_visited = list(visited) new_visited[i] = True backtrack( j + 1, next_a_time, next_b_time, order + [i], new_visited) initial_visited = [False]*n backtrack(0, 0, 0, [], initial_visited) return best_order, min_cost if __name__ == "__main__": # Example input data from the problem statement. num_jobs = int(input().strip()) processing_times_m1 = list(map(int, input().split())) processing_times_m2 = list(map(int, input().split())) optimal_sequence, minimum_completion_time = flow_shop_scheduling(num_jobs, processing_times_m1, processing_times_m2) print(f"Optimal sequence of jobs is {optimal_sequence}") print(f"The minimal completion time required to process all tasks on both machines sequentially is {minimum_completion_time}.") ``` 此程序读入一组测试数据后会输出最佳的任务序列及其对应的最少完工时刻。注意这里采用的是递归形式的回溯方法来进行搜索过程模拟。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值