#include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; typedef long long int lli; int bfs(lli n, lli m, vector<vector<bool>>& grid) { vector<vector<bool>> visited(n, vector<bool>(m)); queue<lli> qr; queue<lli> qc; qr.push(0); qc.push(0); visited[0][0] = true; lli currDist = 0; vector<int> rd = {1, -1, 0, 0}; vector<int> cd = {0, 0, -1, 1}; while (!qr.empty()) { int nodeAtCurrDist = qr.size(); for (int i = 0; i < nodeAtCurrDist; i++) { lli r = qr.front(); qr.pop(); lli c = qc.front(); qc.pop(); if (r == n-1 && c == m-1) { return currDist; } for (int j = 0; j < rd.size(); j++) { lli newR = r + rd[j]; lli newC = c + cd[j]; if (newR < 0 || newR >= n || newC < 0 || newC >= m || visited[newR][newC] || !grid[newR][newC]) { continue; } visited[newR][newC] = true; qr.push(newR); qc.push(newC); } } currDist++; } return -1; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); lli n, m, k; cin >> n >> m >> k; vector<vector<bool>> grid(n, vector<bool>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char val; cin >> val; grid[i][j] = val == '.'; } } lli minSteps = bfs(n, m, grid); if (minSteps == -1) { cout << "Failed" << '\n'; return 1; } lli minTheoreticalSteps = (n-1+m-1); lli forw = minTheoreticalSteps + (minSteps - minTheoreticalSteps) / 2; lli back = (minSteps - minTheoreticalSteps) / 2; vector<lli> minPaths(k); lli totalMin = INT64_MAX; vector<lli> totals(k); for (int i = 0; i < k; i++) { lli forwWeight, backWeight; cin >> forwWeight >> backWeight; totals[i] = forw * forwWeight + back * backWeight; totalMin = min(totalMin, totals[i]); } lli count = 0; for (lli total : totals) { if (total == totalMin) { count++; } } cout << totalMin << " " << count << '\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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; typedef long long int lli; int bfs(lli n, lli m, vector<vector<bool>>& grid) { vector<vector<bool>> visited(n, vector<bool>(m)); queue<lli> qr; queue<lli> qc; qr.push(0); qc.push(0); visited[0][0] = true; lli currDist = 0; vector<int> rd = {1, -1, 0, 0}; vector<int> cd = {0, 0, -1, 1}; while (!qr.empty()) { int nodeAtCurrDist = qr.size(); for (int i = 0; i < nodeAtCurrDist; i++) { lli r = qr.front(); qr.pop(); lli c = qc.front(); qc.pop(); if (r == n-1 && c == m-1) { return currDist; } for (int j = 0; j < rd.size(); j++) { lli newR = r + rd[j]; lli newC = c + cd[j]; if (newR < 0 || newR >= n || newC < 0 || newC >= m || visited[newR][newC] || !grid[newR][newC]) { continue; } visited[newR][newC] = true; qr.push(newR); qc.push(newC); } } currDist++; } return -1; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); lli n, m, k; cin >> n >> m >> k; vector<vector<bool>> grid(n, vector<bool>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char val; cin >> val; grid[i][j] = val == '.'; } } lli minSteps = bfs(n, m, grid); if (minSteps == -1) { cout << "Failed" << '\n'; return 1; } lli minTheoreticalSteps = (n-1+m-1); lli forw = minTheoreticalSteps + (minSteps - minTheoreticalSteps) / 2; lli back = (minSteps - minTheoreticalSteps) / 2; vector<lli> minPaths(k); lli totalMin = INT64_MAX; vector<lli> totals(k); for (int i = 0; i < k; i++) { lli forwWeight, backWeight; cin >> forwWeight >> backWeight; totals[i] = forw * forwWeight + back * backWeight; totalMin = min(totalMin, totals[i]); } lli count = 0; for (lli total : totals) { if (total == totalMin) { count++; } } cout << totalMin << " " << count << '\n'; return 0; } |