#include <stdio.h> char map[2000][2001]; // 4 MB int bfs[2000][2000]; // 16 MB int qX[4000000]; // 16 MB int qY[4000000]; // 16 MB long long solutions[1000000]; // 8 MB int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) scanf("%s", map[i]); // BFS int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; int head = 1; int tail = 0; while (head > tail) { int x = qX[tail]; int y = qY[tail]; tail++; for (int j = 0; j < 4; j++) { int nx = x + dx[j]; int ny = y + dy[j]; if (0 <= nx && nx < n && 0 <= ny && ny < m && map[nx][ny] == '.' && bfs[nx][ny] == 0) { bfs[nx][ny] = bfs[x][y] + 1; qX[head] = nx; qY[head] = ny; head++; } } } // ups and downs int quickest_way = bfs[n-1][m-1]; int optimal = n - 1 + m - 1; int ups = optimal + (quickest_way - optimal) / 2; int downs = (quickest_way - optimal) / 2; for (int i = 0; i < k; i++) { int a, b; scanf("%d%d", &a, &b); solutions[i] = (long long)a * ups + (long long)b * downs; } long long minimum = solutions[0]; for (int i = 1; i < k; i++) if (solutions[i] < minimum) minimum = solutions[i]; int count = 0; for (int i = 0; i < k; i++) if (solutions[i] == minimum) count++; printf("%lld %d\n", minimum, count); 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include <stdio.h> char map[2000][2001]; // 4 MB int bfs[2000][2000]; // 16 MB int qX[4000000]; // 16 MB int qY[4000000]; // 16 MB long long solutions[1000000]; // 8 MB int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) scanf("%s", map[i]); // BFS int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; int head = 1; int tail = 0; while (head > tail) { int x = qX[tail]; int y = qY[tail]; tail++; for (int j = 0; j < 4; j++) { int nx = x + dx[j]; int ny = y + dy[j]; if (0 <= nx && nx < n && 0 <= ny && ny < m && map[nx][ny] == '.' && bfs[nx][ny] == 0) { bfs[nx][ny] = bfs[x][y] + 1; qX[head] = nx; qY[head] = ny; head++; } } } // ups and downs int quickest_way = bfs[n-1][m-1]; int optimal = n - 1 + m - 1; int ups = optimal + (quickest_way - optimal) / 2; int downs = (quickest_way - optimal) / 2; for (int i = 0; i < k; i++) { int a, b; scanf("%d%d", &a, &b); solutions[i] = (long long)a * ups + (long long)b * downs; } long long minimum = solutions[0]; for (int i = 1; i < k; i++) if (solutions[i] < minimum) minimum = solutions[i]; int count = 0; for (int i = 0; i < k; i++) if (solutions[i] == minimum) count++; printf("%lld %d\n", minimum, count); return 0; } |