# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
407395 |
2021-05-18T21:19:50 Z |
rainboy |
Domino (COCI15_domino) |
C |
|
4000 ms |
524292 KB |
#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);
| ^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
247 ms |
39512 KB |
Output is correct |
2 |
Correct |
142 ms |
39512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
716 KB |
Output is correct |
2 |
Correct |
2 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1238 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4049 ms |
358164 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3958 ms |
158536 KB |
Output is correct |
2 |
Correct |
1427 ms |
158684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1163 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
716 KB |
Output is correct |
2 |
Correct |
3 ms |
736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1198 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
1868 KB |
Output is correct |
2 |
Correct |
14 ms |
1836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1172 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1152 KB |
Output is correct |
2 |
Correct |
7 ms |
1064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4064 ms |
158472 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1178 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |