#include<stdio.h>
int optg;
int alloc_res(int n, int m, int G[3][8], int optq[]) {
int optx, k, i, x, z;
int* q = new int[n];
int(*d)[8] = new int[3][8];
int(*f)[8] = new int[3][8];
int* g = new int[n];
for (x = 0; x <= m; x++) {
f[0][x] = G[0][x];
d[0][x] = x;
}
for (i = 1; i < n; i++) {
f[i][0] = G[i][0] + f[i - 1][0];
d[i][0] = 0;
for (x = 1; x <= m; x++) {
f[i][x] = f[i][0];
d[i][x] = 0;
for (z = 0; z <= x; z++) {
if (f[i][x] < G[i][z] + f[i - 1][x - z]) {
f[i][x] = G[i][z] + f[i - 1][x - z];
d[i][x] = z;
}
}
}
}
for (i = 0; i < n; i++) {
g[i] = f[i][0]; q[i] = 0;
for (x = 1; x <= m; x++) {
if (g[i] < f[i][x]) {
g[i] = f[i][x]; q[i] = x;
}
}
}
optg = g[0]; optx = q[0]; k = 0;
for (i = 1; i < n; i++) {
if (optg < g[i]) {
optg = g[i]; optx = q[i]; k = i;
}
}
if (k < n - 1) {
for (i = k + 1; i < n; i++)
optq[i] = 0;
}
for (i = k; i >= 0; i--) {
optq[i] = d[i][optx];
optx = optx - optq[i];
}
delete q; delete d; delete f; delete g;
return optg;
}
int main() {
int m = 7;//可分配资源份额数
int n = 3;//工程项目数
int optq[3];//各项目分配额
int G[3][8] = { {0,4,26,40,45,50,51,52},
{0,5,15,40,61,70,73,74},
{0,5,15,40,80,90,95,98} };
int optg = alloc_res(3, 7, G, optq);
printf("%d", optg);
return 0;
}
资源分配问题
于 2024-05-07 11:07:17 首次发布