#include
#include
#define N 100
using namespace std;
/*----------作业*/
class Jobtype{
public:
int operator <= (Jobtype a) const{
return (key <= a.key);
};
int key , index;
bool job;
};
Jobtype d[N];
/*----------排序*/
int cmp(Jobtype a , Jobtype b)
{
return a.key < b.key;
}
/*--------------johnson算法*/
int FlowShop(int n , int a[] , int b[] , int c[])
{
for(int i = 0 ; i < n ; i++){
d[i].key = a[i] > b[i] ? b[i] : a[i];//按Johnson法则分别取对应的b[i]或a[i]值作为关键字
d[i].job = a[i] <= b[i];//给符合条件a[i]
d[i].index = i;
}
sort(d , d+n , cmp);//对数组d按关键字升序进行排序
int j = 0 , k = n-1;
for(int i = 0 ; i < n ; i++){
if(d[i].job)
c[j++] = d[i].index;//将排过序的数组d,取其中作业序号属于N1的从前面进入
else c[k--] = d[i].index;//属于N2的从后面进入,从而实现N1的非减序排序,N2的非增序排序
}
j = a[c[0]];
k = j+b[c[0]];
for(int i = 1 ; i < n ; i++){
j += a[c[i]];//M1在执行c[i]作业的同时,M2在执行c[i-1]号作业,最短执行时间取决于M1与M2谁后执行完
k = j < k ? k+b[c[i]] : j+b[c[i]];//计算最优加工时间
}
return k;
}
/*--------main()*/
int main()
{
int n;
cout<
cin>>n;
int a[n] , b[n] , c[n];
cout<
for(int i = 0 ; i < n ; i++)
cin>>a[i]>>b[i];
int mintime = FlowShop(n , a , b , c);
cout<
cout<
for(int i = 0 ; i < n ;i++)
cout<
cout<
return 0;
}
/**
请输入作业数:
5
请输入作业在 M1 M2 上的作业时间:
2 5
4 2
3 3
6 1
1 7
作业完成最短时间为:19
作业调度顺序是:
4 0 2 1 3
Process returned 0 (0x0) execution time : 19.001 s
Press any key to continue.
*/