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
| #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<queue> using namespace std;
#define LL long long const int CN = 1e3 + 3; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; const LL INF = 0x3f3f3f3f3f3f3f3f;
class DJ {public: int x, y; LL v; bool operator < (const DJ & a) const {return v > a.v;}}; DJ mk(int a, int b, LL c) {DJ d; d.x = a, d.y = b, d.v = c; return d;} priority_queue<DJ> Q;
bool vis[CN][CN]; void SP(int n, int m,int sx, int sy, LL d[][CN], LL dis[][CN]){ memset(vis, 0, sizeof(vis)); Q.push( mk(sx, sy, dis[sx][sy] = d[sx][sy]) ); while(!Q.empty()){ int x = Q.top().x, y = Q.top().y; Q.pop(); if(vis[x][y]) continue; vis[x][y] = true; for(int k = 0; k < 4; k++){ int vx = x + dx[k], vy = y + dy[k]; if(!vx || !vy || vx > n || vy > m || vis[vx][vy]) continue; if(dis[vx][vy] > dis[x][y] + d[vx][vy]){ dis[vx][vy] = dis[x][y] + d[vx][vy]; Q.push( mk(vx, vy, dis[vx][vy]) ); } } } }
int n, m, a, b, c; LL d[CN][CN], da[CN][CN], db[CN][CN], dc[CN][CN];
int main() { n = read(), m = read(), a = read(), b = read(), c = read(); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) d[i][j] = read();
memset(da, 0x3f, sizeof(da)), SP(n, m, 1, a, d, da); memset(db, 0x3f, sizeof(db)), SP(n, m, n, b, d, db); memset(dc, 0x3f, sizeof(dc)), SP(n, m, n, c, d, dc);
LL ans = INF; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) ans = min(ans, da[i][j] + db[i][j] + dc[i][j] - 2ll * d[i][j]); printf("%lld", ans);
return 0; }
|