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
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <unordered_map>
#include <set>

using ull = unsigned long long;
enum class walk_type {none, a, b};
struct V 
{
    int i, j, t;
    V* s = nullptr;
    char type;
    walk_type walk = walk_type::none;
    bool operator <(const V& pt) const
    {
        if (this->t == pt.t)
        {
            if (this->i == pt.i)
                return this->j > pt.j;
            return this->i > pt.i;
        }

        return this->t > pt.t;
    }
};

const int INF = std::numeric_limits<int>().max();
int n, m, k;
std::vector<std::vector<V>> graph;

void bfs()
{
    std::queue<V*> q;
    graph[0][0].t = 0;
    q.push(&graph[0][0]);

    while(!q.empty()) 
    {
        auto y = q.front();
        q.pop();

        //up
        if (y->i < n - 1 && graph[y->i + 1][y->j].type == '.')
        {
            auto child = &graph[y->i + 1][y->j];
            if(child->t == -1)
            {
                child->t = y->t + 1;
                child->s = y;
                child->walk = walk_type::a;
                q.push(child);
            }
        }

        //right
        if (y->j < m - 1 && graph[y->i][y->j + 1].type == '.')
        {
            auto child = &graph[y->i][y->j + 1];
            if(child->t == -1)
            {
                child->t = y->t + 1;
                child->s = y;
                child->walk = walk_type::a;
                q.push(child);
            }
        }

        //down
        if (y->i > 0 && graph[y->i - 1][y->j].type == '.')
        {
            auto child = &graph[y->i - 1][y->j];
            if(child->t == -1)
            {
                child->t = y->t + 1;
                child->s = y;
                child->walk = walk_type::b;
                q.push(child);
            }
        }

        //left
        if (y->j > 0 && graph[y->i][y->j - 1].type == '.')
        {
            auto child = &graph[y->i][y->j - 1];
            if(child->t == -1)
            {
                child->t = y->t + 1;
                child->s = y;
                child->walk = walk_type::b;
                q.push(child);
            }
        }
    }
}

int main()
{
    std::iostream::sync_with_stdio(false);
    std::string line;
    std::cin >> n >>m >> k;
    graph.resize(n);

    for (int i = 0; i < n; ++i)
    {
        graph[i].resize(m);
        std::cin >> line;

        for (int j = 0; j < m; ++j)
        {
            auto& g = graph[i][j];
            g.i = i;
            g.j = j;
            g.t = -1;
            g.type = line[j];
        }
    }

    bfs();

    ull a_count = 0, b_count = 0;
    V* node = &graph[n - 1][m - 1];
    while(node->i != 0 || node->j != 0)
    {
        if(node->walk == walk_type::a)
            ++a_count;
        else if(node->walk == walk_type::b)
            ++b_count;
        else
            return -1;

        node = node->s;
    }

    ull a, b;
    ull fastest_count = 0, tmp;
    ull fastest_value = std::numeric_limits<ull>().max();
    for(int i = 0; i < k; ++i)
    {
        std::cin >> a >> b;
        tmp = a_count * a + b_count * b;
        if(tmp < fastest_value)
        {
            fastest_count = 1;
            fastest_value = tmp;
        }
        else if(tmp == fastest_value)
        {
            ++fastest_count;
        }
    }

    std::cout << fastest_value << ' ' << fastest_count;

    return 0;
}