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 <bits/stdc++.h>
using namespace std;

#define mp make_pair

const int N = 2005;

int n, m, k;
char S[N][N];
int dp[N][N];

int main()
{
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 0; i < n; i++) {
		scanf("%s", &S[i]);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			dp[i][j] = N*N;
		}
	}
	queue<pair<int, int> > order;
	order.push(make_pair(0, 0));
	dp[0][0] = 0;
	while (!order.empty()) {
		pair<int, int> v = order.front();
		order.pop();
		for (auto p : {mp(1, 0), mp(0, 1), mp(-1, 0), mp(0, -1)}) {
			pair<int, int> u = make_pair(v.first + p.first, v.second + p.second);
			if (u.first < 0 || u.first > n - 1 || u.second < 0 || u.second > m - 1 || S[u.first][u.second] == 'X')	continue;
			if (dp[v.first][v.second] + 1 < dp[u.first][u.second]) {
				dp[u.first][u.second] = dp[v.first][v.second] + 1;
				order.push(u);
			}
		}
	}
	long long g = n + m - 2;
	long long l = (dp[n - 1][m - 1] - g) / 2;
	g += l;
	long long best = 1e18;
	int ile = 0;
	while (k--) {
		long long a, b;
		scanf("%lld%lld", &a, &b);
		if (a * g + b * l < best ) {
			ile = 0;
			best = a * g + b * l;
		}
		if (a * g + b * l == best) {
			ile++;
		}
	}
	printf("%lld %d\n", best, ile);
}