#include <iostream> #include <limits> #include <queue> #include <vector> #define INF 2147483647 #define FOREACH_IJ for(int i=0;i<n;++i) for (int j=0;j<m;++j) using namespace std; typedef struct { int i,j; int d; } field; class fComp { public: bool operator()(field f1,field f2) { return f1.d > f2.d; } }; int TakeOutMin(char **tab, int **d, int n, int m, int &length, priority_queue<field,vector<field>,fComp> &queue) { int min = INF; int mini, minj; field f; do { f = queue.top(); queue.pop(); } while (tab[f.i][f.j]=='X' && !queue.empty()); tab[f.i][f.j]='X'; length--; return f.i*m+f.j; } void Dijkstra(char **tab, int **d, int n, int m, int &length, priority_queue<field,vector<field>,fComp> &queue) { FOREACH_IJ d[i][j] = INF; d[0][0] = 0; field f = {0,0,0}; queue.push(f); int minInd, i, j; while (!queue.empty() && length>0) { minInd = TakeOutMin(tab,d,n,m,length,queue); //for each neighbour i = minInd/m; j = minInd%m; if (i>0 && tab[i-1][j]=='.') { if (d[i-1][j]>d[i][j]+1) d[i-1][j] = d[i][j]+1; field f1 = {i-1,j,d[i-1][j]}; queue.push(f1); } if (j>0 && tab[i][j-1]=='.') { if (d[i][j-1]>d[i][j]+1) d[i][j-1] = d[i][j]+1; field f1 = {i,j-1,d[i][j-1]}; queue.push(f1); } if (i<n-1 && tab[i+1][j]=='.') { if (d[i+1][j]>d[i][j]+1) d[i+1][j] = d[i][j]+1; field f1 = {i+1,j,d[i+1][j]}; queue.push(f1); } if (j<m-1 && tab[i][j+1]=='.') { if (d[i][j+1]>d[i][j]+1) d[i][j+1] = d[i][j]+1; field f1 = {i,j+1,d[i][j+1]}; queue.push(f1); } } } int main() { int n,m,k; char **tab; priority_queue<field,vector<field>,fComp> queue; int **d; long long *a, *b; cin >> n >> m >> k; tab = new char*[n]; d = new int*[n]; int length = 0; for (int i=0;i<n;++i) { tab[i] = new char[m]; d[i] = new int[m]; } a = new long long[k]; b = new long long[k]; FOREACH_IJ { cin >> tab[i][j]; if (tab[i][j]=='.') length++; } for (int i=0;i<k;++i) cin >> a[i] >> b[i]; Dijkstra(tab,d,n,m,length,queue); long long min = 9223372036854775807; int lmin=0; for (int i=0;i<k;++i) { if (((a[i]+b[i])*(d[n-1][m-1]-n-m+2)/2+a[i]*(n+m-2)<min)) { min = ((a[i]+b[i])*(d[n-1][m-1]-n-m+2)/2+a[i]*(n+m-2)); lmin=1; } else if ((a[i]+b[i])*(d[n-1][m-1]-n-m+2)/2+a[i]*(n+m-2)==min) lmin++; } cout << min << " " << lmin << endl; for (int i=0;i<n;++i) { delete[] tab[i]; delete[] d[i]; } delete[] tab; delete[] d; delete[] a; delete[] b; 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #include <iostream> #include <limits> #include <queue> #include <vector> #define INF 2147483647 #define FOREACH_IJ for(int i=0;i<n;++i) for (int j=0;j<m;++j) using namespace std; typedef struct { int i,j; int d; } field; class fComp { public: bool operator()(field f1,field f2) { return f1.d > f2.d; } }; int TakeOutMin(char **tab, int **d, int n, int m, int &length, priority_queue<field,vector<field>,fComp> &queue) { int min = INF; int mini, minj; field f; do { f = queue.top(); queue.pop(); } while (tab[f.i][f.j]=='X' && !queue.empty()); tab[f.i][f.j]='X'; length--; return f.i*m+f.j; } void Dijkstra(char **tab, int **d, int n, int m, int &length, priority_queue<field,vector<field>,fComp> &queue) { FOREACH_IJ d[i][j] = INF; d[0][0] = 0; field f = {0,0,0}; queue.push(f); int minInd, i, j; while (!queue.empty() && length>0) { minInd = TakeOutMin(tab,d,n,m,length,queue); //for each neighbour i = minInd/m; j = minInd%m; if (i>0 && tab[i-1][j]=='.') { if (d[i-1][j]>d[i][j]+1) d[i-1][j] = d[i][j]+1; field f1 = {i-1,j,d[i-1][j]}; queue.push(f1); } if (j>0 && tab[i][j-1]=='.') { if (d[i][j-1]>d[i][j]+1) d[i][j-1] = d[i][j]+1; field f1 = {i,j-1,d[i][j-1]}; queue.push(f1); } if (i<n-1 && tab[i+1][j]=='.') { if (d[i+1][j]>d[i][j]+1) d[i+1][j] = d[i][j]+1; field f1 = {i+1,j,d[i+1][j]}; queue.push(f1); } if (j<m-1 && tab[i][j+1]=='.') { if (d[i][j+1]>d[i][j]+1) d[i][j+1] = d[i][j]+1; field f1 = {i,j+1,d[i][j+1]}; queue.push(f1); } } } int main() { int n,m,k; char **tab; priority_queue<field,vector<field>,fComp> queue; int **d; long long *a, *b; cin >> n >> m >> k; tab = new char*[n]; d = new int*[n]; int length = 0; for (int i=0;i<n;++i) { tab[i] = new char[m]; d[i] = new int[m]; } a = new long long[k]; b = new long long[k]; FOREACH_IJ { cin >> tab[i][j]; if (tab[i][j]=='.') length++; } for (int i=0;i<k;++i) cin >> a[i] >> b[i]; Dijkstra(tab,d,n,m,length,queue); long long min = 9223372036854775807; int lmin=0; for (int i=0;i<k;++i) { if (((a[i]+b[i])*(d[n-1][m-1]-n-m+2)/2+a[i]*(n+m-2)<min)) { min = ((a[i]+b[i])*(d[n-1][m-1]-n-m+2)/2+a[i]*(n+m-2)); lmin=1; } else if ((a[i]+b[i])*(d[n-1][m-1]-n-m+2)/2+a[i]*(n+m-2)==min) lmin++; } cout << min << " " << lmin << endl; for (int i=0;i<n;++i) { delete[] tab[i]; delete[] d[i]; } delete[] tab; delete[] d; delete[] a; delete[] b; return 0; } |