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
#include <iostream>
#include <vector>
#include <string>
#include <array>
#include <queue>
#include <limits>
#include <iomanip>
#include <set>

using namespace std;

struct Distance {
    int a, b;
};

struct Point {
    int i, j;
};

int n, m, k;

int intINF = std::numeric_limits<int>::max();

vector<string> map;
array<vector<vector<Distance>>, 3> distanceMap;

void bfs(int a, int b, int bfsIndex)
{
    auto& dmap = distanceMap[bfsIndex]; // distance map

    auto cmp = [a, b, &dmap](Point af, Point bf) {
        Distance afd = dmap[af.i][af.j];
        Distance bfd = dmap[bf.i][bf.j];
        return (afd.a*a + afd.b*b) < (bfd.a*a + bfd.b*b);
    };

    dmap[1][1] = {0, 0};
    priority_queue<Point, vector<Point>, decltype(cmp)> nextf(cmp); // next field
    nextf.push(Point({1, 1}));

    int diri[] {0, -1, 0, 1};
    int dirj[] {-1, 0, 1, 0};

    while (!nextf.empty())
    {
        auto curp = nextf.top();
        nextf.pop();
        // auto curd = dmap[curp.i][curp.j];

        for (int dirid = 0; dirid < 4; ++dirid)
        {
            Point np = {curp.i+diri[dirid], curp.j+dirj[dirid]};
            if (map[np.i][np.j] == '.')
            {
                Distance& curDist = dmap[np.i][np.j];
                Distance newDist = dmap[curp.i][curp.j];
                if (dirid <= 1)
                    newDist.b++;
                else
                    newDist.a++;

                if ((newDist.a*a + newDist.b*b) < (curDist.a*a + curDist.b*b) || curDist.a == intINF)
                {
                    curDist = newDist;
                    nextf.push(np);
                }
            }
        }
    }
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    cin >> n >> m >> k;

    map.push_back(string(m+2, 'X'));
    for (int i = 0; i < n; ++i)
    {
        string row;
        cin >> row;
        row.insert(row.begin(), 'X');
        row.push_back('X');
        // map.at(i) = row;
        map.push_back(row);
    }
    map.push_back(string(m+2, 'X'));

    distanceMap[0] = vector<vector<Distance>>(n+2, vector<Distance>(m+2, {intINF, intINF}));
    distanceMap[1] = distanceMap[0];
    distanceMap[2] = distanceMap[0];

    bfs(2, 1, 0);
    bfs(1, 1, 1);
    bfs(1, 2, 2);

    auto printDist = [&](vector<vector<Distance>> dmap, int a, int b) {
        for (int i = 0; i < dmap.size(); ++i)
        {
            for (int j = 0; j < dmap[i].size(); ++j)
            {
                int val;
                if (dmap[i][j].a == intINF)
                    val = intINF;
                else
                    val = dmap[i][j].a*a + dmap[i][j].b*b;

                if (val == intINF)
                    cout << left << setw(3) << ' ';
                else
                    cout << left << setw(3) << val;
            }
            cout << '\n';
            cout << '\n';
        }
    };

    // printDist(distanceMap[0], 2, 1);
    // cout << "\n";
    // printDist(distanceMap[1], 1, 1);
    // cout << "\n";
    // printDist(distanceMap[2], 1, 2);


    multiset<int> times;
    while (k-- > 0)
    {
        int a, b;
        cin >> a >> b;

        int bfsIndex;
        if (a > b)
        {
            bfsIndex = 0;
        }
        else if (a == b)
        {
            bfsIndex = 1;
        }
        else if (a < b)
        {
            bfsIndex = 2;
        }

        Distance dist = distanceMap[bfsIndex][n][m];
        times.insert(
                dist.a*a + dist.b*b
        );
    }

    // for (auto e : times)
    // {
    //     cout << e << '\n';
    // }
    cout << (*times.begin()) << " " << times.count(*times.begin()) << '\n';


    // cout << "\n";
}