#include<iostream> #include<queue> using namespace std; const int MAX_N = 2007; int t[MAX_N][MAX_N]; int main() { // ios::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); int n, m, k; long long a, b, dist; string s; cin >> n >> m >> k; for(int i = 0; i < n; i++) { cin >> s; for(int j = 0; j < m; j++) { t[i][j] = ((s[j] == '.') ? 1e9 : -1e9); } } queue<pair<int, int>> q[2]; q[0].push(make_pair(0, 0)); t[0][0] = 0; int curr = 0, next = 1; while(true) { while(q[curr].size()) { pair<int, int> v = q[curr].front(); q[curr].pop(); if(v.second < m-1 && t[v.first][v.second+1] > t[v.first][v.second]) { q[curr].push(make_pair(v.first, v.second+1)); t[v.first][v.second+1] = t[v.first][v.second]; } if(v.first < n-1 && t[v.first+1][v.second] > t[v.first][v.second]) { q[curr].push(make_pair(v.first+1, v.second)); t[v.first+1][v.second] = t[v.first][v.second]; } if(v.second > 0 && t[v.first][v.second-1] > t[v.first][v.second]+1) { q[next].push(make_pair(v.first, v.second-1)); t[v.first][v.second-1] = t[v.first][v.second]+1; } if(v.first > 0 && t[v.first-1][v.second] > t[v.first][v.second]+1) { q[next].push(make_pair(v.first-1, v.second)); t[v.first-1][v.second] = t[v.first][v.second]+1; } } if(q[next].empty()) break; curr = !curr; next = !next; } dist = t[n-1][m-1]; long long bestScore = 1e18; int bestAmount = 0; for(int i = 0; i < k; i++) { cin >> a >> b; long long x = a*(n+m-2)+(a+b)*dist; if(x < bestScore) { bestScore = x; bestAmount = 1; } else if(x == bestScore) bestAmount++; } cout << bestScore << " " << bestAmount << "\n"; 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 | #include<iostream> #include<queue> using namespace std; const int MAX_N = 2007; int t[MAX_N][MAX_N]; int main() { // ios::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); int n, m, k; long long a, b, dist; string s; cin >> n >> m >> k; for(int i = 0; i < n; i++) { cin >> s; for(int j = 0; j < m; j++) { t[i][j] = ((s[j] == '.') ? 1e9 : -1e9); } } queue<pair<int, int>> q[2]; q[0].push(make_pair(0, 0)); t[0][0] = 0; int curr = 0, next = 1; while(true) { while(q[curr].size()) { pair<int, int> v = q[curr].front(); q[curr].pop(); if(v.second < m-1 && t[v.first][v.second+1] > t[v.first][v.second]) { q[curr].push(make_pair(v.first, v.second+1)); t[v.first][v.second+1] = t[v.first][v.second]; } if(v.first < n-1 && t[v.first+1][v.second] > t[v.first][v.second]) { q[curr].push(make_pair(v.first+1, v.second)); t[v.first+1][v.second] = t[v.first][v.second]; } if(v.second > 0 && t[v.first][v.second-1] > t[v.first][v.second]+1) { q[next].push(make_pair(v.first, v.second-1)); t[v.first][v.second-1] = t[v.first][v.second]+1; } if(v.first > 0 && t[v.first-1][v.second] > t[v.first][v.second]+1) { q[next].push(make_pair(v.first-1, v.second)); t[v.first-1][v.second] = t[v.first][v.second]+1; } } if(q[next].empty()) break; curr = !curr; next = !next; } dist = t[n-1][m-1]; long long bestScore = 1e18; int bestAmount = 0; for(int i = 0; i < k; i++) { cin >> a >> b; long long x = a*(n+m-2)+(a+b)*dist; if(x < bestScore) { bestScore = x; bestAmount = 1; } else if(x == bestScore) bestAmount++; } cout << bestScore << " " << bestAmount << "\n"; return 0; } |