问题:
设有n个独立的作业{1, 2, …, n}, 由m台相同的机器进行加工处理. 作业i所需时间为t i. 约定:任何作业可以在任何一台机器上加工处理, 但未完工前不允许中断处理,任何作业不能拆分成更小的子作业。要求给出一种作业调度方案,使所给的n 个作业在尽可能短的时间内由m台机器加工处理完成。
贪心策略:
最长处理时间作业优先,即把处理时间最长的作业分配给最先空闲的机器。
贪心法:
贪心法是在已知信息下做出局部最优解,且做出贪心选择后不会再改变选择,贪心法的核心是贪心策略的选择。
贪心法的一般过程:
greedy © //c是问题的输入集合,即候选集合
{
S = {};//初始解集合置空
while (not solution(s))//集合S没有构成问题的一个解
{
x = select©;//在候选集合中做贪心选择
if feasible(x)//判断集合s中加入x后的解是否可行
s = s + {x};
c = c - {x};
}
retuen s;
}
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
class mycompare
{
public:
bool operator()(int s1, int s2)
{
return s1 > s2;
}
};
int main()
{
mycompare mycompare1;
int n, m, c;
vector<int> v1, v2;
cout << "作业个数为:";
cin >> n;
cout << "作业时间依次为:" << endl;
for (int i = 0; i < n; i++)
{
cin >> c;
v1.push_back(c);
}
sort(v1.begin(), v1.end(), mycompare1);//作业时间降序排序
cout << "处理机个数为:";
cin >> m;
for (int i = 0; i < m; i++)
v2.push_back(0);
for (int i = 0; i < n; i++)
{
sort(v2.begin(), v2.end());//处理机完成作业时间升序排列
v2[0] += v1[i];//将作业分配给最先空闲的处理机,并将处理机完成作业时间加上相应作业时间
}
cout << "最短处理机时间为:" << *v2.rbegin();
}