#include <iostream> #include <queue> #include <map> #define FOR(i, n) for(int i = 1; i <= n; i++) int n, m, k; struct vertex { int x, y; int value = 1000000000; bool has_last = false; vertex* last; vertex(int x, int y) { this->x = x; this->y = y; } }; struct dvalue { int value; bool operator > (const dvalue& b) const { return value < b.value; } bool operator < (const dvalue& b) const { return value > b.value; } dvalue(int value) { this->value = value; } }; std::map<std::pair<int, int>, vertex*> vertex_map; std::map<std::pair<int, int>, std::vector<vertex*>> neighbors; void dijkstra(vertex* ver) { std::priority_queue<std::pair<dvalue, vertex*>> que; vertex* actual = ver; actual->value = 0; que.push(std::make_pair(0, actual)); while (!que.empty()) { dvalue v = que.top().first; vertex* dver = que.top().second; que.pop(); if (v.value != dver->value) continue; if (dver->x == m && dver->y == n) return; if (!neighbors.count(std::make_pair(dver->x, dver->y))) neighbors[std::make_pair(dver->x, dver->y)] = *new std::vector<vertex*>(); for (vertex* neigh : neighbors[std::make_pair(dver->x, dver->y)]) { int d = m + n - (dver->x + dver->y); if (neigh->value > d + dver->value) { neigh->value = d + dver->value; neigh->has_last = true; neigh->last = dver; que.push(std::make_pair(neigh->value, neigh)); } } } return; } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cin >> n >> m >> k; FOR(y, n) { FOR(x, m) { char miejsce; std::cin >> miejsce; if(miejsce == '.'){ vertex* v = new vertex(x, y); vertex_map[std::make_pair(x, y)] = v; if (v->x > 1 && vertex_map.count(std::make_pair(v->x - 1, v->y))) { neighbors[std::make_pair(v->x - 1, v->y)].push_back(v); neighbors[std::make_pair(v->x, v->y)].push_back(vertex_map[std::make_pair(v->x - 1, v->y)]); } if (v->y > 1 && vertex_map.count(std::make_pair(v->x, v->y - 1))) { neighbors[std::make_pair(v->x, v->y - 1)].push_back(v); neighbors[std::make_pair(v->x, v->y)].push_back(vertex_map[std::make_pair(v->x, v->y - 1)]); } } } } dijkstra(vertex_map[std::make_pair(1, 1)]); vertex* ver = vertex_map[std::make_pair(m, n)]; int one = 0; int two = 0; while (ver->has_last) { //ver->x << " " << ver->y if (ver->last->x > ver->x) { two++; } if (ver->last->y > ver->y) { two++; } if (ver->last->x < ver->x) { one++; } if (ver->last->y < ver->y) { one++; } ver = ver->last; } int amount = 0, min = 1000000000; FOR(i, k) { int wchodzi, schodzi; std::cin >> wchodzi >> schodzi; int licz = one * wchodzi + two * schodzi; if (licz == min) amount++; if (licz < min) { amount = 1; min = licz; } } std::cout << min << " " << amount << 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 | #include <iostream> #include <queue> #include <map> #define FOR(i, n) for(int i = 1; i <= n; i++) int n, m, k; struct vertex { int x, y; int value = 1000000000; bool has_last = false; vertex* last; vertex(int x, int y) { this->x = x; this->y = y; } }; struct dvalue { int value; bool operator > (const dvalue& b) const { return value < b.value; } bool operator < (const dvalue& b) const { return value > b.value; } dvalue(int value) { this->value = value; } }; std::map<std::pair<int, int>, vertex*> vertex_map; std::map<std::pair<int, int>, std::vector<vertex*>> neighbors; void dijkstra(vertex* ver) { std::priority_queue<std::pair<dvalue, vertex*>> que; vertex* actual = ver; actual->value = 0; que.push(std::make_pair(0, actual)); while (!que.empty()) { dvalue v = que.top().first; vertex* dver = que.top().second; que.pop(); if (v.value != dver->value) continue; if (dver->x == m && dver->y == n) return; if (!neighbors.count(std::make_pair(dver->x, dver->y))) neighbors[std::make_pair(dver->x, dver->y)] = *new std::vector<vertex*>(); for (vertex* neigh : neighbors[std::make_pair(dver->x, dver->y)]) { int d = m + n - (dver->x + dver->y); if (neigh->value > d + dver->value) { neigh->value = d + dver->value; neigh->has_last = true; neigh->last = dver; que.push(std::make_pair(neigh->value, neigh)); } } } return; } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cin >> n >> m >> k; FOR(y, n) { FOR(x, m) { char miejsce; std::cin >> miejsce; if(miejsce == '.'){ vertex* v = new vertex(x, y); vertex_map[std::make_pair(x, y)] = v; if (v->x > 1 && vertex_map.count(std::make_pair(v->x - 1, v->y))) { neighbors[std::make_pair(v->x - 1, v->y)].push_back(v); neighbors[std::make_pair(v->x, v->y)].push_back(vertex_map[std::make_pair(v->x - 1, v->y)]); } if (v->y > 1 && vertex_map.count(std::make_pair(v->x, v->y - 1))) { neighbors[std::make_pair(v->x, v->y - 1)].push_back(v); neighbors[std::make_pair(v->x, v->y)].push_back(vertex_map[std::make_pair(v->x, v->y - 1)]); } } } } dijkstra(vertex_map[std::make_pair(1, 1)]); vertex* ver = vertex_map[std::make_pair(m, n)]; int one = 0; int two = 0; while (ver->has_last) { //ver->x << " " << ver->y if (ver->last->x > ver->x) { two++; } if (ver->last->y > ver->y) { two++; } if (ver->last->x < ver->x) { one++; } if (ver->last->y < ver->y) { one++; } ver = ver->last; } int amount = 0, min = 1000000000; FOR(i, k) { int wchodzi, schodzi; std::cin >> wchodzi >> schodzi; int licz = one * wchodzi + two * schodzi; if (licz == min) amount++; if (licz < min) { amount = 1; min = licz; } } std::cout << min << " " << amount << std::endl; return 0; } |