#include<bits/stdc++.h> using namespace std; using lld = long long; const int MAXN = 2e3; const int INF = 1e9; int dist[MAXN+5][MAXN+5]; int main(void){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,m,k; cin >> n >> m >> k; vector<string> t(n+2); for(int i=1;i<=n;i++) cin >> t[i]; t[0] = t[n+1] = string(m+2,'X'); for(int i=1;i<=n;i++) t[i] = "X"+t[i]+"X"; for(int i=0;i<=n+1;i++) for(int j=0;j<=m+1;j++) dist[i][j] = INF; queue<pair<int,int> > Q; dist[1][1] = 0; Q.push({1,1}); while(dist[n][m] == INF){ auto q = Q; while(!Q.empty()) Q.pop(); while(!q.empty()){ auto [x,y] = q.front(); q.pop(); if(t[x][y] == 'X') continue; if(dist[x+1][y] > dist[x][y]) dist[x+1][y] = dist[x][y], q.push({x+1,y}); if(dist[x][y+1] > dist[x][y]) dist[x][y+1] = dist[x][y], q.push({x,y+1}); if(dist[x-1][y] > dist[x][y]+1) dist[x-1][y] = dist[x][y]+1, Q.push({x-1,y}); if(dist[x][y-1] > dist[x][y]+1) dist[x][y-1] = dist[x][y]+1, Q.push({x,y-1}); } } vector<pair<lld,lld> > p(k); for(auto& i : p) cin >> i.first >> i.second; lld res = 1e18; for(auto& i : p) res = min(res, (lld) dist[n][m] * (i.first+i.second) + i.first * (lld) (n+m-2)); int cnt = 0; for(auto& i : p) cnt += (res == (lld) dist[n][m] * (i.first+i.second) + i.first * (lld) (n+m-2)); cout << res << " " << cnt << "\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 | #include<bits/stdc++.h> using namespace std; using lld = long long; const int MAXN = 2e3; const int INF = 1e9; int dist[MAXN+5][MAXN+5]; int main(void){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,m,k; cin >> n >> m >> k; vector<string> t(n+2); for(int i=1;i<=n;i++) cin >> t[i]; t[0] = t[n+1] = string(m+2,'X'); for(int i=1;i<=n;i++) t[i] = "X"+t[i]+"X"; for(int i=0;i<=n+1;i++) for(int j=0;j<=m+1;j++) dist[i][j] = INF; queue<pair<int,int> > Q; dist[1][1] = 0; Q.push({1,1}); while(dist[n][m] == INF){ auto q = Q; while(!Q.empty()) Q.pop(); while(!q.empty()){ auto [x,y] = q.front(); q.pop(); if(t[x][y] == 'X') continue; if(dist[x+1][y] > dist[x][y]) dist[x+1][y] = dist[x][y], q.push({x+1,y}); if(dist[x][y+1] > dist[x][y]) dist[x][y+1] = dist[x][y], q.push({x,y+1}); if(dist[x-1][y] > dist[x][y]+1) dist[x-1][y] = dist[x][y]+1, Q.push({x-1,y}); if(dist[x][y-1] > dist[x][y]+1) dist[x][y-1] = dist[x][y]+1, Q.push({x,y-1}); } } vector<pair<lld,lld> > p(k); for(auto& i : p) cin >> i.first >> i.second; lld res = 1e18; for(auto& i : p) res = min(res, (lld) dist[n][m] * (i.first+i.second) + i.first * (lld) (n+m-2)); int cnt = 0; for(auto& i : p) cnt += (res == (lld) dist[n][m] * (i.first+i.second) + i.first * (lld) (n+m-2)); cout << res << " " << cnt << "\n"; return 0; } |