#include <stdio.h> #include <iostream> #include <algorithm> #include <utility> #include <queue> using namespace std; const int n_max = 2000; const int k_max = 1000002; auto map = new int [n_max][n_max]; auto cost = new pair<int, int> [n_max][n_max]; int LEFT = 1; int RIGHT = 2; int UP = 3; int DOWN = 4; int n, m, k; bool can_go(int where, int i, int j) { if (where == LEFT) { return j - 1 >= 0; } if (where == RIGHT) { return j + 1 < m; } if (where == DOWN) { return i - 1 >= 0; } if (where == UP) { return i + 1 < n; } return false; } pair<int, int> go(int where, int i, int j) { if (where == LEFT) { return make_pair(i, j - 1); } if (where == RIGHT) { return make_pair(i, j + 1); } if (where == DOWN) { return make_pair(i - 1, j); } if (where == UP) { return make_pair(i + 1, j); } } bool cmp_cost(pair<int, int> first, pair<int, int> second) { // true first > secodn // false first =< second return ((first.first > second.first) && (first.second > second.second)); // return false; } bool visited(int i, int j) { return cost[i][j].first >= 0; } bool should_go(int where, int i, int j) { if (!can_go(where, i, j)) return false; auto new_loc = go(where, i, j); if (map[new_loc.first][new_loc.second] == -1) { return false; } if (!visited(new_loc.first, new_loc.second)) { return true; } return cmp_cost(cost[new_loc.first][new_loc.second], cost[i][j]); } int main() { scanf("%d", &n); scanf("%d", &m); scanf("%d", &k); queue<pair<int, int>> to_calc; long long int * final_result = new long long int[k_max]; int * up = new int[k_max]; int * down = new int[k_max]; char sign; char line[n_max]; for (int i = 0; i < n; i++) { scanf("%s", line); for (int j = 0; j < m; j++) { sign = line[j]; if (sign == '.') { map[i][j] = 0; } else { map[i][j] = -1; } cost[i][j] = make_pair(-1, -1); } } for (int i = 0; i < k; i++) { scanf("%d", &up[i]); scanf("%d", &down[i]); } to_calc.push(make_pair(n - 1, m - 1)); cost[n][m] = make_pair(0, 0); while (!to_calc.empty()) { auto p = to_calc.front(); to_calc.pop(); // cout << "I am here: " << p.first << ", " << p.second << endl; if (should_go(LEFT, p.first, p.second)) { // cout << "Should go LEFT from " << p.first << ", " << p.second << endl; auto p_cost = cost[p.first][p.second]; auto new_cost = make_pair(p_cost.first + 1, p_cost.second); auto new_loc = go(LEFT, p.first, p.second); cost[new_loc.first][new_loc.second] = new_cost; to_calc.push(new_loc); } if (should_go(RIGHT, p.first, p.second)) { // cout << "Should go RIGHT from " << p.first << ", " << p.second << endl; auto p_cost = cost[p.first][p.second]; auto new_cost = make_pair(p_cost.first, p_cost.second + 1); auto new_loc = go(RIGHT, p.first, p.second); cost[new_loc.first][new_loc.second] = new_cost; to_calc.push(new_loc); } if (should_go(UP, p.first, p.second)) { // cout << "Should go UP from " << p.first << ", " << p.second << endl; auto p_cost = cost[p.first][p.second]; auto new_cost = make_pair(p_cost.first, p_cost.second + 1); auto new_loc = go(UP, p.first, p.second); cost[new_loc.first][new_loc.second] = new_cost; to_calc.push(new_loc); } if (should_go(DOWN, p.first, p.second)) { // cout << "Should go DOWN from " << p.first << ", " << p.second << endl; auto p_cost = cost[p.first][p.second]; auto new_cost = make_pair(p_cost.first + 1, p_cost.second); auto new_loc = go(DOWN, p.first, p.second); cost[new_loc.first][new_loc.second] = new_cost; to_calc.push(new_loc); } } long long int going_up = cost[0][0].first + 1; long long int going_down = cost[0][0].second + 1; long long int up_cost, down_cost; up_cost = (long long) up[0]; down_cost = (long long) down[0]; long long int min_res = up_cost * going_up + down_cost * going_down; for (int i = 0; i < k; i++) { up_cost = (long long) up[i]; down_cost = (long long) down[i]; long long int person_cost = up_cost * going_up + down_cost * going_down; min_res = min(min_res, person_cost); final_result[i] = person_cost; } int count_winners = 0; for (int i = 0; i < k; i++) { if (final_result[i] == min_res) { count_winners += 1; } } printf("%lld ", min_res); printf("%d ", count_winners); // cout << cost[0][0].first << " " << cost[0][0].second << 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | #include <stdio.h> #include <iostream> #include <algorithm> #include <utility> #include <queue> using namespace std; const int n_max = 2000; const int k_max = 1000002; auto map = new int [n_max][n_max]; auto cost = new pair<int, int> [n_max][n_max]; int LEFT = 1; int RIGHT = 2; int UP = 3; int DOWN = 4; int n, m, k; bool can_go(int where, int i, int j) { if (where == LEFT) { return j - 1 >= 0; } if (where == RIGHT) { return j + 1 < m; } if (where == DOWN) { return i - 1 >= 0; } if (where == UP) { return i + 1 < n; } return false; } pair<int, int> go(int where, int i, int j) { if (where == LEFT) { return make_pair(i, j - 1); } if (where == RIGHT) { return make_pair(i, j + 1); } if (where == DOWN) { return make_pair(i - 1, j); } if (where == UP) { return make_pair(i + 1, j); } } bool cmp_cost(pair<int, int> first, pair<int, int> second) { // true first > secodn // false first =< second return ((first.first > second.first) && (first.second > second.second)); // return false; } bool visited(int i, int j) { return cost[i][j].first >= 0; } bool should_go(int where, int i, int j) { if (!can_go(where, i, j)) return false; auto new_loc = go(where, i, j); if (map[new_loc.first][new_loc.second] == -1) { return false; } if (!visited(new_loc.first, new_loc.second)) { return true; } return cmp_cost(cost[new_loc.first][new_loc.second], cost[i][j]); } int main() { scanf("%d", &n); scanf("%d", &m); scanf("%d", &k); queue<pair<int, int>> to_calc; long long int * final_result = new long long int[k_max]; int * up = new int[k_max]; int * down = new int[k_max]; char sign; char line[n_max]; for (int i = 0; i < n; i++) { scanf("%s", line); for (int j = 0; j < m; j++) { sign = line[j]; if (sign == '.') { map[i][j] = 0; } else { map[i][j] = -1; } cost[i][j] = make_pair(-1, -1); } } for (int i = 0; i < k; i++) { scanf("%d", &up[i]); scanf("%d", &down[i]); } to_calc.push(make_pair(n - 1, m - 1)); cost[n][m] = make_pair(0, 0); while (!to_calc.empty()) { auto p = to_calc.front(); to_calc.pop(); // cout << "I am here: " << p.first << ", " << p.second << endl; if (should_go(LEFT, p.first, p.second)) { // cout << "Should go LEFT from " << p.first << ", " << p.second << endl; auto p_cost = cost[p.first][p.second]; auto new_cost = make_pair(p_cost.first + 1, p_cost.second); auto new_loc = go(LEFT, p.first, p.second); cost[new_loc.first][new_loc.second] = new_cost; to_calc.push(new_loc); } if (should_go(RIGHT, p.first, p.second)) { // cout << "Should go RIGHT from " << p.first << ", " << p.second << endl; auto p_cost = cost[p.first][p.second]; auto new_cost = make_pair(p_cost.first, p_cost.second + 1); auto new_loc = go(RIGHT, p.first, p.second); cost[new_loc.first][new_loc.second] = new_cost; to_calc.push(new_loc); } if (should_go(UP, p.first, p.second)) { // cout << "Should go UP from " << p.first << ", " << p.second << endl; auto p_cost = cost[p.first][p.second]; auto new_cost = make_pair(p_cost.first, p_cost.second + 1); auto new_loc = go(UP, p.first, p.second); cost[new_loc.first][new_loc.second] = new_cost; to_calc.push(new_loc); } if (should_go(DOWN, p.first, p.second)) { // cout << "Should go DOWN from " << p.first << ", " << p.second << endl; auto p_cost = cost[p.first][p.second]; auto new_cost = make_pair(p_cost.first + 1, p_cost.second); auto new_loc = go(DOWN, p.first, p.second); cost[new_loc.first][new_loc.second] = new_cost; to_calc.push(new_loc); } } long long int going_up = cost[0][0].first + 1; long long int going_down = cost[0][0].second + 1; long long int up_cost, down_cost; up_cost = (long long) up[0]; down_cost = (long long) down[0]; long long int min_res = up_cost * going_up + down_cost * going_down; for (int i = 0; i < k; i++) { up_cost = (long long) up[i]; down_cost = (long long) down[i]; long long int person_cost = up_cost * going_up + down_cost * going_down; min_res = min(min_res, person_cost); final_result[i] = person_cost; } int count_winners = 0; for (int i = 0; i < k; i++) { if (final_result[i] == min_res) { count_winners += 1; } } printf("%lld ", min_res); printf("%d ", count_winners); // cout << cost[0][0].first << " " << cost[0][0].second << endl; return 0; } |