#include<iostream> #include<string> #include<utility> #include<algorithm> #include<queue> using namespace std; int n, m, k; pair<long long, long long> speeds[1000005]; string mountain[2005]; int dist[2005][2005]; queue<pair<int, int> > bfs; void BFS(pair<int, int> point){ bfs.pop(); if(point.first != 0 and mountain[point.first - 1][point.second] != 'X' and dist[point.first - 1][point.second] == 0){ dist[point.first - 1][point.second] = dist[point.first][point.second] + 1; bfs.push(make_pair(point.first - 1, point.second)); } if(point.first != n - 1 and mountain[point.first + 1][point.second] != 'X' and dist[point.first + 1][point.second] == 0){ dist[point.first + 1][point.second] = dist[point.first][point.second] + 1; bfs.push(make_pair(point.first + 1, point.second)); } if(point.second != 0 and mountain[point.first][point.second - 1] != 'X' and dist[point.first][point.second - 1] == 0){ dist[point.first][point.second - 1] = dist[point.first][point.second] + 1; bfs.push(make_pair(point.first, point.second - 1)); } if(point.second != m - 1 and mountain[point.first][point.second + 1] != 'X' and dist[point.first][point.second + 1] == 0){ dist[point.first][point.second + 1] = dist[point.first][point.second] + 1; bfs.push(make_pair(point.first, point.second + 1)); } } int main(){ cin >> n >> m >> k; for(int i = 0; i < n; i++){ cin >> mountain[i]; } for(int i = 1; i <= k; i++){ cin >> speeds[i].first >> speeds[i].second; } bfs.push(make_pair(0, 0)); dist[0][0] = 1; while(!bfs.empty()) BFS(bfs.front()); int downsteps = (dist[n-1][m-1] - n - m + 2)/2; long long minimal = 1000000000000000001; int numbers = 0; for(int i = 1; i <= k; i++){ long long score = speeds[i].first * (long long)(n + m - 2 + downsteps) + speeds[i].second * (long long)downsteps; if(score < minimal){ minimal = score; numbers = 1; } else if(score == minimal) numbers++; } cout << minimal << " " << numbers; 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 | #include<iostream> #include<string> #include<utility> #include<algorithm> #include<queue> using namespace std; int n, m, k; pair<long long, long long> speeds[1000005]; string mountain[2005]; int dist[2005][2005]; queue<pair<int, int> > bfs; void BFS(pair<int, int> point){ bfs.pop(); if(point.first != 0 and mountain[point.first - 1][point.second] != 'X' and dist[point.first - 1][point.second] == 0){ dist[point.first - 1][point.second] = dist[point.first][point.second] + 1; bfs.push(make_pair(point.first - 1, point.second)); } if(point.first != n - 1 and mountain[point.first + 1][point.second] != 'X' and dist[point.first + 1][point.second] == 0){ dist[point.first + 1][point.second] = dist[point.first][point.second] + 1; bfs.push(make_pair(point.first + 1, point.second)); } if(point.second != 0 and mountain[point.first][point.second - 1] != 'X' and dist[point.first][point.second - 1] == 0){ dist[point.first][point.second - 1] = dist[point.first][point.second] + 1; bfs.push(make_pair(point.first, point.second - 1)); } if(point.second != m - 1 and mountain[point.first][point.second + 1] != 'X' and dist[point.first][point.second + 1] == 0){ dist[point.first][point.second + 1] = dist[point.first][point.second] + 1; bfs.push(make_pair(point.first, point.second + 1)); } } int main(){ cin >> n >> m >> k; for(int i = 0; i < n; i++){ cin >> mountain[i]; } for(int i = 1; i <= k; i++){ cin >> speeds[i].first >> speeds[i].second; } bfs.push(make_pair(0, 0)); dist[0][0] = 1; while(!bfs.empty()) BFS(bfs.front()); int downsteps = (dist[n-1][m-1] - n - m + 2)/2; long long minimal = 1000000000000000001; int numbers = 0; for(int i = 1; i <= k; i++){ long long score = speeds[i].first * (long long)(n + m - 2 + downsteps) + speeds[i].second * (long long)downsteps; if(score < minimal){ minimal = score; numbers = 1; } else if(score == minimal) numbers++; } cout << minimal << " " << numbers; return 0; } |