#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef long long LL; #define FOR(x, b, e) for(int x = b; x<= (e); ++x) #define FORD(x, b, e) for(int x = b; x>= (e); --x) #define REP(x, n) for(int x = 0; x<(n); ++x) #define VAR(v, n)typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB pushback #define ST first #define ND second int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; char x[n][m]; FOR(i, 0, n - 1) { FOR(j, 0, m - 1) { cin >> x[i][j]; } } vector<pair<int, int>> graph[n][m]; pair<int, int> y; FOR(i, 0, n - 1) { FOR(j, 0, m - 1) { if (x[i][j] == '.') { if (i > 0 && x[i - 1][j] == '.') { y.ST = i - 1; y.ND = j; graph[i][j].push_back(y); y.ST = i; graph[i - 1][j].push_back(y); } if (j < m - 1 && x[i][j + 1] == '.') { y.ST = i; y.ND = j + 1; graph[i][j].push_back(y); y.ND = j; graph[i][j + 1].push_back(y); } if (i < n - 1 && x[i + 1][j] == '.') { y.ST = i + 1; y.ND = j; graph[i][j].push_back(y); y.ST = i; graph[i + 1][j].push_back(y); } if (j < 0 && x[i][j - 1] == '.') { y.ST = i; y.ND = j - 1; graph[i][j].push_back(y); y.ND = j; graph[i][j - 1].push_back(y); } } } } queue<pair<int, int>> Q; y.ST = 0; y.ND = 0; Q.push(y); int dist[n][m]; FOR(i, 0, n - 1) { FOR(j, 0, m - 1) { dist[i][j] = m*n; } } dist[0][0] = 0; while (!Q.empty()) { y = Q.front(); Q.pop(); if (graph[y.ST][y.ND].size() > 0) { FOR(i, 0, graph[y.ST][y.ND].size() - 1) { if (dist[graph[y.ST][y.ND][i].ST][graph[y.ST][y.ND][i].ND] == m*n) { Q.push(graph[y.ST][y.ND][i]); } if (dist[graph[y.ST][y.ND][i].ST][graph[y.ST][y.ND][i].ND] > dist[y.ST][y.ND] + 1) { dist[graph[y.ST][y.ND][i].ST][graph[y.ST][y.ND][i].ND] = dist[y.ST][y.ND] + 1; } } } } int d = dist[n - 1][m - 1]; LL min = 100000000000000000; int numofmin = 0; LL a, b; FOR(i, 0, k - 1) { cin >> a >> b; if (min > a * (n + m - 2 + d) / 2 + b * (d - n - m + 2) / 2) { numofmin = 1; min = a * (n + m - 2 + d) / 2 + b * (d - n - m + 2) / 2; } else if (min == a * (n + m - 2 + d) / 2 + b * (d - n - m + 2) / 2) { numofmin++; } } cout << min << " " << numofmin << "\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 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 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef long long LL; #define FOR(x, b, e) for(int x = b; x<= (e); ++x) #define FORD(x, b, e) for(int x = b; x>= (e); --x) #define REP(x, n) for(int x = 0; x<(n); ++x) #define VAR(v, n)typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB pushback #define ST first #define ND second int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; char x[n][m]; FOR(i, 0, n - 1) { FOR(j, 0, m - 1) { cin >> x[i][j]; } } vector<pair<int, int>> graph[n][m]; pair<int, int> y; FOR(i, 0, n - 1) { FOR(j, 0, m - 1) { if (x[i][j] == '.') { if (i > 0 && x[i - 1][j] == '.') { y.ST = i - 1; y.ND = j; graph[i][j].push_back(y); y.ST = i; graph[i - 1][j].push_back(y); } if (j < m - 1 && x[i][j + 1] == '.') { y.ST = i; y.ND = j + 1; graph[i][j].push_back(y); y.ND = j; graph[i][j + 1].push_back(y); } if (i < n - 1 && x[i + 1][j] == '.') { y.ST = i + 1; y.ND = j; graph[i][j].push_back(y); y.ST = i; graph[i + 1][j].push_back(y); } if (j < 0 && x[i][j - 1] == '.') { y.ST = i; y.ND = j - 1; graph[i][j].push_back(y); y.ND = j; graph[i][j - 1].push_back(y); } } } } queue<pair<int, int>> Q; y.ST = 0; y.ND = 0; Q.push(y); int dist[n][m]; FOR(i, 0, n - 1) { FOR(j, 0, m - 1) { dist[i][j] = m*n; } } dist[0][0] = 0; while (!Q.empty()) { y = Q.front(); Q.pop(); if (graph[y.ST][y.ND].size() > 0) { FOR(i, 0, graph[y.ST][y.ND].size() - 1) { if (dist[graph[y.ST][y.ND][i].ST][graph[y.ST][y.ND][i].ND] == m*n) { Q.push(graph[y.ST][y.ND][i]); } if (dist[graph[y.ST][y.ND][i].ST][graph[y.ST][y.ND][i].ND] > dist[y.ST][y.ND] + 1) { dist[graph[y.ST][y.ND][i].ST][graph[y.ST][y.ND][i].ND] = dist[y.ST][y.ND] + 1; } } } } int d = dist[n - 1][m - 1]; LL min = 100000000000000000; int numofmin = 0; LL a, b; FOR(i, 0, k - 1) { cin >> a >> b; if (min > a * (n + m - 2 + d) / 2 + b * (d - n - m + 2) / 2) { numofmin = 1; min = a * (n + m - 2 + d) / 2 + b * (d - n - m + 2) / 2; } else if (min == a * (n + m - 2 + d) / 2 + b * (d - n - m + 2) / 2) { numofmin++; } } cout << min << " " << numofmin << "\n"; return 0; } |