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
#include <iostream>
#include <limits>
#include <queue>
#include <set>
#include <tuple>

int main() {
    int32_t n, m, k;
    std::cin >> n >> m >> k;

    bool arr[n][m];
    for (int32_t i = 0; i < n; ++i) {
        std::string data;
        std::cin >> data;
        for (int32_t j = 0; j < m; ++j) {
            arr[i][j] = data[j] == 'X';
        }
    }

    std::set<std::pair<int32_t, int32_t>> seen;
    std::priority_queue<std::tuple<int32_t, int32_t, int32_t>> queue;
    queue.emplace(0, 0, 0);
    for (;;) {
        int32_t value, i, j;
        std::tie(value, i, j) = queue.top();
        if (i == n - 1 && j == m - 1) { break; }
        queue.pop();

        if (arr[i][j]) { continue; }
        if (seen.count({i, j}) == 1) { continue; }

        seen.emplace(i, j);

        if (i != 0) { queue.emplace(value - 1, i - 1, j); }
        if (j != 0) { queue.emplace(value - 1, i, j - 1); }

        if (i != n - 1) { queue.emplace(value, i + 1, j); }
        if (j != m - 1) { queue.emplace(value, i, j + 1); }
    }
    int32_t minus = -std::get<0>(queue.top());
    int32_t plus = minus + (n - 1) + (m - 1);

    int64_t best = std::numeric_limits<int64_t>::max();
    int32_t count = 0;
    for (int32_t i = 0; i < k; ++i) {
        int64_t minus_c, plus_c;
        std::cin >> plus_c >> minus_c;

        int64_t cost = minus * minus_c + plus * plus_c;
        if (cost < best) {
            best = cost;
            count = 1;
        } else if (cost == best) {
            count += 1;
        }
    }

    std::cout << best << " " << count << "\n";
}