#include<bits/stdc++.h> using namespace std; int n, m, k; int operations = 0; long long d[4000000]; long long x[1000000]; long long y[1000000]; int xyToId(int x, int y) { return x * m + y; } void dijkstra(long long *d) { int p, newDist; priority_queue<pair<int, int>> points; points.push(make_pair(0, 0)); while(!points.empty()) { /*operations++; if(operations % 1000000 == 0) cout << operations << endl;*/ /* for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(d[xyToId(i,j)] > 1000000) cout << '-'; else cout << d[xyToId(i,j)]; cout << '\t'; } cout << endl; }*/ p = points.top().second; points.pop(); newDist = d[p] + 1; if((p+1)%m > 0 && d[p+1] > newDist) { d[p+1] = newDist; points.push(make_pair(-newDist, p+1)); } if(p%m > 0 && d[p-1] > newDist) { d[p-1] = newDist; points.push(make_pair(-newDist, p-1)); } if(p >= m && d[p-m] > newDist) { d[p-m] = newDist; points.push(make_pair(-newDist, p-m)); } if(p < m*(n-1) && d[p+m] > newDist) { d[p+m] = newDist; points.push(make_pair(-newDist, p+m)); } } } //ifstream in; int main() { //in.open("in1.in"); //in >> n >> m >> k; cin >> n >> m >> k; char elemCin; long long optDist, howManyDown, howManyUp, result, howManyResults; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { cin >> elemCin; //in >> elemCin; d[xyToId(i,j)] = (elemCin == '.' ? LLONG_MAX - 20 : -3); } d[0] = 0; //cout << ":-)"; dijkstra(d); //cout << ":-O"; howManyDown = (d[n*m-1] - (n-1 + m-1)) / 2; howManyUp = d[n*m-1] - howManyDown; result = LLONG_MAX; howManyResults = 0; for(int i = 0; i < k; i++) { cin >> x[i] >> y[i]; //in >> x[i] >> y[i]; optDist = x[i] * howManyUp + y[i] * howManyDown; if(optDist < result) result = optDist; } //in.close(); for(int i = 0; i < k; i++) if(x[i] * howManyUp + y[i] * howManyDown == result) howManyResults++; cout << result << " " << howManyResults; 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | #include<bits/stdc++.h> using namespace std; int n, m, k; int operations = 0; long long d[4000000]; long long x[1000000]; long long y[1000000]; int xyToId(int x, int y) { return x * m + y; } void dijkstra(long long *d) { int p, newDist; priority_queue<pair<int, int>> points; points.push(make_pair(0, 0)); while(!points.empty()) { /*operations++; if(operations % 1000000 == 0) cout << operations << endl;*/ /* for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(d[xyToId(i,j)] > 1000000) cout << '-'; else cout << d[xyToId(i,j)]; cout << '\t'; } cout << endl; }*/ p = points.top().second; points.pop(); newDist = d[p] + 1; if((p+1)%m > 0 && d[p+1] > newDist) { d[p+1] = newDist; points.push(make_pair(-newDist, p+1)); } if(p%m > 0 && d[p-1] > newDist) { d[p-1] = newDist; points.push(make_pair(-newDist, p-1)); } if(p >= m && d[p-m] > newDist) { d[p-m] = newDist; points.push(make_pair(-newDist, p-m)); } if(p < m*(n-1) && d[p+m] > newDist) { d[p+m] = newDist; points.push(make_pair(-newDist, p+m)); } } } //ifstream in; int main() { //in.open("in1.in"); //in >> n >> m >> k; cin >> n >> m >> k; char elemCin; long long optDist, howManyDown, howManyUp, result, howManyResults; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { cin >> elemCin; //in >> elemCin; d[xyToId(i,j)] = (elemCin == '.' ? LLONG_MAX - 20 : -3); } d[0] = 0; //cout << ":-)"; dijkstra(d); //cout << ":-O"; howManyDown = (d[n*m-1] - (n-1 + m-1)) / 2; howManyUp = d[n*m-1] - howManyDown; result = LLONG_MAX; howManyResults = 0; for(int i = 0; i < k; i++) { cin >> x[i] >> y[i]; //in >> x[i] >> y[i]; optDist = x[i] * howManyUp + y[i] * howManyDown; if(optDist < result) result = optDist; } //in.close(); for(int i = 0; i < k; i++) if(x[i] * howManyUp + y[i] * howManyDown == result) howManyResults++; cout << result << " " << howManyResults; return 0; } |