Pagini recente » Cod sursa (job #2107641) | Cod sursa (job #469155) | Cod sursa (job #2484671) | Cod sursa (job #2231633) | Cod sursa (job #1073088)
#include <fstream>
#include <vector>
#include <algorithm>
#include <bitset>
#define NMAX 700
using namespace std;
int M, N, R, C, MAX, total;
int mat[NMAX][NMAX];
vector<int> sum_row, sum_col;
bitset<7295> sol;
void input() {
ifstream in("elimin.in");
in>>M>>N>>R>>C;
if (M < N) {
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
in>>mat[i][j];
}
}
} else {
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
in>>mat[j][i];
}
}
swap(M, N); swap(R, C);
}
sum_col.resize(N, 0);
for (int i = 0; i < M; ++i) {
int sum = 0;
for (int j = 0; j < N; ++j) {
sum += mat[i][j];
}
total += sum;
sum_row.push_back(sum);
}
in.close();
}
void sortescu(int total) {
int i, j;
for (i = 0; i < N; ++i) {
sum_col[i] = 0;
}
for (i = 0; i < M; ++i) {
if (!sol[i]) {
for (j = 0; j < N; ++j) {
sum_col[j] += mat[i][j];
}
}
}
sort(sum_col.begin(), sum_col.end());
for (i = 0; i < C && total > MAX; ++i) {
total -= sum_col[i];
}
if (total > MAX) {
MAX = total;
}
}
void back(const int &pos, const int &lvl, const int &total) {
if (lvl == R) {
sortescu(total);
return;
}
for (int i = pos + 1; (i < M) && (M - i - R + lvl >= 0); ++i) {
int newtotal = total - sum_row[i];
if (newtotal > MAX) {
sol[i] = 1;
back(i, lvl + 1, newtotal);
sol[i] = 0;
}
}
}
inline void output() {
ofstream out("elimin.out");
out<<MAX;
out.close();
}
int main() {
input();
back(-1, 0, total);
output();
return 0;
}