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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <cstdio>
#include <queue>
#include <utility>
#include <tuple>

using namespace std;

const int NMAX = 2000 + 7;
int n, m, k;

char field[NMAX][NMAX];

queue<tuple<int, int, int, int> > q_e;

int main() {
	int up_end = -1, down_end = -1;
	scanf("%d %d %d", &n, &m, &k);

	for (int i = 0; i < n; i++)
		scanf("%s", field[i]);

	q_e.push(make_tuple(0, 0, 0, 0));

	while(!q_e.empty()) {
		tuple<int, int, int, int> p = q_e.front();
		int x = get<0>(p), y = get<1>(p);
		int up = get<2>(p), down = get<3>(p);
		q_e.pop();
	//	printf("%d %d\n", x, y);

		if (0 <= x && x < n && 0 <= y && y < m && field[x][y] == '.') {
			field[x][y] = 'O';

	//		printf("|| %d %d %d %d || \n", x, y, up, down);
			if(x == n - 1 && y == m - 1) {
				up_end = up;
				down_end = down;
				break;
			}

			if (0 < x)
				q_e.push(make_tuple(x - 1, y, up, down + 1));
			if (x < n - 1)
				q_e.push(make_tuple(x + 1, y, up + 1, down));
			if (0 < y)
				q_e.push(make_tuple(x, y - 1, up, down + 1));
			if (y < m - 1)
				q_e.push(make_tuple(x, y + 1, up + 1, down));

			/*for (int i = -1; i <= 1; i++)
			{
				q_e.push(pair(x + i, y + j));
						q_e.push(pair(x + i, y + j));
				for(int j = -1; j <=1; j++) {
					if(i != 0 && j != 0)
						q_e.push(pair(x + i, y + j));
				}
			}*/
		}

	}
	//printf("%d %d\n", up_end, down_end);

	unsigned long long int w_min =  -1;
	int w_min_counter = 0;
	int a, b;
	for(int i = 0; i < k; i++) {
		scanf("%d %d", &a, &b);
		unsigned long long int time = ((unsigned long long) a) * up_end + ((unsigned long long) b) * down_end;
		if (time < w_min) {
			w_min = time;
			w_min_counter = 1;
		} else if (time == w_min) {
			w_min_counter++;
		}
	}
	printf("%lld %d\n", w_min, w_min_counter);

	//printf("%s\n",field[i]);
	return 0;
}