Files
python/TangDou/AcWing/BeiBao/1013_dfs.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

42 lines
875 B
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 11;
const int M = 16;
int n;
int m;
int path[N], res[N];
int w[N][M];
int mx;
// u:第几个公司 s:已经产生的价值 r:剩余的机器数量
void dfs(int u, int s, int r) {
if (u == n + 1) {
if (s > mx) {
mx = s;
memcpy(res, path, sizeof path);
}
return;
}
for (int i = 0; i <= r; i++) {
path[u] = i;
dfs(u + 1, s + w[u][i], r - i); // 给u号公司分配i个机器
path[u] = 0;
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> w[i][j];
dfs(1, 0, m);
printf("%d\n", mx);
// 输出最优答案时的路径
for (int i = 1; i <= n; i++) printf("%d %d\n", i, res[i]);
return 0;
}