#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int *x;
int **M;
int *best;
int *f2;
int f1;
int f;
int bestf;
int n;
void init() {
f = 0;
f1 = 0;
bestf = INT_MAX;
printf("请输入作业数");
scanf("%d", &n);
x = (int*)malloc(sizeof(int) * (n + 1));
best = (int*)malloc(sizeof(int) * (n + 1));
f2 = (int*)malloc(sizeof(int) * (n + 1));
M = (int**)malloc(sizeof(int*) * (n + 1));
for (int i = 1; i <= n; ++i) {
M[i] = (int*)malloc(sizeof(int) * 3);
}
for (int i = 1; i <= n; ++i) {
printf("请输入%d个作业机器一处理的时间", i);
scanf("%d", &M[i][1]);
printf("请输入%d个作业机器二处理的时间", i);
scanf("%d", &M[i][2]);
}
for (int i = 1; i <= n; ++i) {
x[i] = i;
}
f2[0] = 0;
}
void backtrack(int i) {
if (i > n) {
bestf = f;
memcpy(best + 1, x + 1, sizeof(int) * n);
return;
}
for (int j = i; j <= n; ++j) {
f1 += M[x[j]][1];
f2[i] = (f2[i - 1] > f1 ? f2[i - 1] : f1) + M[x[j]][2];
f += f2[i];
if (f < bestf) {
int temp;
temp = x[i];
x[i] = x[j];
x[j] = temp;
backtrack(i + 1);
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
f1 -= M[x[j]][1];
f -= f2[i];
}
}
void show() {
printf("调度顺序为");
for (int i = 1; i <= n; ++i) {
printf("%d ", best[i]);
}
printf("\n最小时间为%d", bestf);
}
int main() {
init();
backtrack(1);
show();
return 0;
}
批处理调度C语言
最新推荐文章于 2021-09-16 18:26:32 发布