#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; const int INF = 0x3f3f3f3f; #define FOR(i,b,e) for(int i = (b); i < (e); i++) #define TRAV(x,a) for(auto &x: (a)) #define SZ(x) ((int)x.size()) #define PB push_back #define X first #define Y second const int N = 2020; int dist[N][N]; bool blocked[N][N]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int n, m; void solve(){ int k; cin >> n >> m >> k; FOR(i, 1, n+1) FOR(j, 1, m+1){ char c; cin >> c; blocked[i][j] = (c == 'X'); } FOR(i, 1, n+1) blocked[i][0] = blocked[i][m+1] = 1; FOR(j, 1, m+1) blocked[0][j] = blocked[n+1][j] = 1; memset(dist, INF, sizeof(dist)); dist[1][1] = 0; deque<ii> q; q.PB({1, 1}); while(!q.empty()){ ii v = q.front(); q.pop_front(); FOR(l, 0, 4){ ii u = {v.X+dx[l], v.Y+dy[l]}; if(blocked[u.X][u.Y]) continue; int nd = dist[v.X][v.Y] + (l > 1); if(nd < dist[u.X][u.Y]){ dist[u.X][u.Y] = nd; if(l < 2) q.push_front(u); else q.PB(u); } } } ll mink = 1ll*INF*INF; int ile = 0; FOR(i, 0, k){ ll a, b, tim; cin >> a >> b; tim = dist[n][m]*(a+b) + (n+m-2)*a; if(tim < mink) ile = 0, mink = tim; if(tim == mink) ile++; } cout << mink << ' ' << ile << '\n'; } int main(){ ios::sync_with_stdio(0); cin.tie(0); // int tt; cin >> tt; // FOR(te, 0, tt) solve(); 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 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; const int INF = 0x3f3f3f3f; #define FOR(i,b,e) for(int i = (b); i < (e); i++) #define TRAV(x,a) for(auto &x: (a)) #define SZ(x) ((int)x.size()) #define PB push_back #define X first #define Y second const int N = 2020; int dist[N][N]; bool blocked[N][N]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int n, m; void solve(){ int k; cin >> n >> m >> k; FOR(i, 1, n+1) FOR(j, 1, m+1){ char c; cin >> c; blocked[i][j] = (c == 'X'); } FOR(i, 1, n+1) blocked[i][0] = blocked[i][m+1] = 1; FOR(j, 1, m+1) blocked[0][j] = blocked[n+1][j] = 1; memset(dist, INF, sizeof(dist)); dist[1][1] = 0; deque<ii> q; q.PB({1, 1}); while(!q.empty()){ ii v = q.front(); q.pop_front(); FOR(l, 0, 4){ ii u = {v.X+dx[l], v.Y+dy[l]}; if(blocked[u.X][u.Y]) continue; int nd = dist[v.X][v.Y] + (l > 1); if(nd < dist[u.X][u.Y]){ dist[u.X][u.Y] = nd; if(l < 2) q.push_front(u); else q.PB(u); } } } ll mink = 1ll*INF*INF; int ile = 0; FOR(i, 0, k){ ll a, b, tim; cin >> a >> b; tim = dist[n][m]*(a+b) + (n+m-2)*a; if(tim < mink) ile = 0, mink = tim; if(tim == mink) ile++; } cout << mink << ' ' << ile << '\n'; } int main(){ ios::sync_with_stdio(0); cin.tie(0); // int tt; cin >> tt; // FOR(te, 0, tt) solve(); return 0; } |