#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <bitset>
#include <stack>
#include <cstdlib>
#include <queue>
#include <stdint.h>
#include <cstdio>
#include <limits.h>
using namespace std;
struct Domino {
pair<int,int> from, to;
int64_t cost;
bool operator < (const Domino& d1) const {
if (this->cost != d1.cost) return (this->cost > d1.cost);
if (this->from != d1.from) return (this->from < d1.from);
if (this->to != d1.to) return (this->to < d1.to);
return false;
}
};
class Graph {
public:
vector<int64_t> weight;
vector<vector<int>> adj;
vector<int64_t> dp;
void add_edge (int u, int v) {
adj[u][v] = adj[v][u] = 1;
}
Graph (int n) {
adj.resize(n);
for (int i = 0; i < n; i++) {
adj[i].assign(n, 0);
}
}
void solve (int k) {
dp.assign((1 << (int)adj.size()), 0);
for (int i = 0; i < dp.size(); i++) {
if (__builtin_popcount(i) > k) {
continue;
}
vector<int> nodes;
for (int j = 0; j < adj.size(); j++) {
if (i & (1 << j)) {
nodes.push_back(j);
//cout << "YES ";
}
}
//cout << adj.size() << " " << i << " " << nodes.size() << " " << __builtin_popcount(i) << '\n';
assert(nodes.size() == __builtin_popcount(i));
bool fine = true;
for (int x: nodes) {
for (int y: nodes) {
if (x == y) continue;
if (!adj[x][y]) {
fine = false;
}
}
}
if (!fine) {
continue;
}
for (int x: nodes) {
dp[i] += weight[x];
}
}
for (int i = 1; i < dp.size(); i++) {
for (int j = 0; j < adj.size(); j++) {
if (i & (1 << j)) {
dp[i] = max(dp[i], dp[i - (1 << j)]);
}
}
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
int64_t grid[n][n];
int sm = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> grid[i][j];
sm += grid[i][j];
}
}
vector<Domino> dominoes;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (i + dx < 0 || j + dy < 0) continue;
if (i + dx == n || j + dy == n) continue;
if (abs(dx) + abs(dy) != 1) continue;
if (dx < dy) continue;
dominoes.push_back({{i, j}, {i + dx,j + dy}, grid[i][j] + grid[i + dx][j + dy]});
}
}
}
}
sort(dominoes.begin(), dominoes.end());
while (dominoes.size() > 7 * (m - 1) + 5) {
dominoes.pop_back();
}
vector<pair<int64_t,pair<Domino, Domino>>> edges;
for (auto& d1: dominoes) {
for (auto& d2: dominoes) {
set<pair<int,int>> s; s.insert(d1.from), s.insert(d1.to), s.insert(d2.from), s.insert(d2.to);
if (s.size() != 4) continue;
//cout << d1.from.first << " " << d1.from.seco
edges.push_back({d1.cost + d2.cost, {d1, d2}});
}
}
set<Domino> mySet;
for (auto& p: edges) {
mySet.insert(p.second.first), mySet.insert(p.second.second);
}
map<Domino,int> myMap; int cntr = 0;
vector<int64_t> weights;
for (Domino d: mySet) {
weights.push_back(d.cost);
myMap[d] = cntr++;
}
Graph gr(cntr);
gr.weight = weights;
for (auto& e: edges) {
gr.add_edge(myMap[e.second.first], myMap[e.second.second]);
}
gr.solve(m);
cout << sm - gr.dp.back();
}
Compilation message
domino.cpp: In member function 'void Graph::solve(int)':
domino.cpp:47:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i = 0; i < dp.size(); i++) {
| ~~^~~~~~~~~~~
domino.cpp:52:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int j = 0; j < adj.size(); j++) {
| ~~^~~~~~~~~~~~
domino.cpp:76:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int i = 1; i < dp.size(); i++) {
| ~~^~~~~~~~~~~
domino.cpp:77:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int j = 0; j < adj.size(); j++) {
| ~~^~~~~~~~~~~~
domino.cpp: In function 'int main()':
domino.cpp:114:28: warning: comparison of integer expressions of different signedness: 'std::vector<Domino>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
114 | while (dominoes.size() > 7 * (m - 1) + 5) {
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
14648 KB |
Output is correct |
2 |
Correct |
54 ms |
14656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1622 ms |
228756 KB |
Output is correct |
2 |
Correct |
954 ms |
228764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
4436 KB |
Output is correct |
2 |
Correct |
36 ms |
4436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
964 ms |
214924 KB |
Output is correct |
2 |
Correct |
571 ms |
215192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
561 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1810 ms |
445064 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1172 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1797 ms |
445096 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
2764 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1314 ms |
131636 KB |
Output is correct |
2 |
Correct |
1287 ms |
131660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1834 ms |
445820 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
44 ms |
68296 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
481 ms |
178844 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
852 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1926 ms |
512968 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |