#include <iostream> #include <queue> int main() { long long int n, m, k; std::cin >> n >> m >> k; std::queue<std::pair<int, int> > q; char map[n][m]; long long int dist[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { std::cin >> map[i][j]; dist[i][j] = -1; } } dist[0][0] = 0LL; q.push(std::make_pair(0, 0)); while (!q.empty()) { if (q.front().first < n - 1 && map[q.front().first + 1][q.front().second] != 'X' && dist[q.front().first + 1][q.front().second] == -1) { dist[q.front().first + 1][q.front().second] = dist[q.front().first][q.front().second] + 1LL; q.push(std::make_pair(q.front().first + 1, q.front().second)); } if (q.front().first > 0 && map[q.front().first - 1][q.front().second] != 'X' && dist[q.front().first - 1][q.front().second] == -1) { dist[q.front().first - 1][q.front().second] = dist[q.front().first][q.front().second] + 1LL; q.push(std::make_pair(q.front().first - 1, q.front().second)); } if (q.front().second < m - 1 && map[q.front().first][q.front().second + 1] != 'X' && dist[q.front().first][q.front().second + 1] == -1) { dist[q.front().first][q.front().second + 1] = dist[q.front().first][q.front().second] + 1LL; q.push(std::make_pair(q.front().first, q.front().second + 1)); } if (q.front().second > 0 && map[q.front().first][q.front().second - 1] != 'X' && dist[q.front().first][q.front().second - 1] == -1) { dist[q.front().first][q.front().second - 1] = dist[q.front().first][q.front().second] + 1LL; q.push(std::make_pair(q.front().first, q.front().second - 1)); } q.pop(); } long long int min, num; min = 100000000000000000LL; num = 0; for (int i = 0; i < k; i++) { long long int a, b; std::cin >> a >> b; if (a * (n + m - 2LL + (dist[n - 1][m - 1] - n - m + 2LL) / 2LL) + b * ((dist[n - 1][m - 1] - n - m + 2LL) / 2LL) == min) { num++; } else if (a * (n + m - 2LL + (dist[n - 1][m - 1] - n - m + 2LL) / 2LL) + b * ((dist[n - 1][m - 1] - n - m + 2LL) / 2LL) < min) { min = a * (n + m - 2LL + (dist[n - 1][m - 1] - n - m + 2LL) / 2LL) + b * ((dist[n - 1][m - 1] - n - m + 2LL) / 2LL); num = 1; } } std::cout << min << " " << num; return 0; }
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 | #include <iostream> #include <queue> int main() { long long int n, m, k; std::cin >> n >> m >> k; std::queue<std::pair<int, int> > q; char map[n][m]; long long int dist[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { std::cin >> map[i][j]; dist[i][j] = -1; } } dist[0][0] = 0LL; q.push(std::make_pair(0, 0)); while (!q.empty()) { if (q.front().first < n - 1 && map[q.front().first + 1][q.front().second] != 'X' && dist[q.front().first + 1][q.front().second] == -1) { dist[q.front().first + 1][q.front().second] = dist[q.front().first][q.front().second] + 1LL; q.push(std::make_pair(q.front().first + 1, q.front().second)); } if (q.front().first > 0 && map[q.front().first - 1][q.front().second] != 'X' && dist[q.front().first - 1][q.front().second] == -1) { dist[q.front().first - 1][q.front().second] = dist[q.front().first][q.front().second] + 1LL; q.push(std::make_pair(q.front().first - 1, q.front().second)); } if (q.front().second < m - 1 && map[q.front().first][q.front().second + 1] != 'X' && dist[q.front().first][q.front().second + 1] == -1) { dist[q.front().first][q.front().second + 1] = dist[q.front().first][q.front().second] + 1LL; q.push(std::make_pair(q.front().first, q.front().second + 1)); } if (q.front().second > 0 && map[q.front().first][q.front().second - 1] != 'X' && dist[q.front().first][q.front().second - 1] == -1) { dist[q.front().first][q.front().second - 1] = dist[q.front().first][q.front().second] + 1LL; q.push(std::make_pair(q.front().first, q.front().second - 1)); } q.pop(); } long long int min, num; min = 100000000000000000LL; num = 0; for (int i = 0; i < k; i++) { long long int a, b; std::cin >> a >> b; if (a * (n + m - 2LL + (dist[n - 1][m - 1] - n - m + 2LL) / 2LL) + b * ((dist[n - 1][m - 1] - n - m + 2LL) / 2LL) == min) { num++; } else if (a * (n + m - 2LL + (dist[n - 1][m - 1] - n - m + 2LL) / 2LL) + b * ((dist[n - 1][m - 1] - n - m + 2LL) / 2LL) < min) { min = a * (n + m - 2LL + (dist[n - 1][m - 1] - n - m + 2LL) / 2LL) + b * ((dist[n - 1][m - 1] - n - m + 2LL) / 2LL); num = 1; } } std::cout << min << " " << num; return 0; } |