#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 3e3 + 1;
const int inf = 0x3f3f3f3f;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int n, m, k;
int s, t;
int d[maxN * maxN];
struct TEdge{
int u, v, c, w;
TEdge(){}
TEdge(int _u, int _v, int _c, int _w){
u = _u;
v = _v;
c = _c;
w = _w;
}
};
vector <int> adj[maxN * maxN];
vector <TEdge> S;
int a[maxN][maxN];
bool inqueue[maxN * maxN];
int trace[maxN * maxN];
inline int conv(int i, int j){
return (i - 1) * m + j;
}
inline void add(int u, int v, int w){
adj[u].pb(S.size());
S.pb({u, v, 1, w});
adj[v].pb(S.size());
S.pb({v, u, 0, -w});
}
void SPFA(){
fill(d, d + n * m + 2, inf);
d[0] = 0;
queue <int> Q;
Q.push(0);
while (!Q.empty()){
int u = Q.front();
inqueue[u] = 0;
Q.pop();
for (int i: adj[u]){
int c = S[i].c, v = S[i].v, w = S[i].w;
if (c && d[v] > d[u] + w){
d[v] = d[u] + w;
trace[v] = i;
if (!inqueue[v]){
inqueue[v] = 1;
Q.push(v);
}
}
}
}
}
void Init(){
cin >> n >> k;
m = n;
s = 0, t = n * m + 1;
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j) cin >> a[i][j];
}
ll res = 0;
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j){
res += a[i][j];
if ((i + j) % 2){
add(s, conv(i, j), 0);
for (int k = 0; k < 4; ++k){
int x = i + dx[k];
int y = j + dy[k];
if (x < 1 || x > n || y < 1 || y > m) continue;
add(conv(i, j), conv(x, y), -a[i][j] - a[x][y]);
}
}
else add(conv(i, j), t, 0);
}
}
while (k--){
SPFA();
int v = t;
res += d[v];
while (v){
int i = trace[v];
S[i].c ^= 1;
S[i ^ 1].c ^= 1;
v = S[i].u;
}
}
cout << res;
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message
domino.cpp: In function 'int main()':
domino.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
domino.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
179 ms |
256572 KB |
Output is correct |
2 |
Correct |
196 ms |
256576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
212432 KB |
Output is correct |
2 |
Correct |
94 ms |
212472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
639 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
211800 KB |
Output is correct |
2 |
Correct |
93 ms |
211776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
554 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
595 ms |
386600 KB |
Output is correct |
2 |
Correct |
564 ms |
386628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
630 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
212400 KB |
Output is correct |
2 |
Correct |
94 ms |
212488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
647 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
213804 KB |
Output is correct |
2 |
Correct |
121 ms |
213788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
211916 KB |
Output is correct |
2 |
Correct |
98 ms |
211836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
635 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
212792 KB |
Output is correct |
2 |
Correct |
98 ms |
212832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
755 ms |
386652 KB |
Output is correct |
2 |
Correct |
719 ms |
386604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
211796 KB |
Output is correct |
2 |
Correct |
95 ms |
211772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
637 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |