1 #include <cstdio> 2 #include <queue> 3 #include <utility> 4 #include <iostream> 5 #include <cstring> 6 using namespace std; 7 typedef pair<int, int> PII; 8 9 const int MAXN = 205; 10 11 int mat[MAXN][MAXN], tx[15], ty[15]; 12 int dis[MAXN][MAXN], di[15][15]; 13 bool ist[MAXN][MAXN]; 14 int post[MAXN][MAXN]; 15 int n, m, k; 16 17 #define pos(x, y) (x*MAXN+y) 18 19 int fx[4] = {-1,0,1,0}; 20 int fy[4] = {0,1,0,-1}; 21 22 void min_path(int st_x, int st_y, int now) { 23 priority_queue<PII> que; 24 que.push(make_pair(-mat[st_x][st_y], pos(st_x, st_y))); 25 memset(dis, 0x3f, sizeof(dis)); 26 dis[st_x][st_y] = mat[st_x][st_y]; 27 while(!que.empty()) { 28 int abc = -que.top().first, tmp = que.top().second; que.pop(); 29 int x = tmp / MAXN, y = tmp % MAXN; 30 if(abc != dis[x][y]) continue; 31 for(int i = 0; i < 4; ++i) { 32 int newx = x + fx[i], newy = y + fy[i]; 33 if(0 <= newx && newx < n && 0 <= newy && newy < m) { 34 if(mat[newx][newy] == -1) continue; 35 if(dis[newx][newy] > mat[newx][newy] + dis[x][y]) { 36 dis[newx][newy] = mat[newx][newy] + dis[x][y]; 37 que.push(make_pair(-dis[newx][newy], pos(newx, newy))); 38 if(ist[newx][newy] && dis[newx][newy] < di[now][post[newx][newy]]) 39 di[now][post[newx][newy]] = dis[newx][newy]; 40 } 41 } 42 else if(dis[x][y] < di[now][k]) di[now][k] = dis[x][y]; 43 } 44 } 45 } 46 47 int dp[14][20000]; 48 int ans, sum; 49 50 int dfs(int u, int use) { 51 if(dp[u][use] < 0x3f3f3f3f) return dp[u][use]; 52 if(use == 0) { 53 return dp[u][use] = di[u][k]; 54 } 55 for(int i = 0; i < k; ++i) { 56 if(use & (1 << i)) 57 dp[u][use] = min(dp[u][use], di[u][i] + dfs(i, use ^ (1 << i))); 58 } 59 return dp[u][use]; 60 } 61 62 void solve() { 63 memset(dp, 0x3f, sizeof(dp)); 64 ans = 0x7fff7fff; 65 for(int i = 0; i < k; ++i) 66 ans = min(ans, di[i][k] + dfs(i, (1 << i) ^ ((1 << k) - 1))); 67 } 68 69 void printdi() { 70 for(int i = 0; i < k; ++i) { 71 for(int j = 0; j <= k; ++j) printf("%d ", di[i][j]); 72 printf("\n"); 73 } 74 } 75 76 int main() { 77 int T; 78 scanf("%d", &T); 79 while(T--) { 80 scanf("%d%d", &n, &m); 81 for(int i = 0; i < n; ++i) 82 for(int j = 0; j < m; ++j) scanf("%d", &mat[i][j]); 83 scanf("%d", &k); 84 memset(ist, 0, sizeof(ist)); 85 sum = 0; 86 for(int i = 0; i < k; ++i) { 87 scanf("%d%d", &tx[i], &ty[i]); 88 ist[tx[i]][ty[i]] = true; 89 post[tx[i]][ty[i]] = i; 90 sum += mat[tx[i]][ty[i]]; 91 } 92 memset(di, 0x3f, sizeof(di)); 93 for(int i = 0; i < k; ++i) min_path(tx[i], ty[i], i); 94 //printdi(); 95 solve(); 96 printf("%d\n", ans - sum); 97 } 98 }