#include <iostream> #include <string> #include <queue> #include <map> #include <limits> using namespace std; const long long INFINITY = numeric_limits<long long>::max(); const long long PRIME = 1000000007LL; int field_id(int row, int col, int row_size) { return row * row_size + col; } long long player_id(long long costA, long long costB) { return costA * PRIME + costB; } int mapGoLeft(int fieldId, int columns) { //return field id for going left on map int result = fieldId - 1; if (result % columns == columns - 1) { //illegal return -1; } return result; } int mapGoRight(int fieldId, int columns) { //return field id for going right on map int result = fieldId + 1; if (result % columns == 0) { //illegal return -1; } return result; } int mapGoUp(int fieldId, int columns) { //return field id for going up on map int result = fieldId - columns; if (result < 0) { //illegal return -1; } return result; } int mapGoDown(int fieldId, int rows, int columns) { //return field id for going down on map int result = fieldId + columns; if (result >= rows * columns) { //illegal return -1; } return result; } long long dijkstra(int start_field, int end_field, bool* fields, int rows, int columns, long long costIncH, long long costDecH) { int size = rows * columns; long long* dist = new long long[size]; // int* prev = new int[size]; // bool* visited = new bool[size]; priority_queue<pair<long long, int> > Q; for (int v = 0; v < size; ++v) { dist[v] = INFINITY; // prev[v] = -1; // visited[v] = false; } dist[start_field] = 0; for (int v = 0; v < size; ++v) { Q.push(make_pair(-dist[v], v)); } int current = -1; int neighbour = -1; long long newDistance = -1; bool stop = false; while (!stop && !Q.empty()) { current = Q.top().second; Q.pop(); // if (visited[current]) { // // cleaning Q from outdated vertex priorities // continue; // } // visited[current] = true; if (current == end_field) { // at target stop = true; continue; } //to up neighbour = mapGoUp(current, columns); if (neighbour >= 0 && fields[neighbour]) { newDistance = dist[current] + costDecH; if (newDistance < dist[neighbour]) { dist[neighbour] = newDistance; // prev[neighbour] = current; Q.push(make_pair(-dist[neighbour], neighbour)); } } //to right neighbour = mapGoRight(current, columns); if (neighbour >= 0 && fields[neighbour]) { newDistance = dist[current] + costIncH; // cout << "DEBUG: neigh: " << neighbour << "\n" // << " dist[curr]: " << dist[current] << "\n" // << " cost: " << costIncH[player] << "\n" // << " newDist: " << newDistance << "\n"; if (newDistance < dist[neighbour]) { dist[neighbour] = newDistance; // prev[neighbour] = current; Q.push(make_pair(-dist[neighbour], neighbour)); } } //to down neighbour = mapGoDown(current, rows, columns); if (neighbour >= 0 && fields[neighbour]) { newDistance = dist[current] + costIncH; if (newDistance < dist[neighbour]) { dist[neighbour] = newDistance; // prev[neighbour] = current; Q.push(make_pair(-dist[neighbour], neighbour)); } } //to left neighbour = mapGoLeft(current, columns); if (neighbour >= 0 && fields[neighbour]) { newDistance = dist[current] + costDecH; if (newDistance < dist[neighbour]) { dist[neighbour] = newDistance; // prev[neighbour] = current; Q.push(make_pair(-dist[neighbour], neighbour)); } } } // for (int i = 0; i < size; ++i) { // cout << "DEBUG: dist[" << i << "]=" << dist[i] << "\n"; // } // cout << "DEBUG: FINAL current=" << current << "\n"; // delete[] dist; // delete[] visited; return dist[end_field]; } int main() { ios::sync_with_stdio(false); // cout << INFINITY; // cout << player_id(1000000000, 1000000000, 1000000); int n, m; long long k; string s; cin >> n >> m >> k; bool *fields = new bool[n * m]; long long *costIncH = new long long[k]; //a long long *costDecH = new long long[k]; //b long long *times = new long long[k]; //destination time map<long long, long long> cache; long long min_time = INFINITY; int count = 0; int fieldId; for (int row = 0; row < n; ++row) { cin >> s; for (int col = 0; col < m; ++col) { fieldId = field_id(row, col, m); if (s[col] == '.') { fields[fieldId] = true; } else { // X fields[fieldId] = false; } } } for (long long i = 0; i < k; ++i) { cin >> costIncH[i] >> costDecH[i]; } map<long long, long long>::iterator it; long long playerId; long long bestCostIncH = INFINITY; long long bestCostDecH = INFINITY; for (long long i = 0; i < k; ++i) { playerId = player_id(costIncH[i], costDecH[i]); // cout << "player"<<costIncH[i] << " " << costDecH[i] <<"\n"; // cout << "playerid" << playerId << "\n"; if (costIncH[i] > bestCostIncH && costDecH[i] > bestCostDecH) { // cout << "skipped" << playerId << "\n"; continue; } it = cache.find(playerId); if (it != cache.end()) { times[i] = it->second; // cout << "cache"<<it->second << "\n"; } else { // new computation times[i] = dijkstra(0, n * m - 1, fields, n, m, costIncH[i], costDecH[i]); // cout << "new comp"<< times[i] << "\n"; cache[playerId] = times[i]; if (times[i] < min_time) { // cout << "updated"<< min_time << " with " << times[i] << "\n"; min_time = times[i]; bestCostIncH = costIncH[i]; bestCostDecH = costDecH[i]; } } } for (long long i = 0; i < k; ++i) { if (min_time == times[i]) { ++count; } } //answer cout << min_time << " " << count; 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | #include <iostream> #include <string> #include <queue> #include <map> #include <limits> using namespace std; const long long INFINITY = numeric_limits<long long>::max(); const long long PRIME = 1000000007LL; int field_id(int row, int col, int row_size) { return row * row_size + col; } long long player_id(long long costA, long long costB) { return costA * PRIME + costB; } int mapGoLeft(int fieldId, int columns) { //return field id for going left on map int result = fieldId - 1; if (result % columns == columns - 1) { //illegal return -1; } return result; } int mapGoRight(int fieldId, int columns) { //return field id for going right on map int result = fieldId + 1; if (result % columns == 0) { //illegal return -1; } return result; } int mapGoUp(int fieldId, int columns) { //return field id for going up on map int result = fieldId - columns; if (result < 0) { //illegal return -1; } return result; } int mapGoDown(int fieldId, int rows, int columns) { //return field id for going down on map int result = fieldId + columns; if (result >= rows * columns) { //illegal return -1; } return result; } long long dijkstra(int start_field, int end_field, bool* fields, int rows, int columns, long long costIncH, long long costDecH) { int size = rows * columns; long long* dist = new long long[size]; // int* prev = new int[size]; // bool* visited = new bool[size]; priority_queue<pair<long long, int> > Q; for (int v = 0; v < size; ++v) { dist[v] = INFINITY; // prev[v] = -1; // visited[v] = false; } dist[start_field] = 0; for (int v = 0; v < size; ++v) { Q.push(make_pair(-dist[v], v)); } int current = -1; int neighbour = -1; long long newDistance = -1; bool stop = false; while (!stop && !Q.empty()) { current = Q.top().second; Q.pop(); // if (visited[current]) { // // cleaning Q from outdated vertex priorities // continue; // } // visited[current] = true; if (current == end_field) { // at target stop = true; continue; } //to up neighbour = mapGoUp(current, columns); if (neighbour >= 0 && fields[neighbour]) { newDistance = dist[current] + costDecH; if (newDistance < dist[neighbour]) { dist[neighbour] = newDistance; // prev[neighbour] = current; Q.push(make_pair(-dist[neighbour], neighbour)); } } //to right neighbour = mapGoRight(current, columns); if (neighbour >= 0 && fields[neighbour]) { newDistance = dist[current] + costIncH; // cout << "DEBUG: neigh: " << neighbour << "\n" // << " dist[curr]: " << dist[current] << "\n" // << " cost: " << costIncH[player] << "\n" // << " newDist: " << newDistance << "\n"; if (newDistance < dist[neighbour]) { dist[neighbour] = newDistance; // prev[neighbour] = current; Q.push(make_pair(-dist[neighbour], neighbour)); } } //to down neighbour = mapGoDown(current, rows, columns); if (neighbour >= 0 && fields[neighbour]) { newDistance = dist[current] + costIncH; if (newDistance < dist[neighbour]) { dist[neighbour] = newDistance; // prev[neighbour] = current; Q.push(make_pair(-dist[neighbour], neighbour)); } } //to left neighbour = mapGoLeft(current, columns); if (neighbour >= 0 && fields[neighbour]) { newDistance = dist[current] + costDecH; if (newDistance < dist[neighbour]) { dist[neighbour] = newDistance; // prev[neighbour] = current; Q.push(make_pair(-dist[neighbour], neighbour)); } } } // for (int i = 0; i < size; ++i) { // cout << "DEBUG: dist[" << i << "]=" << dist[i] << "\n"; // } // cout << "DEBUG: FINAL current=" << current << "\n"; // delete[] dist; // delete[] visited; return dist[end_field]; } int main() { ios::sync_with_stdio(false); // cout << INFINITY; // cout << player_id(1000000000, 1000000000, 1000000); int n, m; long long k; string s; cin >> n >> m >> k; bool *fields = new bool[n * m]; long long *costIncH = new long long[k]; //a long long *costDecH = new long long[k]; //b long long *times = new long long[k]; //destination time map<long long, long long> cache; long long min_time = INFINITY; int count = 0; int fieldId; for (int row = 0; row < n; ++row) { cin >> s; for (int col = 0; col < m; ++col) { fieldId = field_id(row, col, m); if (s[col] == '.') { fields[fieldId] = true; } else { // X fields[fieldId] = false; } } } for (long long i = 0; i < k; ++i) { cin >> costIncH[i] >> costDecH[i]; } map<long long, long long>::iterator it; long long playerId; long long bestCostIncH = INFINITY; long long bestCostDecH = INFINITY; for (long long i = 0; i < k; ++i) { playerId = player_id(costIncH[i], costDecH[i]); // cout << "player"<<costIncH[i] << " " << costDecH[i] <<"\n"; // cout << "playerid" << playerId << "\n"; if (costIncH[i] > bestCostIncH && costDecH[i] > bestCostDecH) { // cout << "skipped" << playerId << "\n"; continue; } it = cache.find(playerId); if (it != cache.end()) { times[i] = it->second; // cout << "cache"<<it->second << "\n"; } else { // new computation times[i] = dijkstra(0, n * m - 1, fields, n, m, costIncH[i], costDecH[i]); // cout << "new comp"<< times[i] << "\n"; cache[playerId] = times[i]; if (times[i] < min_time) { // cout << "updated"<< min_time << " with " << times[i] << "\n"; min_time = times[i]; bestCostIncH = costIncH[i]; bestCostDecH = costDecH[i]; } } } for (long long i = 0; i < k; ++i) { if (min_time == times[i]) { ++count; } } //answer cout << min_time << " " << count; return 0; } |