1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| public class seriesOfWork { static int n = 5; static int[] x = {1, 2, 3, 4, 5}; static int[] bestx = new int[x.length+1]; static int f1; static int[] f2 = new int[x.length+1]; static int f; static int bestf=9999;
static int[][] chart = {
{0, 0, 0}, {0, 3, 5}, {0, 6, 1}, {0, 5, 2}, {0, 4, 4}, {0, 3, 2}};
public static void main(String[] args) { int[] temp=new int[x.length+1]; boolean isVisit[] = new boolean[x.length]; work( 0, x, temp, isVisit); System.out.println("最优的作业调度方案:"); for(int i =1;i<temp.length;i++){ System.out.print(" "+temp[i]+" "); } System.out.println(); System.out.println("最优值为:"); System.out.println(bestf); }
public static void work( int index, int[] x, int[] temp, boolean[] isVisit) { if (index == x.length) { for(int i=1;i<x.length+1;i++){ f1+=chart[temp[i]][1]; f2[i]=(Math.max(f2[i - 1], f1))+chart[temp[i]][2]; f+=f2[i]; } if(f<bestf){ bestf=f; for(int j=1;j<=x.length;j++){ bestx[j]=temp[j]; } } }
for (int i = 0; i < x.length; i++) { if (!isVisit[i]) { temp[index+1] = x[i]; isVisit[i] = true; work( index + 1, x, temp, isVisit); isVisit[i] = false; f1=0; f=0; } } } }
|