This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500
#define M 500
#define N_ ((N + 1) * (M + 1) * 3)
#define K 100000
#define INF 0x3f3f3f3f3f3f3f3fLL
int min(int a, int b) { return a < b ? a : b; }
int *ej[N_], eo[N_]; long long *ew[N_];
void append(int i, int j, long long w) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ew[i] = (long long *) realloc(ew[i], o * 2 * sizeof *ew[i]);
}
ej[i][o] = j, ew[i][o] = w;
}
long long dd_[N_]; int iq[N_ + 1], pq[N_], cnt;
int lt(int i, int j) { return dd_[i] < dd_[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 main() {
static int ii[K], jj[K], dd[N + 1][M + 1];
int n, m, n_, k, h, i, j, a, b, c, o;
scanf("%d%d%d%d%d%d", &n, &m, &a, &b, &c, &k), n_ = (n + 1) * (m + 1) * 3;
for (h = 0; h < k; h++)
scanf("%d%d", &ii[h], &jj[h]);
for (i = 0; i <= n; i++)
memset(dd[i], 0x3f, (m + 1) * sizeof *dd[i]);
for (h = 0; h < k; h++)
dd[ii[h]][jj[h]] = 0;
for (i = 0; i <= n; i++) {
for (j = 1; j <= m; j++)
dd[i][j] = min(dd[i][j], dd[i][j - 1] + 1);
for (j = m - 1; j >= 0; j--)
dd[i][j] = min(dd[i][j], dd[i][j + 1] + 1);
}
for (j = 0; j <= m; j++) {
for (i = 1; i <= n; i++)
dd[i][j] = min(dd[i][j], dd[i - 1][j] + 1);
for (i = n - 1; i >= 0; i--)
dd[i][j] = min(dd[i][j], dd[i + 1][j] + 1);
}
for (i = 0; i < n_; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
ew[i] = (long long *) malloc(2 * sizeof *ew[i]);
}
for (i = 0; i <= n; i++)
for (j = 0; j <= m; j++) {
if (j > 0)
append((i * (m + 1) + j) * 3 + 1, (i * (m + 1) + (j - 1)) * 3 + 1, a);
if (j < m)
append((i * (m + 1) + j) * 3 + 1, (i * (m + 1) + (j + 1)) * 3 + 1, a);
if (i > 0)
append((i * (m + 1) + j) * 3 + 2, ((i - 1) * (m + 1) + j) * 3 + 2, a);
if (i < n)
append((i * (m + 1) + j) * 3 + 2, ((i + 1) * (m + 1) + j) * 3 + 2, a);
append((i * (m + 1) + j) * 3 + 0, (i * (m + 1) + j) * 3 + 1, b);
append((i * (m + 1) + j) * 3 + 0, (i * (m + 1) + j) * 3 + 2, b);
append((i * (m + 1) + j) * 3 + 1, (i * (m + 1) + j) * 3 + 0, (long long) c * dd[i][j]);
append((i * (m + 1) + j) * 3 + 2, (i * (m + 1) + j) * 3 + 0, (long long) c * dd[i][j]);
if (j > 0)
append((i * (m + 1) + j) * 3 + 0, (i * (m + 1) + (j - 1)) * 3 + 0, c);
if (j < m)
append((i * (m + 1) + j) * 3 + 0, (i * (m + 1) + (j + 1)) * 3 + 0, c);
if (i > 0)
append((i * (m + 1) + j) * 3 + 0, ((i - 1) * (m + 1) + j) * 3 + 0, c);
if (i < n)
append((i * (m + 1) + j) * 3 + 0, ((i + 1) * (m + 1) + j) * 3 + 0, c);
}
memset(dd_, 0x3f, n_ * sizeof *dd_);
i = (ii[0] * (m + 1) + jj[0]) * 3 + 0;
dd_[i] = 0, pq_add_last(i);
while (cnt) {
i = pq_remove_first();
if (i / 3 == ii[k - 1] * (m + 1) + jj[k - 1]) {
printf("%lld\n", dd_[i]);
return 0;
}
for (o = eo[i]; o--; ) {
long long w, d;
j = ej[i][o], w = ew[i][o], d = dd_[i] + w;
if (dd_[j] > d) {
if (dd_[j] == INF)
pq_add_last(j);
dd_[j] = d, pq_up(j);
}
}
}
return 0;
}
Compilation message (stderr)
soccer.c: In function 'append':
soccer.c:18:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
18 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
soccer.c: In function 'main':
soccer.c:66:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d%d%d%d%d%d", &n, &m, &a, &b, &c, &k), n_ = (n + 1) * (m + 1) * 3;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
soccer.c:68:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d%d", &ii[h], &jj[h]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |