1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include <cstdio> #include <queue> using namespace std; typedef long long LL; void chkmin(LL &x, LL k) { (x > k) && (x = k); } template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } LL inf = 1e18; const int N = 110, K = 20; const int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0}; bool mp[N][N], vis[N][N][K]; int n, m, A, B, C; LL d[N][N][K]; bool inside(int x, int y) { return x > 0 && y > 0 && x <= n && y <= n; } struct Node { int x, y, t; LL s; friend bool operator<(const Node &x, const Node &y) { return x.s > y.s; } }; int main() { read(n), read(m), read(A), read(B), read(C); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) read(mp[i][j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) for (int k = 0; k <= m; k++) d[i][j][k] = inf; d[1][1][m] = 0; priority_queue<Node> q; q.push((Node){1, 1, m, 0}); while (!q.empty()) { Node t = q.top(); q.pop(); if (vis[t.x][t.y][t.t] || !t.t) continue; vis[t.x][t.y][t.t] = true; for (int i = 0; i < 4; i++) { int nx = t.x + dx[i], ny = t.y + dy[i]; if (!inside(nx, ny)) continue; if (mp[nx][ny]) { if (t.s + A + (i < 2) * B < d[nx][ny][m]) { d[nx][ny][m] = t.s + A + (i < 2) * B; q.push((Node){nx, ny, m, d[nx][ny][m]}); } continue; } if (t.s + (i < 2) * B < d[nx][ny][t.t - 1]) { d[nx][ny][t.t - 1] = t.s + (i < 2) * B; q.push((Node){nx, ny, t.t - 1, d[nx][ny][t.t - 1]}); } if (!(nx == n && ny == n) && t.s + A + C + (i < 2) * B < d[nx][ny][m]) { d[nx][ny][m] = t.s + A + C + (i < 2) * B; q.push((Node){nx, ny, m, d[nx][ny][m]}); } } } LL res = inf; for (int i = 0; i <= m; i++) chkmin(res, d[n][n][i]); write(res); return 0; }
|