主题:请教流水作业调度问题
我的算法是流水作业问题,就是有多个作业,每个有2个任务,用动态规划实现作业的最优调度,使到完成作业的时间最小.以下是我的代码,我自己实现不了,又找不到错误,请帮帮我吧!谢谢!!!!
class Element1 implements Comparable
{
int key;
int index;
boolean job;
Element1(int kk,int ii,boolean jj)
{
key=kk;
index=ii;
job=jj;
}
public int compareTo(Object x)
{
int xkey=((Element1)x).key;
if(key<xkey)return -1;
if(key==xkey)return 0;
return 1;
}
}
class MergeSort //合并排序
{
public static void mergeSort(Comparable []a)
{
Comparable []b=new Comparable[a.length];
int s=1;
while(s<a.length){
mergePass(a,b,s);//合并到数组b
s+=s;
mergePass(b,a,s);//合并到数组a
s+=s;
}
}
public static void mergePass(Comparable []x,Comparable[]y,int s)
{
//合并大小为s的相邻子数组
int i=0;
while(i<=x.length-2*s)
{//合并大小为s的相邻2段子数组
merge(x,y,i,i+s-1,i+2*s-1);
i=i+2*s;
}
//剩下的元素个数少于2s
if(i+s<x.length)
merge(x,y,i,i+s-1,x.length-1);
else
//复制到y
for(int j=i;j<x.length;j++)
y[j]=x[j];
}
public static void merge(Comparable []c,Comparable[]d,int l,int m,int r)
{
//合并c[l:m]和c[m+1:r]到d[l:r]
int i=1,
j=m+1,
k=1;
while ((i<=m)&&(j<=r))
if (c[i].compareTo(c[j])<=0)
d[k++]=c[i++];
else d[k++]=c[j++];
if(i>m)
for(int q=j;q<=r;q++)
d[k++]=c[q];
else
for (int q=i;q<=m;q++ )
d[k++]=c[q];
}
}
public class flowshop
{
public static void main(String arg[])
{
int a[]={1,2,3,4,8}; //自实定作业的任务1
int b[]={6,6,9,15,6}; //自实定作业的任务2
int c[]=new int[b.length];
int h=flowShop(a,b,c);
System.out.println(h);
}
public static int flowShop(int[]a,int[]b,int[]c)
{
int n=a.length;
Element1 []d=new Element1[n];
for(int i=0;i<n;i++){
int key=a[i]>b[i]? b[i]:a[i];
boolean job=a[i]<=b[i];
d[i]=new Element1(key,i,job);
}
int j=0,k=n-1;
for(int i=0;i<n;i++){
if(d[i].job) c[j++]=d[i].index;
else c[k--]=d[i].index;
}
j=a[c[0]];
k=j+b[c[0]];
for(int i=1;i<n;i++){
j+=a[c[i]];
k=j<k? k+b[c[i]]:j+b[c[i]];
}
for(int i=0;i<n;i++)
{
System.out.println(c[i]+1); //输出调度的次序
}
return k; //返回最优调度时间
}
}
class Element1 implements Comparable
{
int key;
int index;
boolean job;
Element1(int kk,int ii,boolean jj)
{
key=kk;
index=ii;
job=jj;
}
public int compareTo(Object x)
{
int xkey=((Element1)x).key;
if(key<xkey)return -1;
if(key==xkey)return 0;
return 1;
}
}
class MergeSort //合并排序
{
public static void mergeSort(Comparable []a)
{
Comparable []b=new Comparable[a.length];
int s=1;
while(s<a.length){
mergePass(a,b,s);//合并到数组b
s+=s;
mergePass(b,a,s);//合并到数组a
s+=s;
}
}
public static void mergePass(Comparable []x,Comparable[]y,int s)
{
//合并大小为s的相邻子数组
int i=0;
while(i<=x.length-2*s)
{//合并大小为s的相邻2段子数组
merge(x,y,i,i+s-1,i+2*s-1);
i=i+2*s;
}
//剩下的元素个数少于2s
if(i+s<x.length)
merge(x,y,i,i+s-1,x.length-1);
else
//复制到y
for(int j=i;j<x.length;j++)
y[j]=x[j];
}
public static void merge(Comparable []c,Comparable[]d,int l,int m,int r)
{
//合并c[l:m]和c[m+1:r]到d[l:r]
int i=1,
j=m+1,
k=1;
while ((i<=m)&&(j<=r))
if (c[i].compareTo(c[j])<=0)
d[k++]=c[i++];
else d[k++]=c[j++];
if(i>m)
for(int q=j;q<=r;q++)
d[k++]=c[q];
else
for (int q=i;q<=m;q++ )
d[k++]=c[q];
}
}
public class flowshop
{
public static void main(String arg[])
{
int a[]={1,2,3,4,8}; //自实定作业的任务1
int b[]={6,6,9,15,6}; //自实定作业的任务2
int c[]=new int[b.length];
int h=flowShop(a,b,c);
System.out.println(h);
}
public static int flowShop(int[]a,int[]b,int[]c)
{
int n=a.length;
Element1 []d=new Element1[n];
for(int i=0;i<n;i++){
int key=a[i]>b[i]? b[i]:a[i];
boolean job=a[i]<=b[i];
d[i]=new Element1(key,i,job);
}
int j=0,k=n-1;
for(int i=0;i<n;i++){
if(d[i].job) c[j++]=d[i].index;
else c[k--]=d[i].index;
}
j=a[c[0]];
k=j+b[c[0]];
for(int i=1;i<n;i++){
j+=a[c[i]];
k=j<k? k+b[c[i]]:j+b[c[i]];
}
for(int i=0;i<n;i++)
{
System.out.println(c[i]+1); //输出调度的次序
}
return k; //返回最优调度时间
}
}