机器分配

机器分配open in new window

实现

#include <iostream>
using namespace std;

const int N = 12, M = 17;
int n, m;
int w[N][M];
int f[N][M], rec[N];

int main () {
    cin >> n >> m;
    for (int i = 1; i <= n; ++ i)
        for (int j = 1; j <= m; ++ j)
            cin >> w[i][j];
    
    for (int i = 1; i <= n; ++ i)
        for (int j = 0; j <= m; ++ j)
            for (int k = 0; k <= j; ++ k)
                f[i][j] = max(f[i][j], f[i - 1][j - k] + w[i][k]);

    int j = m;
    for (int i = n; i >= 1; -- i) {
        for (int k = 0; k <= j; ++ k) {
            if (f[i][j] == f[i - 1][j - k] + w[i][k]) {
                rec[i] = k;
                j -= k;
                break;
            }
        }
    }

    cout << f[n][m] << endl;
    for (int i = 1; i <= n; ++ i)
        cout << i << ' ' << rec[i] << endl;
    return 0;
}
最后修改于: