#include <cstdio> #include <cstring> #include <queue> using namespace std; struct Rhine_Lab { Rhine_Lab() { freopen("expand.in", "r", stdin); freopen("expand.out", "w", stdout); } }; Rhine_Lab Ptilopsis_w;
const int P = 25; const int N = 305; const int dx[] = {1, -1, 0, 0, -1, -1, 1, 1}; const int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};
struct node { int x, y; int dis, sum; }; bool operator < (node a, node b) { if(a.dis != b.dis) return a.dis > b.dis; else return a.sum < b.sum; }
int n, m, s; int sx, sy, p; int mp[N][N]; int siz[N][N]; int dis[P][P]; int sum[P][P]; int qry[N][N]; bool vis[N][N]; int f[1<<16][16]; int g[1<<16][16]; int px[P], py[P];
void get_size(); void dijkstra(int sx, int sy, int id);
#define pos(i) (1<<(i-1)) #define lowbit(i) (i&-i)
int main() { scanf("%d%d%d", &n, &m, &s); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) scanf("%d", &mp[i][j]); scanf("%d%d%d", &sx, &sy, &p); sx++, sy++; for(int i = 1; i <= p; i++) { scanf("%d%d", &px[i], &py[i]); px[i]++, py[i]++; qry[px[i]][py[i]] = i; } get_size();
for(int i = 1; i <= p; i++) dijkstra(px[i], py[i], i); dijkstra(sx, sy, 0); memset(f, 0x3f, sizeof(f)); memset(g, -0x3f, sizeof(g)); for(int i = 1; i <= p; i++) { f[pos(i)][i] = dis[0][i]; g[pos(i)][i] = sum[0][i]; } for(int i = 1; i < (1<<p); i++) { if(i == lowbit(i)) continue; for(int j = 1; j <= p; j++) { if(!(i&pos(j))) continue; int k = i-pos(j); for(int s = 1; s <= p; s++) { if(!(k&pos(s))) continue; if(f[i][j] > f[k][s]+dis[s][j]) { f[i][j] = f[k][s]+dis[s][j]; g[i][j] = g[k][s]+sum[s][j]; } else if(f[i][j] == f[k][s]+dis[s][j]) { g[i][j] = max(g[i][j], g[k][s]+sum[s][j]); } } } } int id = 0; for(int i = 1; i <= p; i++) if(f[(1<<p)-1][i] < f[(1<<p)-1][id]) id = i; printf("%d %d", f[(1<<p)-1][id], g[(1<<p)-1][id] +siz[sx][sy] ); }
void get_size() { queue<node> q; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { siz[i][j] = s; if(mp[i][j]) { q.push({i, j, -1}); siz[i][j] = -1; } } for(int i = 1; i <= n; i++) q.push({i, 0, -1}), q.push({i, m+1, -1}); for(int j = 1; j <= m; j++) q.push({0, j, -1}), q.push({n+1, j, -1}); while(!q.empty()) { node a = q.front(); q.pop(); for(int k = 0; k < 8; k++) { int nx = a.x+dx[k]; int ny = a.y+dy[k]; if(nx < 1 or nx > n or ny < 1 or ny > m) continue; if(mp[nx][ny] == 1) continue; if(siz[nx][ny] > a.dis+1) { siz[nx][ny] = a.dis+1; q.push({nx, ny, a.dis+1}); } } } } void dijkstra(int sx, int sy, int id) { memset(vis, 0, sizeof(vis)); priority_queue<node> q; q.push({sx, sy, 0, 0}); while(!q.empty()) { node a = q.top(); q.pop(); if(vis[a.x][a.y]) continue; vis[a.x][a.y] = true; if(qry[a.x][a.y] != 0) { int t = qry[a.x][a.y]; dis[id][t] = a.dis; sum[id][t] = a.sum; } for(int k = 0; k < 4; k++) { int nx = a.x+dx[k]; int ny = a.y+dy[k]; if(nx < 1 or nx > n or ny < 1 or ny > m) continue; if(mp[nx][ny] == 1) continue; q.push({nx, ny, a.dis+1, a.sum+siz[nx][ny]}); } } }
|