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
#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

int in_x, in_y, in_k;

char row[2010];

bool visited[2010][2010];
int dist[2010][2010];
bool freee[2010][2010];

int main() {
    scanf("%d%d%d", &in_y, &in_x, &in_k);

    for(int y = 0; y <= in_y+1; ++y) {
        for (int x = 0; x <= in_x + 1; ++x) {
            dist[y][x] = std::numeric_limits<int>::max();
            visited[y][x] = false;
            freee[y][x] = false;
        }
    }

    for(int y = 0; y < in_y; ++y) {
        scanf("%s", row);

        for(int x = 0; x < in_x; ++x) {
            if (row[x] == '.') {
                freee[y+1][x+1] = true;
            } else {
                freee[y+1][x+1] = false;
            }
        }
    }

    queue<pair<int, int>> Q;
    Q.push(make_pair(1, 1));
    dist[1][1] = 0;
    visited[1][1] = true;

    while (!Q.empty()) {
        pair<int, int> q = Q.front();
        Q.pop();

        int y = q.first;
        int x = q.second;

//        printf("\n======================\ntop: (%d %d) -> %d\n", y, x, backs[y][x]);

        if (!visited[y][x]) {
            printf("yyyyy\n");
            exit(1);
        }

        if (x == in_x && y == in_y) {
            break;
        }

        for (int dx = -1; dx <= 1; dx++) {
            for (int dy = -1; dy <= 1; dy++) {
//                printf("check (%d %d)", x+dx, y+dy);

                if (freee[y+dy][x+dx] && (dx == 0 || dy == 0)) {
                    dist[y + dy][x + dx] = std::min(dist[y + dy][x + dx], dist[y][x] + 1);

                    if (!visited[y+dy][x+dx]) {
//                        printf(" pushing\n");
                        Q.push(make_pair(y + dy, x + dx));
                        visited[y+dy][x+dx] = true;
                    }
                }
//                printf("\n");
            }
        }
    }

//    for(int y = 1; y <= in_y; ++y) {
//        for (int x = 1; x <= in_x; ++x) {
//            if (freee[y][x]) {
//                printf(".");
//            } else {
//                printf("x");
//            }
//        }
//        printf("\n");
//    }
//    printf("\n");
//    printf("\n");
//    for(int y = 1; y <= in_y; ++y) {
//        for (int x = 1; x <= in_x; ++x) {
//            printf("%d\t", dist[y][x]);
//        }
//        printf("\n");
//    }

    using ull = unsigned long long;
    ull distance = dist[in_y][in_x];
    ull ideal = in_x + in_y - 2;

    ull remaining = distance - ideal;
    if (remaining % 2 == 1) {
        printf("yyyyyyyyyyyyy\n");
        exit(1);
    }

    remaining /= 2;

    ull len_up = ideal + remaining;
    ull len_down = remaining;

    ull best_time = std::numeric_limits<ull>::max();
    ull best_times_counter = 0;

    for (int k = 0; k < in_k; k++) {
        ull up, down;
        cin >> up >> down;

        ull time = len_up * up + len_down * down;
        if (time < best_time) {
            best_time = time;
            best_times_counter = 1;
        } else if (time == best_time) {
            best_times_counter++;
        }
    }

    cout << best_time << " " << best_times_counter << endl;

    return 0;
}