Only#4, #7, #8AC, 其他全部TLE
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 1010;
const LL INF = 1e18;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int n, m, A, B, C;
int r[N][N];
LL dist[N][N], res = INF;
bool st[N][N];
struct sky{
int x, y;
LL d;
bool operator < (const sky &t) const{
return d > t.d;
}
};
LL dijkstra(sky S, sky T){
memset(st, 0, sizeof st);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
dist[i][j] = INF;
dist[S.x][S.y] = 0;
priority_queue<sky> q;
q.push(S);
while (q.size()){
auto t = q.top();
q.pop();
if (st[t.x][t.y]) continue;
st[t.x][t.y] = true;
for (int i = 0; i < 4; i ++ ){
int nx = t.x + dx[i], ny = t.y + dy[i];
if (nx < 1 || ny < 1 || nx > n || ny > m || st[nx][ny]) continue;
if (dist[nx][ny] > dist[t.x][t.y] + r[nx][ny]){
dist[nx][ny] = dist[t.x][t.y] + r[nx][ny];
q.push({nx, ny, dist[nx][ny]});
}
}
}
if (dist[T.x][T.y] == INF) return -1;
return dist[T.x][T.y];
}
int main(){
scanf("%d%d%d%d%d", &n, &m, &A, &B, &C);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
scanf("%d", &r[i][j]);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ ){
LL a = dijkstra({1, A, 0}, {i, j, 0}), b = dijkstra({i, j, 0}, {n, B, 0}), c = dijkstra({i, j, 0}, {n, C, 0});
if (~a && ~b && ~c) res = min(res, a + b + c);
}
printf("%lld\n", res + r[1][A]);
return 0;
}