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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

int INF = 2000*2000+1;


struct pole {
    int x,
        y,
        dist, // mozliwe => -1, przeszkoda => -2
        visited,
        enabled;
    pole  *from; // skad droga    
};


void print_polex(const pole *p) {
    cout << "(" << p->x << "," << p->y << ") dist=" << p->dist;
    // if (p.from != nullptr) {
    //     cout << " droga_z (" << p.from->x << "," << p.from->y << ")";    
    // }
    cout << '\n';

}

pole **mm;

struct APtrComp
{
  bool operator()(const pole *a, const pole *b) const  { 
       return a->dist > b->dist;
    };
};

void find_path(int y, int x) {

    priority_queue<pole*, std::vector<pole*>, APtrComp> pq;
    
   

    pole *n = new pole();
    n->x = 0;
    n->y = 0;
    n->dist = 0;
    n->from = NULL;
    pq.push(n);


    while (!pq.empty()) {
        auto d = pq.top();

        if (d->x == x && d->y == y) {
            return;
        }

        mm[d->y][d->x].visited = 1;

        pq.pop();
        
        for (int dd=1;dd>=-1;dd-=2) {
            int dx = d->x + dd;
        
            if (dx >= 0 && dx <= x) {
                pole *nxt = &mm[d->y][dx];
        
                if (nxt->enabled && !nxt->visited) {
                    if (nxt->dist > d->dist+1) {
                        pole *n = new pole();
                        n->x = nxt->x;
                        n->y = nxt->y;
                        n->dist = d->dist + 1;
                        n->from = d;
                        nxt->from = d;
                        nxt->dist = n->dist;
                        pq.push(n);
                    }
                }
            }
            int dy = d->y + dd;
            
            if (dy >= 0 && dy <= y) {
                pole *nxt = &mm[dy][d->x];
            
                if (nxt->enabled && !nxt->visited) {
                    if (nxt->dist > d->dist+1) {
                        pole *n = new pole();
                        n->x = nxt->x;
                        n->y = nxt->y;
                        n->dist = d->dist + 1;
                        n->from = d;
                        nxt->from = d;
                        nxt->dist = n->dist;
                        pq.push(n);

                    }
                }
            }

        }
    }


}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, m, k;
    cin >> n >> m >> k;
    mm = new pole*[n];
    for (int i=0;i<n;i++) {
       
        mm[i] = new pole[m];
        for (int j=0;j<m;j++) {
            char p;
            cin >> p;

            mm[i][j].x = j;
            mm[i][j].y = i;
            mm[i][j].from = NULL;
            mm[i][j].dist = INF;
            mm[i][j].visited = 0;
            if (p == 'X') mm[i][j].enabled = 0; else mm[i][j].enabled = 1;

        }
    }

    find_path(n-1, m-1);

    int px = m-1;
    int py = n-1;
    long long l = 0, h = 0;
    auto f = mm[py][px].from;
    while (f) {
        if (f->x > px || f->y > py) l++;
        if (f->x < px || f->y < py) h++;
        px = f->x;
        py = f->y;
        f = f->from;
    }

    // cout << "l=" << l << " h=" << h << "\n";
/* 
    for (int i=0;i<n;i++) {
        for (int j=0;j<m;j++) {
            char p = ' ';
            if (mm[i][j].dist == -2) p = 'X';
            if (mm[i][j].enabled == -2) p = '*';
            cout << p;
        }
        cout << "\n";
    }
*/
    vector<long long> costs(k);
    for (int i=0;i<k;i++) {
        long long a,b;
        cin >> a >> b;
        costs[i] = a * h + b * l;
        //cout << "c" << i << " = " << costs[i] << '\n';
    }
    sort(costs.begin(), costs.end());
    long long ch = costs[0];
    auto up = upper_bound(costs.begin(), costs.end(), ch);
    int res = up - costs.begin();


    cout << ch << ' ' << res << '\n';
}