有N個獨立作業,每個作業處理時間為time[i],有M個相同的機器加工處理,約定每個作業可以在任何一台機器上加工處理,未完工前不允許中斷處理,作業不能拆分成更小的子作業。要求在最短時間內完成,求最短時
最理想的方法是平均分配,每台機器處理的時間相同,最後同時處理完任務。實際情況中不一定能完全分配,我們應盡量縮小各個機器處理時間的差距,用貪 心演算法可以比較好的解決:先將作業處理時間降序排列,依次選擇時間往機器上安排,每次安排在當前工作量總時間最小的機器上,最後求得時間差距最小
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
def main():
Machine = 4
time = []
for i in range(100):
time.append(random.randint(1,100))
time.sort()
time.reverse()
print time
total = [0,0,0,0]
for i in time:
min_time = total[0]
k = 0
for j in range(1,4):
if min_time > total[j]:
k = j
min_time = total[j]
total[k] += i
print total
return 0
if __name__ == '__main__':
main()
[100, 99, 97, 95, 95, 95, 94, 94, 92, 91, 91, 90, 89, 89, 89, 89, 88, 88, 87, 87, 86, 85, 84, 84, 83, 82, 82, 82, 82, 81, 79, 79, 78, 77, 76, 76, 75, 73, 72, 72, 68, 67, 61, 60, 60, 58, 58, 56, 54, 53, 53, 52, 51, 50, 50, 49, 49, 47, 47, 47, 46, 45, 44, 44, 43, 43, 43, 43, 42, 41, 41, 40, 38, 38, 36, 36, 34, 32, 31, 29, 28, 27, 27, 25, 25, 23, 22, 22, 19, 18, 18, 15, 14, 14, 12, 10, 8, 5, 4, 2]
[1410, 1411, 1411, 1412]