// Wycieczka gorska // Sebastian Mroz #include<bits/stdc++.h> #define ll long long #define pp pair<ll, ll> #define ff first #define ss second #define pb push_back using namespace std; const int maxN = 2007; const int maxK = 1000007; const ll INF = 1e18 + 7; ll T[maxN][maxN]; vector<pp> G[maxN][maxN]; queue<pair<pp, ll> > Q; void BFS(pp p){ Q.push({{p.ff, p.ss}, 0}); T[p.ff][p.ss] = -1; while(!Q.empty()){ ll x = Q.front().ff.ff; ll y = Q.front().ff.ss; ll d = Q.front().ss; Q.pop(); for(pp adj : G[x][y]){ if(T[adj.ff][adj.ss] == 0){ T[adj.ff][adj.ss] = d + 1; Q.push({{adj.ff, adj.ss}, d + 1}); } } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, m, k, a, b, gora, dol, ans = 0, maxans = INF, czas; char c; cin >> n >> m >> k; for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ cin >> c; if(c == 'X'){ T[i][j] = -1; continue; } if(i != 1){ if(T[i - 1][j] == 0){ G[i][j].pb({i - 1, j}); G[i - 1][j].pb({i, j}); } } if(j != 1){ if(T[i][j - 1] == 0){ G[i][j].pb({i, j - 1}); G[i][j - 1].pb({i, j}); } } } } /* for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ cout << T[i][j] << ' '; } cout << "\n"; } */ BFS({1, 1}); /* for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ cout << T[i][j] << ' '; } cout << "\n"; } */ gora = n + m - 2; dol = (T[n][m] - gora) / 2; gora += dol; for(int i = 1; i <= k; ++i){ cin >> a >> b; czas = (a * gora) + (b * dol); if(czas < maxans){ maxans = czas; ans = 1; } else if(czas == maxans){ ++ans; } } cout << maxans << ' ' << ans << "\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 | // Wycieczka gorska // Sebastian Mroz #include<bits/stdc++.h> #define ll long long #define pp pair<ll, ll> #define ff first #define ss second #define pb push_back using namespace std; const int maxN = 2007; const int maxK = 1000007; const ll INF = 1e18 + 7; ll T[maxN][maxN]; vector<pp> G[maxN][maxN]; queue<pair<pp, ll> > Q; void BFS(pp p){ Q.push({{p.ff, p.ss}, 0}); T[p.ff][p.ss] = -1; while(!Q.empty()){ ll x = Q.front().ff.ff; ll y = Q.front().ff.ss; ll d = Q.front().ss; Q.pop(); for(pp adj : G[x][y]){ if(T[adj.ff][adj.ss] == 0){ T[adj.ff][adj.ss] = d + 1; Q.push({{adj.ff, adj.ss}, d + 1}); } } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, m, k, a, b, gora, dol, ans = 0, maxans = INF, czas; char c; cin >> n >> m >> k; for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ cin >> c; if(c == 'X'){ T[i][j] = -1; continue; } if(i != 1){ if(T[i - 1][j] == 0){ G[i][j].pb({i - 1, j}); G[i - 1][j].pb({i, j}); } } if(j != 1){ if(T[i][j - 1] == 0){ G[i][j].pb({i, j - 1}); G[i][j - 1].pb({i, j}); } } } } /* for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ cout << T[i][j] << ' '; } cout << "\n"; } */ BFS({1, 1}); /* for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ cout << T[i][j] << ' '; } cout << "\n"; } */ gora = n + m - 2; dol = (T[n][m] - gora) / 2; gora += dol; for(int i = 1; i <= k; ++i){ cin >> a >> b; czas = (a * gora) + (b * dol); if(czas < maxans){ maxans = czas; ans = 1; } else if(czas == maxans){ ++ans; } } cout << maxans << ' ' << ans << "\n"; return 0; } |