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
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define st first
#define nd second
int dist[2005][2005];
bool nie[2005][2005];
ll a[(int)1e6 + 10],b[(int)1e6 + 10];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	for(int i = 1; i <= n; i++){
		string s;
		cin >> s;
		for(int j = 1; j <= m; j++)
			if(s[j - 1] == 'X')
				nie[i][j] = 1;
	}
	for(int j = 0; j <= m + 1; j++){
		nie[0][j] = 1;
		nie[n + 1][j] = 1;
	}
	for(int i = 0; i <= n + 1; i++){
		nie[i][0] = 1;
		nie[i][m + 1] = 1;
	}
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			dist[i][j] = 1e9;
	dist[1][1] = 0;
	queue<pair<int,int> > q;
	q.push({1,1});
	vector<int> vi = {0,0,-1,1},vj = {1,-1,0,0};
	while(q.size()){
		pair<int,int> v = q.front();
		q.pop();
		pair<int,int> w;
		for(int i = 0; i < 4; i++){
			w = v;
			w.st += vi[i];
			w.nd += vj[i];
			if(nie[w.st][w.nd] == 0 && dist[w.st][w.nd] == 1e9){
				dist[w.st][w.nd] = dist[v.st][v.nd] + 1;
				q.push(w);
			}
		}
	}
	ll ans = 1e18;
	ll t1,t2;
	t1 = m + n - 2;
	t2 = dist[n][m] - t1;
	t2 /= 2;
	for(int i = 1; i <= k; i++){
		cin >> a[i] >> b[i];
		ans = min(ans,a[i] * t1 + (a[i] + b[i]) * t2);
	}
	int cnt = 0;
	for(int i = 1; i <= k; i++)
		if(ans == a[i] * t1 + (a[i] + b[i]) * t2)
			cnt++;
	cout << ans << ' ' << cnt;
	return 0;
}