#include <iostream> #include <vector> #include <cstdlib> #include <queue> #include <algorithm> using namespace std; #define REP(i,n) for(int i=0; i<(n); i++) #define FOR(i,a,b) for(int i=(a); i<=(b); i++) const int INF = 2000000000; const int MAXN = 2010; int n, m, k; char map[MAXN][MAXN]={0}; int d[MAXN][MAXN]; pair<int, int> pre[MAXN][MAXN]; int adjX[4] = {1, 0, -1, 0}; int adjY[4] = {0, -1, 0, 1}; void solve() { queue<pair<int, int> > q; q.push(make_pair(0,0)); d[0][0] = 0; while(!q.empty()) { pair<int,int> v = q.front(); q.pop(); int row = v.first; int col = v.second; REP(i,4) { int r = row + adjY[i]; int c = col + adjX[i]; if(r<0 || c<0 || r >= n || c >= m) continue; if(map[r][c] == 1) continue; if(d[row][col] + 1 >= d[r][c]) continue; d[r][c] = d[row][col] + 1; pre[r][c] = make_pair(row, col); q.push(make_pair(r,c)); } } } int main() { cin >> n >> m >> k; string row; REP(i,n) { cin >> row; REP(j,m) { map[i][j] = row[j]=='.' ? 0 : 1; d[i][j] = INF; } } solve(); long long a=0, b=0; int r = n-1, c = m-1; while(r+c > 0) { pair<int, int> v = pre[r][c]; int rp = v.first; int cp = v.second; if(rp < r || cp < c) a++; else b++; r = rp; c = cp; } long long best = 2LL * MAXN * MAXN * 1000000100; vector<long long> times(k, 0); int ta, tb; REP(i,k) { cin >> ta >> tb; times[i] = a*ta + b*tb; best = min(best, times[i]); } int numBest = 0; REP(i,k) { if(times[i] == best) numBest++; } cout << best << " " << numBest << 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 | #include <iostream> #include <vector> #include <cstdlib> #include <queue> #include <algorithm> using namespace std; #define REP(i,n) for(int i=0; i<(n); i++) #define FOR(i,a,b) for(int i=(a); i<=(b); i++) const int INF = 2000000000; const int MAXN = 2010; int n, m, k; char map[MAXN][MAXN]={0}; int d[MAXN][MAXN]; pair<int, int> pre[MAXN][MAXN]; int adjX[4] = {1, 0, -1, 0}; int adjY[4] = {0, -1, 0, 1}; void solve() { queue<pair<int, int> > q; q.push(make_pair(0,0)); d[0][0] = 0; while(!q.empty()) { pair<int,int> v = q.front(); q.pop(); int row = v.first; int col = v.second; REP(i,4) { int r = row + adjY[i]; int c = col + adjX[i]; if(r<0 || c<0 || r >= n || c >= m) continue; if(map[r][c] == 1) continue; if(d[row][col] + 1 >= d[r][c]) continue; d[r][c] = d[row][col] + 1; pre[r][c] = make_pair(row, col); q.push(make_pair(r,c)); } } } int main() { cin >> n >> m >> k; string row; REP(i,n) { cin >> row; REP(j,m) { map[i][j] = row[j]=='.' ? 0 : 1; d[i][j] = INF; } } solve(); long long a=0, b=0; int r = n-1, c = m-1; while(r+c > 0) { pair<int, int> v = pre[r][c]; int rp = v.first; int cp = v.second; if(rp < r || cp < c) a++; else b++; r = rp; c = cp; } long long best = 2LL * MAXN * MAXN * 1000000100; vector<long long> times(k, 0); int ta, tb; REP(i,k) { cin >> ta >> tb; times[i] = a*ta + b*tb; best = min(best, times[i]); } int numBest = 0; REP(i,k) { if(times[i] == best) numBest++; } cout << best << " " << numBest << endl; return 0; } |