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

using namespace std;
using ll = long long;
int n, m, k;
int mapa[2007][2007];
ll koszt[2007][2007];
int X[] = {0, 0, 1, -1};
int Y[] = {1, -1, 0, 0};
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    string s;

    cin>>n>>m>>k;
    for (int i=1; i<=n; i++){
        cin>>s;
        for (int j=0; j<m; j++){
            if (s[j]=='.')
                mapa[i][j+1]=1;
        }
    }

    queue<pair<int, int>> q;
    q.push(make_pair(1,1));
    while (!q.empty()){
        auto t = q.front();
        q.pop();
        if (mapa[t.first][t.second])
        {
            mapa[t.first][t.second]=0;
            for (int i=0; i<4; i++ ){
                int x= t.first+X[i], y = t.second+Y[i];
                if (koszt[x][y] == 0 ){
                    q.push(make_pair(x, y));
                    koszt[x][y] = koszt[t.first][t.second] +1;
                }

            }
            if (t.first == n && t.second == m) break;
        }

    }

    ll mini = INT64_MAX;
    int ile=1;
    ll pod = (koszt[n][m] - n - m + 2)/2;
    ll a, b;
    for (int i = 0; i<k; i++){
        cin >>  a >>b;
        ll wyn = a*(koszt[n][m] - pod)  + b*pod;
        if (wyn < mini){
            mini = wyn;
            ile = 1;
        } else if (wyn == mini)
            ile++;

    }
    cout<<mini<<" "<<ile<<"\n";

}