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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <cstdio>
#include <climits>
#include <deque>

#define N 2002
#define T 1000002

using namespace std;

int n, m, k;
char grid[N * N];

unsigned long long minRet[N * N];
deque<int> q;
pair<long long, long long> times[T];
unsigned long long res[T];

void bfs() {
    auto v = q.back();
    q.pop_back();

    int a = v % m;
    int b = v / m;

    if (a > 0 && grid[v - 1] != 'X') {
        if (minRet[v] + 1 < minRet[v - 1]) {
            minRet[v - 1] = minRet[v] + 1;
            q.push_back(v - 1);
        }
    }
    if (a < m - 1 && grid[v + 1] != 'X') {
        if (minRet[v] < minRet[v + 1]) {
            minRet[v + 1] = minRet[v];
            q.push_front(v + 1);
        }
    }
    if (b > 0 && grid[v - m] != 'X') {
        if (minRet[v] + 1 < minRet[v - m]) {
            minRet[v - m] = minRet[v] + 1;
            q.push_back(v - m);
        }
    }
    if (b < n - 1 && grid[v + m] != 'X') {
        if (minRet[v] < minRet[v + m]) {
            minRet[v + m] = minRet[v];
            q.push_front(v + m);
        }
    }
}

int main() {
    scanf("%d %d %d\n", &n, &m, &k);

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            scanf("%c", &grid[i * m + j]);
        }
        scanf("\n");
    }

    for (int i = 0; i < n * m; ++i) {
        minRet[i] = ULLONG_MAX;
    }

    q.push_back(0);
    minRet[0] = 0;
    while (!q.empty()) {
        bfs();
    }

    for (int i = 0; i < k; ++i) {
        long long a, b;
        scanf("%Ld %Ld", &a, &b);
        times[i] = make_pair(a, b);
    }

    unsigned long long rCoef = minRet[n * m - 1];
    unsigned long long uCoef = minRet[n * m - 1] + n - 1 + m - 1;
    unsigned long long minTime = ULLONG_MAX;

    for (int i = 0; i < k; i++) {
        res[i] = uCoef * times[i].first + rCoef * times[i].second;
        minTime = min(minTime, res[i]);
    }

    unsigned long long count = 0;
    for (int i = 0; i < k; i++) {
        if (res[i] == minTime) {
            count++;
        }
    }

    printf("%Ld %Ld\n", minTime, count);
    return 0;
}