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
#include <cstdio>
#include <deque>
#include <vector>

bool good[2010][2010];
int distance[2010][2010];
std::vector<std::pair<int, int> > STEPS = {{1,0}, {-1,0}, {0,1}, {0, -1}};

int main() {
    int N,M,K;
    scanf("%d %d %d\n",&N,&M,&K);
    
    for (int i=1; i<=N; ++i) {
        for (int j=1; j<=M; ++j) {
            char c;
            scanf("%c", &c);
            good[i][j] = (c=='.');
        }
        scanf("\n");
    }

    std::deque<std::pair<int, int> > Q;
    distance[1][1] = 1;
    Q.emplace_back(1,1);

    while (!Q.empty()) {
        auto pos = Q.front();
        Q.pop_front();

        for (auto &step : STEPS) {
            if (distance[pos.first+step.first][pos.second+step.second] == 0 && good[pos.first+step.first][pos.second+step.second]) {
                distance[pos.first+step.first][pos.second+step.second] = distance[pos.first][pos.second] + 1;
                Q.emplace_back(pos.first+step.first, pos.second+step.second);
            }
        }
    }

    int D = distance[N][M]-1;
    int up = N-1 + M-1;
    int down = (D-up)/2;
    up += down;

    long long int result = 2010*2010*1000000000LL;
    int cnt = 0;
    
    for (int i=0; i<K; ++i) {
        long long int a,b,cost;
        scanf("%lld %lld", &a, &b);

        cost = a*up + b*down;
        if (cost == result) {
            cnt++;
        } else if (cost < result) {
            result = cost;
            cnt = 1;
        }
    }

    printf("%lld %d\n", result, cnt);
    return 0;
}