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
174
175
176
177
178
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

int Time(int v, int u, int a, int b)
{
    if (u > v)
        return a;
    else
        return b;
}

int Relaksacja(int* d, int v, int u, int a, int b, int* distNodes, int countNodes)
{
    int result = countNodes;
    int time = Time(v, u, a, b);
    if (d[u] < 1 || d[v] + time < d[u])
    {
        d[u] = d[v] + time;
        distNodes[countNodes] = u;
        result++;
    }
    return result;
}

int Take(int* d, bool* o, int n, int m, int *distNodes, int countNodes)
{
    int min = -1;
    int index = 0;
    for (int i = 0; i < countNodes; i++)
    {
        int node = distNodes[i];
        if (!o[node] && d[node] >= 1)
        {
            if (min < 0 || d[node] < min)
            {
                min = d[node];
                index = node;
            }
        }
    }
    return index;
}

int Dijikstra(int v0, int** tab, int a, int b, int n, int m, int count)
{
    int* dist = (int*)malloc(n * m * sizeof(int));
    bool* odw = (bool*)malloc(n * m * sizeof(bool));
    int* odwList = (int*)malloc(n * m * sizeof(int));

    int* distNodes = (int*)malloc(n * m * sizeof(int));
    int countNodes = 0;

    dist[v0] = 1;
    distNodes[0] = v0;
    countNodes++;
    while (count > 0)
    {
        int v = Take(dist, odw, n, m, distNodes, countNodes);

        int upper = tab[v][1];
        for (int i = 0; i < upper; i++)
        {
            int u = tab[v][2 + i];
            if (!odw[u - 1])
            {
                countNodes = Relaksacja(dist, v, u - 1, a, b, distNodes, countNodes);
            }
        }
        odw[v] = true;
        count--;
    }
    int result = dist[n * m - 1] - 1;
    delete odw;
    delete dist;
    return result;

}

int main()
{
    string str;
    int n, m, k;

    getline(cin >> ws, str);

    istringstream f(str);
    getline(f, str, ' ');
    n = std::stoi(str);
    getline(f, str, ' ');
    m = std::stoi(str);
    getline(f, str, ' ');
    k = std::stoi(str);

    int count = n * m;

    int** map = (int**)malloc(n * m * sizeof(int*));

    for (int i = 0; i < n * m; i++)
    {
        map[i] = (int*)malloc(6 * sizeof(int));
    }

    for (int i = 0; i < n; i++)
    {
        string row;
        getline(cin >> ws, row);

        for (int j = 0; j < m; j++)
        {
            int v = i * m + j;

            if (row[j] == 'X')
            {
                map[v][0] = 0;
                count--;
            }
            else
            {
                map[v][0] = true;

                int index_left = v - 1;

                if (index_left >= i * m && map[index_left][0] == 1)
                {
                    map[v][2 + map[v][1]] = index_left + 1;
                    map[v][1]++;

                    map[index_left][2 + map[index_left][1]] = v + 1;
                    map[index_left][1]++;
                }

                int index_up = v - m;

                if (index_up >= 0 && map[index_up][0] == 1)
                {
                    map[v][2 + map[v][1]] = index_up + 1;
                    map[v][1]++;

                    map[index_up][2 + map[index_up][1]] = v + 1;
                    map[index_up][1]++;
                }
            }
        }
    }

    int min = -1;
    int counted = 0;
    for (int i = 0; i < k; i++)
    {
        string row;
        int a;
        int b;
        getline(cin >> ws, row);

        istringstream ff(row);
        getline(ff, row, ' ');
        a = std::stoi(row);
        getline(ff, row, ' ');
        b = std::stoi(row);

        int result = 0;
        result = Dijikstra(0, map, a, b, n, m, count);

        if (min < 0 || result < min)
        {
            min = result;
            counted = 1;
        }
        else if (result == min)
        {
            counted++;
        }
    }
    cout << min << " " << counted;
}