#include<bits/stdc++.h> using namespace std; int INF = 1000000000; int EMPTY = -1; void print(vector<vector<int>> v) { for(int i=0; i<v.size(); ++i) { for(int j=0; j<v[i].size(); ++j) { if(v[i][j]==INF) cout << 'X'; else if(v[i][j]==EMPTY) cout << '.'; else cout << v[i][j]; } cout << endl; } } vector<pair<int,int>> gen_sasiedzi(const pair<int,int>& p, const vector<vector<int>>& v) { vector<pair<int,int>> res; int w = p.first; int k = p.second; if(v[w][k-1]==EMPTY) res.push_back(make_pair(w,k-1)); if(v[w][k+1]==EMPTY) res.push_back(make_pair(w,k+1)); if(v[w-1][k]==EMPTY) res.push_back(make_pair(w-1,k)); if(v[w+1][k]==EMPTY) res.push_back(make_pair(w+1,k)); return res; } int main() { ios::sync_with_stdio(0); //freopen("wyc00a.in", "r", stdin); int m, n, k; cin >> m >> n >> k; // m wierszy, n kolumn, k osob vector<vector<int>> v; v.resize(m+2); for(int i=0; i<v.size(); ++i) v[i].resize(n+2, INF); for(int i=1; i<=m; ++i) { for(int j=1; j<=n; ++j) { char c; cin >> c; if(c=='.') v[i][j] = EMPTY; // -1 pole dostepne, nieodwiedzone } } //print(v); // BFS queue<pair<int,int>> q; q.push(make_pair(1,1)); v[1][1] = 0; // v[i][j] >= 0 <=> (i,j) is marked while(!q.empty()) { pair<int,int> p = q.front(); q.pop(); //cout << "front: " << p.first << " " << p.second << endl; int delta_p = v[p.first][p.second]; vector<pair<int,int>> sasiedzi = gen_sasiedzi(p, v); for(int i=0; i<sasiedzi.size(); ++i) { //cout << sasiedzi[i].first << " " << sasiedzi[i].second << endl; int w = sasiedzi[i].first; int k = sasiedzi[i].second; v[w][k] = delta_p+1; q.push(sasiedzi[i]); } } //print(v); int len = v[m][n]; //cout << len << endl; int g = (m-1)+(n-1)+(len-(m-1)-(n-1))/2; int d = (len-(m-1)-(n-1))/2; //cout << "g=" << g << endl; //cout << "d=" << d << endl; vector<long long> time; for(int i=0; i<k; ++i) { int a, b; cin >> a >> b; //cout << a << " " << b << endl; //cout << g*a + d*b << endl; time.push_back((long long)g*a + (long long)d*b); } sort(time.begin(), time.end()); int count = 1; int i=1; while(i<time.size() && time[i]==time[i-1]) { count++; i++; } cout << time[0] << " " << count << endl; }
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 105 106 107 108 109 110 111 | #include<bits/stdc++.h> using namespace std; int INF = 1000000000; int EMPTY = -1; void print(vector<vector<int>> v) { for(int i=0; i<v.size(); ++i) { for(int j=0; j<v[i].size(); ++j) { if(v[i][j]==INF) cout << 'X'; else if(v[i][j]==EMPTY) cout << '.'; else cout << v[i][j]; } cout << endl; } } vector<pair<int,int>> gen_sasiedzi(const pair<int,int>& p, const vector<vector<int>>& v) { vector<pair<int,int>> res; int w = p.first; int k = p.second; if(v[w][k-1]==EMPTY) res.push_back(make_pair(w,k-1)); if(v[w][k+1]==EMPTY) res.push_back(make_pair(w,k+1)); if(v[w-1][k]==EMPTY) res.push_back(make_pair(w-1,k)); if(v[w+1][k]==EMPTY) res.push_back(make_pair(w+1,k)); return res; } int main() { ios::sync_with_stdio(0); //freopen("wyc00a.in", "r", stdin); int m, n, k; cin >> m >> n >> k; // m wierszy, n kolumn, k osob vector<vector<int>> v; v.resize(m+2); for(int i=0; i<v.size(); ++i) v[i].resize(n+2, INF); for(int i=1; i<=m; ++i) { for(int j=1; j<=n; ++j) { char c; cin >> c; if(c=='.') v[i][j] = EMPTY; // -1 pole dostepne, nieodwiedzone } } //print(v); // BFS queue<pair<int,int>> q; q.push(make_pair(1,1)); v[1][1] = 0; // v[i][j] >= 0 <=> (i,j) is marked while(!q.empty()) { pair<int,int> p = q.front(); q.pop(); //cout << "front: " << p.first << " " << p.second << endl; int delta_p = v[p.first][p.second]; vector<pair<int,int>> sasiedzi = gen_sasiedzi(p, v); for(int i=0; i<sasiedzi.size(); ++i) { //cout << sasiedzi[i].first << " " << sasiedzi[i].second << endl; int w = sasiedzi[i].first; int k = sasiedzi[i].second; v[w][k] = delta_p+1; q.push(sasiedzi[i]); } } //print(v); int len = v[m][n]; //cout << len << endl; int g = (m-1)+(n-1)+(len-(m-1)-(n-1))/2; int d = (len-(m-1)-(n-1))/2; //cout << "g=" << g << endl; //cout << "d=" << d << endl; vector<long long> time; for(int i=0; i<k; ++i) { int a, b; cin >> a >> b; //cout << a << " " << b << endl; //cout << g*a + d*b << endl; time.push_back((long long)g*a + (long long)d*b); } sort(time.begin(), time.end()); int count = 1; int i=1; while(i<time.size() && time[i]==time[i-1]) { count++; i++; } cout << time[0] << " " << count << endl; } |