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 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <bits/stdc++.h> using namespace std; #define int long long #define resetIO(x) \ freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout) #define debug(fmt, ...) \ fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) template <class _Tp> inline _Tp& read(_Tp& x) { bool sign = false; char ch = getchar(); long double tmp = 1; for (; !isdigit(ch); ch = getchar()) sign |= (ch == '-'); for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48); if (ch == '.') for (ch = getchar(); isdigit(ch); ch = getchar()) tmp /= 10.0, x += tmp * (ch ^ 48); return sign ? (x = -x) : x; } template <class _Tp> inline void write(_Tp x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar((x % 10) ^ 48); } using pii = pair<int, int>; const int MAXN = 150; const int MAXK = 15; const int MAXV = 2e5 + 10; const int MAXE = 4e6 + 10; const int INF = 0x3f3f3f3f3f3f3f3f; const int DX[4] = {-1, 0, 0, 1}; const int DY[4] = {0, -1, 1, 0}; struct Edge { int v, cost; } edge[MAXE]; int n, k, a, b, c, mp[MAXN][MAXN]; int s, t, num, pt[MAXN][MAXN][MAXK]; int tot, head[MAXV], nxt[MAXE], dis[MAXV]; void addedge(int u, int v, int cost) { edge[++tot] = {v, cost}; nxt[tot] = head[u], head[u] = tot; } int dijkstra(int s, int t) { priority_queue<pii, vector<pii>, greater<pii>> que; fill(dis, dis + MAXV, INF); dis[s] = 0, que.push({dis[s], s}); while (!que.empty()) { int u = que.top().second, d = que.top().first; que.pop(); if (d > dis[u]) continue; for (int i = head[u]; i; i = nxt[i]) { int v = edge[i].v; if (dis[v] > dis[u] + edge[i].cost) { dis[v] = dis[u] + edge[i].cost; que.push({dis[v], v}); } } } return dis[t]; } signed main() { read(n), read(k), 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 l = 0; l <= k; ++l) pt[i][j][l] = ++num; } } s = pt[1][1][k], t = ++num; for (int i = 0; i <= k; ++i) addedge(pt[n][n][i], t, 0); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (mp[i][j]) { for (int l = 0; l <= k; ++l) addedge(pt[i][j][l], pt[i][j][k], a); for (int d = 0; d < 4; ++d) { int x = i + DX[d], y = j + DY[d]; if (x < 1 || y < 1 || x > n || y > n) continue; addedge(pt[i][j][k], pt[x][y][k - 1], d > 1 ? 0 : b); } } else { for (int d = 0; d < 4; ++d) { int x = i + DX[d], y = j + DY[d]; if (x < 1 || y < 1 || x > n || y > n) continue; for (int l = 1; l <= k; ++l) addedge(pt[i][j][l], pt[x][y][l - 1], d > 1 ? 0 : b); } } addedge(pt[i][j][0], pt[i][j][k], a + c); } } write(dijkstra(s, t)), putchar('\n'); return 0; }
|