#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 2000
#define N_ (2 + N * N + 1)
#define M_ (1 + N * N + N * (N - 1) * 2)
#define A 1000
#define INF 0x3f3f3f3f
int ii[M_], jj[M_], ww[M_], ww_[M_], cc[M_ * 2], m_;
int *eh[N_], eo[N_], n_;
void append(int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
void add(int i, int j, int c, int w) {
int h = m_++;
ii[h] = i, jj[h] = j, ww[h] = w, cc[h << 1 | 0] = c;
append(i, h << 1 | 0), append(j, h << 1 | 1);
}
int dd[N_], kk[N_], ff[N_], pq[N_], iq[1 + N_], cnt;
int lt(int i, int j) {
return dd[i] < dd[j] || dd[i] == dd[j] && kk[i] < kk[j];
}
int p2(int p) {
return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_add_last(int i) {
iq[pq[i] = ++cnt] = i;
}
int pq_remove_first() {
int i = iq[1], j = iq[cnt--];
if (j != i)
pq[j] = 1, pq_dn(j);
pq[i] = 0;
return i;
}
int dijkstra(int s, int t) {
memset(dd, 0x3f, n_ * sizeof *dd);
dd[s] = 0, kk[s] = 0, pq_add_last(s);
while (cnt) {
int i = pq_remove_first(), k = kk[i] + 1, o;
for (o = eo[i]; o--; ) {
int h_ = eh[i][o];
if (cc[h_]) {
int h = h_ >> 1, j = i ^ ii[h] ^ jj[h], d = dd[i] + ((h_ & 1) == 0 ? ww_[h] : -ww_[h]);
if (dd[j] > d || dd[j] == d && kk[j] > k) {
if (dd[j] == INF)
pq_add_last(j);
ff[j] = h_, dd[j] = d, kk[j] = k, pq_up(j);
}
}
}
}
return dd[t] != INF;
}
void trace(int s, int t) {
while (t != s) {
int h_ = ff[t], h = h_ >> 1;
cc[h_]--, cc[h_ ^ 1]++;
t ^= ii[h] ^ jj[h];
}
}
int edmonds_karp(int s, int t) {
int h, w;
memcpy(ww_, ww, m_ * sizeof *ww);
while (dijkstra(s, t)) {
trace(s, t);
for (h = 0; h < m_; h++) {
int i = ii[h], j = jj[h];
if (dd[i] != INF && dd[j] != INF)
ww_[h] += dd[i] - dd[j];
}
}
w = 0;
for (h = 0; h < m_; h++)
w += ww[h] * cc[h << 1 | 1];
return w;
}
int main() {
int n, k, i, j, sum;
scanf("%d%d", &n, &k), n_ = 2 + n * n + 1;
for (i = 0; i < n_; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
add(0, 1, k, 0);
sum = 0;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
int a;
scanf("%d", &a);
sum += a;
if ((i + j) % 2 == 0)
add(1, 2 + i * n + j, 1, A - a);
else
add(2 + i * n + j, n_ - 1, 1, A - a);
}
for (i = 0; i < n; i++)
for (j = i % 2; j < n; j += 2) {
if (i > 0)
add(2 + i * n + j, 2 + (i - 1) * n + j, 1, 0);
if (i + 1 < n)
add(2 + i * n + j, 2 + (i + 1) * n + j, 1, 0);
if (j > 0)
add(2 + i * n + j, 2 + i * n + (j - 1), 1, 0);
if (j + 1 < n)
add(2 + i * n + j, 2 + i * n + (j + 1), 1, 0);
}
printf("%d\n", sum + edmonds_karp(0, n_ - 1) - A * k * 2);
return 0;
}
Compilation message
domino.c: In function 'append':
domino.c:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
17 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
domino.c: In function 'lt':
domino.c:32:41: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
32 | return dd[i] < dd[j] || dd[i] == dd[j] && kk[i] < kk[j];
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
domino.c: In function 'dijkstra':
domino.c:80:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
80 | if (dd[j] > d || dd[j] == d && kk[j] > k) {
| ~~~~~~~~~~~^~~~~~~~~~~~
domino.c: In function 'main':
domino.c:122:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | scanf("%d%d", &n, &k), n_ = 2 + n * n + 1;
| ^~~~~~~~~~~~~~~~~~~~~
domino.c:131:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | scanf("%d", &a);
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
262 ms |
38616 KB |
Output is correct |
2 |
Correct |
136 ms |
38628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
716 KB |
Output is correct |
2 |
Correct |
2 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1187 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4062 ms |
349624 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2193 ms |
154728 KB |
Output is correct |
2 |
Correct |
1450 ms |
154824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1189 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
716 KB |
Output is correct |
2 |
Correct |
3 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1193 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1868 KB |
Output is correct |
2 |
Correct |
12 ms |
1868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1195 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1100 KB |
Output is correct |
2 |
Correct |
6 ms |
1124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3523 ms |
154744 KB |
Output is correct |
2 |
Correct |
2529 ms |
158680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1178 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |