#include <iostream> #include <vector> #include <string> #include <set> #include <unordered_set> using LL = long long int; struct Node { int y; int x; int parentX; int parentY; LL gCost; LL hCost; LL fCost; bool operator<(const Node &rhs) const { return fCost < rhs.fCost && (fCost == rhs.fCost && hCost < rhs.hCost); } }; using Grid = std::vector<std::vector<bool>>; using GridNode = std::vector<std::vector<Node>>; int dist(Grid const &grid, int x, int y) { return grid.size() - y + grid.back().size() - x + 2; } bool isValid(int x, int y, int cX, int cY) { return (cX >= 0) && (cX < x) && (cY >= 0) && (cY < y); } std::pair<int, int> aStar(Grid const &grid, int y, int x) { Node start; start.x = 0; start.y = 0; start.parentX = 0; start.parentY = 0; start.fCost = 0; start.gCost = 0; start.hCost = 0; std::set<std::pair<Node, std::pair<int, int>>> openList; Grid closedList(y, std::vector<bool>(x, false)); GridNode allMap(y, std::vector<Node>(x)); for (int j = 0; j < y; ++j) { for (int i = 0; i < x; ++i) { Node &node = allMap[j][i]; node.fCost = -1; node.gCost = -1; node.hCost = -1; node.x = i; node.y = j; node.parentX = -1; node.parentY = -1; } } allMap[0][0] = start; openList.insert({start, {0,0}}); while (!openList.empty()) { Node node = openList.begin()->first; openList.erase(openList.begin()); if (closedList[node.y][node.x]) continue; closedList[node.y][node.x] = true; if (node.y == (y - 1) && node.x == (x - 1)) break; std::vector<std::pair<int, int>> neighs; neighs.push_back({node.y, node.x + 1}); neighs.push_back({node.y, node.x - 1}); neighs.push_back({node.y - 1, node.x}); neighs.push_back({node.y + 1, node.x}); for (auto const& neigh : neighs) { int cY = neigh.first; int cX = neigh.second; if (!isValid(x, y, cX, cY)) continue; if (!grid[cY][cX]) continue; if (closedList[cY][cX]) continue; LL gNew = allMap[node.y][node.x].gCost + 1; LL hNew = dist(grid, cX, cY); LL fNew = gNew + hNew; if (allMap[cY][cX].fCost == -1 || allMap[cY][cX].fCost > fNew) { allMap[cY][cX].fCost = fNew; allMap[cY][cX].hCost = hNew; allMap[cY][cX].gCost = gNew; allMap[cY][cX].parentX = node.x; allMap[cY][cX].parentY = node.y; openList.insert({allMap[cY][cX], {cY, cX}}); } } } Node &dst = allMap[y - 1][x - 1]; int up = 1, down = 0; while(dst.parentX != 0 || dst.parentY != 0) { if (dst.x > dst.parentX || dst.y > dst.parentY) ++up; else ++down; dst = allMap[dst.parentY][dst.parentX]; } return {up, down}; } int main() { int n, m, k; std::cin >> n >> m >> k; Grid map; char sign; for (int y = 0; y < n; ++y) { map.push_back(std::vector<bool>(m, false)); for (int x = 0; x < m; ++x) { std::cin >> sign; if (sign == '.') map.back()[x] = true; } } std::pair<int, int> distance = aStar(map, n, m); int up = distance.first; int down = distance.second; // std::cout << up << " " << down << std::endl; LL a, b; std::cin >> a >> b; LL mini = a * up + b * down; LL count = 1; for (int i = 1; i<k; ++i) { std::cin >> a >> b; LL tmp_mini = a * up + b * down; if (tmp_mini < mini) { mini = tmp_mini; count = 1; } else if (tmp_mini == mini) { count++; } } std::cout << mini << " " << count << std::endl; 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | #include <iostream> #include <vector> #include <string> #include <set> #include <unordered_set> using LL = long long int; struct Node { int y; int x; int parentX; int parentY; LL gCost; LL hCost; LL fCost; bool operator<(const Node &rhs) const { return fCost < rhs.fCost && (fCost == rhs.fCost && hCost < rhs.hCost); } }; using Grid = std::vector<std::vector<bool>>; using GridNode = std::vector<std::vector<Node>>; int dist(Grid const &grid, int x, int y) { return grid.size() - y + grid.back().size() - x + 2; } bool isValid(int x, int y, int cX, int cY) { return (cX >= 0) && (cX < x) && (cY >= 0) && (cY < y); } std::pair<int, int> aStar(Grid const &grid, int y, int x) { Node start; start.x = 0; start.y = 0; start.parentX = 0; start.parentY = 0; start.fCost = 0; start.gCost = 0; start.hCost = 0; std::set<std::pair<Node, std::pair<int, int>>> openList; Grid closedList(y, std::vector<bool>(x, false)); GridNode allMap(y, std::vector<Node>(x)); for (int j = 0; j < y; ++j) { for (int i = 0; i < x; ++i) { Node &node = allMap[j][i]; node.fCost = -1; node.gCost = -1; node.hCost = -1; node.x = i; node.y = j; node.parentX = -1; node.parentY = -1; } } allMap[0][0] = start; openList.insert({start, {0,0}}); while (!openList.empty()) { Node node = openList.begin()->first; openList.erase(openList.begin()); if (closedList[node.y][node.x]) continue; closedList[node.y][node.x] = true; if (node.y == (y - 1) && node.x == (x - 1)) break; std::vector<std::pair<int, int>> neighs; neighs.push_back({node.y, node.x + 1}); neighs.push_back({node.y, node.x - 1}); neighs.push_back({node.y - 1, node.x}); neighs.push_back({node.y + 1, node.x}); for (auto const& neigh : neighs) { int cY = neigh.first; int cX = neigh.second; if (!isValid(x, y, cX, cY)) continue; if (!grid[cY][cX]) continue; if (closedList[cY][cX]) continue; LL gNew = allMap[node.y][node.x].gCost + 1; LL hNew = dist(grid, cX, cY); LL fNew = gNew + hNew; if (allMap[cY][cX].fCost == -1 || allMap[cY][cX].fCost > fNew) { allMap[cY][cX].fCost = fNew; allMap[cY][cX].hCost = hNew; allMap[cY][cX].gCost = gNew; allMap[cY][cX].parentX = node.x; allMap[cY][cX].parentY = node.y; openList.insert({allMap[cY][cX], {cY, cX}}); } } } Node &dst = allMap[y - 1][x - 1]; int up = 1, down = 0; while(dst.parentX != 0 || dst.parentY != 0) { if (dst.x > dst.parentX || dst.y > dst.parentY) ++up; else ++down; dst = allMap[dst.parentY][dst.parentX]; } return {up, down}; } int main() { int n, m, k; std::cin >> n >> m >> k; Grid map; char sign; for (int y = 0; y < n; ++y) { map.push_back(std::vector<bool>(m, false)); for (int x = 0; x < m; ++x) { std::cin >> sign; if (sign == '.') map.back()[x] = true; } } std::pair<int, int> distance = aStar(map, n, m); int up = distance.first; int down = distance.second; // std::cout << up << " " << down << std::endl; LL a, b; std::cin >> a >> b; LL mini = a * up + b * down; LL count = 1; for (int i = 1; i<k; ++i) { std::cin >> a >> b; LL tmp_mini = a * up + b * down; if (tmp_mini < mini) { mini = tmp_mini; count = 1; } else if (tmp_mini == mini) { count++; } } std::cout << mini << " " << count << std::endl; return 0; } |