#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
int main()
{
const int dr[4] = { -1, 1, 0, 0 };
const int dc[4] = { 0, 0, -1, 1 };
int n, m, k;
LL a, b;
cin >> n >> m >> k;
vector<string> map(n);
vector<PLL> friends(k);
for (int i = 0; i < n; ++i)
{
cin >> map[i];
}
for (int i = 0; i < k; ++i)
{
cin >> a >> b;
friends[i] = make_pair(a, b);
}
vector<PII> dist(n * m, make_pair(10000000, 10000000));
vector<pair<PII, int>> Q;
int cur = 0;
dist[0].first = 0;
dist[0].second = 0;
Q.push_back(make_pair(dist[0], 0));
while (cur < Q.size())
{
pair<PII, int> c = Q[cur];
PII d = c.first;
int u = c.second;
int row = u / m;
int col = u % m;
if (dist[u] == d)
{
for (int dd = 0; dd < 4; ++dd)
{
int nrow = row + dr[dd];
int ncol = col + dc[dd];
if (nrow < 0 || nrow >= n || ncol < 0 || ncol >= m || map[nrow][ncol] == 'X')
continue;
int v = nrow * m + ncol;
PII ndist = make_pair(d.first, d.second);
if (dr[dd] > 0 || dc[dd] > 0)
ndist.first++;
else
ndist.second++;
if (ndist < dist[v])
{
dist[v] = ndist;
Q.push_back(make_pair(ndist, v));
}
}
}
++cur;
}
PII end = dist[n * m - 1];
LL res = 10000000000000000;
int cnt = 0;
for (int i = 0; i < k; ++i)
{
LL time = friends[i].first * end.first + friends[i].second * end.second;
if (time < res)
{
res = time;
cnt = 0;
}
if (time == res)
++cnt;
}
cout << res << " " << cnt << 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 | #include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; using LL = long long; using PII = pair<int, int>; using PLL = pair<LL, LL>; int main() { const int dr[4] = { -1, 1, 0, 0 }; const int dc[4] = { 0, 0, -1, 1 }; int n, m, k; LL a, b; cin >> n >> m >> k; vector<string> map(n); vector<PLL> friends(k); for (int i = 0; i < n; ++i) { cin >> map[i]; } for (int i = 0; i < k; ++i) { cin >> a >> b; friends[i] = make_pair(a, b); } vector<PII> dist(n * m, make_pair(10000000, 10000000)); vector<pair<PII, int>> Q; int cur = 0; dist[0].first = 0; dist[0].second = 0; Q.push_back(make_pair(dist[0], 0)); while (cur < Q.size()) { pair<PII, int> c = Q[cur]; PII d = c.first; int u = c.second; int row = u / m; int col = u % m; if (dist[u] == d) { for (int dd = 0; dd < 4; ++dd) { int nrow = row + dr[dd]; int ncol = col + dc[dd]; if (nrow < 0 || nrow >= n || ncol < 0 || ncol >= m || map[nrow][ncol] == 'X') continue; int v = nrow * m + ncol; PII ndist = make_pair(d.first, d.second); if (dr[dd] > 0 || dc[dd] > 0) ndist.first++; else ndist.second++; if (ndist < dist[v]) { dist[v] = ndist; Q.push_back(make_pair(ndist, v)); } } } ++cur; } PII end = dist[n * m - 1]; LL res = 10000000000000000; int cnt = 0; for (int i = 0; i < k; ++i) { LL time = friends[i].first * end.first + friends[i].second * end.second; if (time < res) { res = time; cnt = 0; } if (time == res) ++cnt; } cout << res << " " << cnt << endl; return 0; } |
English