//资源分配算法的实现
#include "stdio.h"
//#include "iostream.h"
//#define n 100
typedef int Type;
#define M 8 // 可分配的资源份额
#define N 3 // 工程项目个数
//int m;
//int n;
Type allot_res(int n, int m, Type G[N ][M+1], int optq[ ]);
Type G[N][M+1]={{0,4,26,40,45,50,51,52,53},{0,5,15,40,60,70,73,74,75},{0,6,15,40,80,90,95,98,100}}; // 工程的利润表
Type optg; // 最优的总利润
int optq[N]; // 工程最优分配份额
Type f[N][M+1]; // 各阶段不同资源份额的最大利润
int d[N][M+1]; // f[i][x] 最大时,第i项工程的分配份额
Type g[N]; // 阶段的最大利润
int q[N]; // 第 i 阶段第 i 工程最优分配份额
int optx; // 最优分配时的资源最优分配份额
int k; // 最优分配时的工程项目数
void main()
{
optx = allot_res(N,M,G,optq);
printf("%d \n",optx);
getchar();
}
Type allot_res(int n, int m, Type G[N ][M+1], int optq[ ])
{
int optx, k, i, j, x;
int *q = new int[ N ]; // 分配工作单元
int (*d)[M+1] = new int[ N ][ M+1 ];
Type (*f)[M+1] = new Type[ N ][ M+1 ];
Type *g = new Type[ N ];
for (j=0; j
{ // 第一个工程的份额利润表
f[ 0 ][ j ] = G[ 0 ][ j ]; d[ 0 ][ j ] = j;
}
for (i=1; i
{
f[ i ][ 0 ] = G[ i ][ 0 ] + f[ i-1 ][ 0 ];
d[ i ][ 0 ] = 0;
for (j=1; j
{
f[ i ][ j ] = f[ i ][ 0 ]; d[ i ][ j ] = 0;
for (x=0; x
{
if (f[ i ][ j ]
{
f[ i ][ j ] = G[ i ][ x ] + f[ i-1][ j-x ];
d[ i ][ j ] = x;
}
}
}
}
for (i=0; i
{
g[ i ] = f[ i ][ 0 ]; q[ i ] = 0;
for (j=1; j
if (g[ i ]
{
g[ i ] = f[ i ][ j ]; q[ i ] = j;
}
}
optg = g[ 0 ]; optx = q[ 0 ]; k = 0;
for (i=1; i
{
if (optg
{
optg = g[ i ]; optx = q[ i ]; k = i;
}
}
if (k
{ // 最大编号之后的
for (i=k+1; 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;
}
}
//资源分配算法的实现
#include "stdio.h"
//#include "iostream.h"
//#define n 100
typedef int Type;
#define M 8 // 可分配的资源份额
#define N 3 // 工程项目个数
//int m;
//int n;
Type allot_res(int n, int m, Type G[N ][M+1], int optq[ ]);
Type G[N][M+1]={{0,4,26,40,45,50,51,52,53},{0,5,15,40,60,70,73,74,75},{0,6,15,40,80,90,95,98,100}}; // 工程的利润表
Type optg; // 最优的总利润
int optq[N]; // 工程最优分配份额
Type f[N][M+1]; // 各阶段不同资源份额的最大利润
int d[N][M+1]; // f[i][x] 最大时,第i项工程的分配份额
Type g[N]; // 阶段的最大利润
int q[N]; // 第 i 阶段第 i 工程最优分配份额
int optx; // 最优分配时的资源最优分配份额
int k; // 最优分配时的工程项目数
void main()
{
optx = allot_res(N,M,G,optq);
printf("%d \n",optx);
getchar();
}
Type allot_res(int n, int m, Type G[N ][M+1], int optq[ ])
{
int optx, k, i, j, x;
int *q = new int[ N ]; // 分配工作单元
int (*d)[M+1] = new int[ N ][ M+1 ];
Type (*f)[M+1] = new Type[ N ][ M+1 ];
Type *g = new Type[ N ];
for (j=0; j
{ // 第一个工程的份额利润表
f[ 0 ][ j ] = G[ 0 ][ j ]; d[ 0 ][ j ] = j;
}
for (i=1; i
{
f[ i ][ 0 ] = G[ i ][ 0 ] + f[ i-1 ][ 0 ];
d[ i ][ 0 ] = 0;
for (j=1; j
{
f[ i ][ j ] = f[ i ][ 0 ]; d[ i ][ j ] = 0;
for (x=0; x
{
if (f[ i ][ j ]
{
f[ i ][ j ] = G[ i ][ x ] + f[ i-1][ j-x ];
d[ i ][ j ] = x;
}
}
}
}
for (i=0; i
{
g[ i ] = f[ i ][ 0 ]; q[ i ] = 0;
for (j=1; j
if (g[ i ]
{
g[ i ] = f[ i ][ j ]; q[ i ] = j;
}
}
optg = g[ 0 ]; optx = q[ 0 ]; k = 0;
for (i=1; i
{
if (optg
{
optg = g[ i ]; optx = q[ i ]; k = i;
}
}
if (k
{ // 最大编号之后的
for (i=k+1; 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;
}
}