#include <cstdio> #include <limits> #include <queue> using namespace std; const int C = 2005; char t[C][C]; int dist[C][C]; typedef long long LL; typedef pair<int, pair<int, int>> QE; const int MAX = numeric_limits<int>::max(); int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); char* a, b; for (int i=0; i<n; ++i) { scanf("%s ", t[i]); for (int j=0; j<m; ++j) { dist[i][j] = MAX; } } priority_queue<QE, vector<QE>, greater<QE>> q; q.push(make_pair(0, make_pair(0, 0))); while (!q.empty()) { QE top = q.top(); int x = top.second.first; int y = top.second.second; int d = top.first; q.pop(); if (dist[x][y] != MAX) continue; dist[x][y] = d; for (int i=0; i<4; ++i) { int x2 = x + dx[i]; int y2 = y + dy[i]; if (x2 < 0 || x2 >= n) continue; if (y2 < 0 || y2 >= m) continue; if (t[x2][y2] == 'X') continue; q.push(make_pair(d+1, make_pair(x2, y2))); } } int downs = (dist[n-1][m-1] - n - m + 2) / 2; int ups = dist[n-1][m-1] - downs; LL best = MAX; int bestc = 0; for (int i=0; i<k; ++i) { LL a, b; scanf("%lld %lld", &a, &b); LL score = a*ups + b*downs; if (score < best) { best = score; bestc = 0; } if (score == best) ++bestc; } printf("%lld %d\n", best, bestc); }
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 | #include <cstdio> #include <limits> #include <queue> using namespace std; const int C = 2005; char t[C][C]; int dist[C][C]; typedef long long LL; typedef pair<int, pair<int, int>> QE; const int MAX = numeric_limits<int>::max(); int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); char* a, b; for (int i=0; i<n; ++i) { scanf("%s ", t[i]); for (int j=0; j<m; ++j) { dist[i][j] = MAX; } } priority_queue<QE, vector<QE>, greater<QE>> q; q.push(make_pair(0, make_pair(0, 0))); while (!q.empty()) { QE top = q.top(); int x = top.second.first; int y = top.second.second; int d = top.first; q.pop(); if (dist[x][y] != MAX) continue; dist[x][y] = d; for (int i=0; i<4; ++i) { int x2 = x + dx[i]; int y2 = y + dy[i]; if (x2 < 0 || x2 >= n) continue; if (y2 < 0 || y2 >= m) continue; if (t[x2][y2] == 'X') continue; q.push(make_pair(d+1, make_pair(x2, y2))); } } int downs = (dist[n-1][m-1] - n - m + 2) / 2; int ups = dist[n-1][m-1] - downs; LL best = MAX; int bestc = 0; for (int i=0; i<k; ++i) { LL a, b; scanf("%lld %lld", &a, &b); LL score = a*ups + b*downs; if (score < best) { best = score; bestc = 0; } if (score == best) ++bestc; } printf("%lld %d\n", best, bestc); } |