#include <bits/stdc++.h> using namespace std; typedef long long ll; char graph[2020][2020]; bool visited[2020][2020]; set<pair<ll, pair<ll, pair<int, int>>>> ds; #define w1 first #define w2 second.first #define c1 second.second.first #define c2 second.second.second map<ll, int> rc; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; for(int i = 0; i < n; i++) { string row; cin >> row; for(int j = 0; j < m; j++) { graph[i][j] = row[j]; } } // for(ll i = 0; i < n; i++) { // for(ll j = 0; j < m; j++) { // cout << graph[i][j]; // } // cout << endl; // } // cout << endl; ds.insert({0, {0, {n-1, m-1}}}); ll a1, a2; while(1) { auto ds_top = *ds.begin(); ds.erase(ds.begin()); int curr_x = ds_top.c1; int curr_y = ds_top.c2; ll weight1 = ds_top.w1; ll weight2 = ds_top.w2; if(curr_x == 0 && curr_y == 0) { a1 = weight1; a2 = weight2; break; } if(visited[curr_x][curr_y]) { continue; } // cout << curr_x << " " << curr_y << endl; visited[curr_x][curr_y] = true; int next_x; int next_y; next_x = curr_x-1; next_y = curr_y+0; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1+1, {weight2, {next_x, next_y}}}); } next_x = curr_x+1; next_y = curr_y+0; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1, {weight2+1, {next_x, next_y}}}); } next_x = curr_x+0; next_y = curr_y-1; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1+1, {weight2, {next_x, next_y}}}); } next_x = curr_x+0; next_y = curr_y+1; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1, {weight2+1, {next_x, next_y}}}); } } // cout << a1 << " " << a2 << endl; for(ll i = 0; i < n; i++) { for(ll j = 0; j < m; j++) { visited[i][j] = false; } } ds.clear(); ds.insert({0, {0, {n-1, m-1}}}); ll b1, b2; while(1) { auto ds_top = *ds.begin(); ds.erase(ds.begin()); ll curr_x = ds_top.c1; ll curr_y = ds_top.c2; ll weight1 = ds_top.w1; ll weight2 = ds_top.w2; if(curr_x == 0 && curr_y == 0) { b1 = weight1; b2 = weight2; break; } if(visited[curr_x][curr_y]) { continue; } // cout << curr_x << " " << curr_y << endl; visited[curr_x][curr_y] = true; ll next_x; ll next_y; next_x = curr_x-1; next_y = curr_y+0; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1, {weight2+1, {next_x, next_y}}}); } next_x = curr_x+1; next_y = curr_y+0; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1+1, {weight2, {next_x, next_y}}}); } next_x = curr_x+0; next_y = curr_y-1; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1, {weight2+1, {next_x, next_y}}}); } next_x = curr_x+0; next_y = curr_y+1; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1+1, {weight2, {next_x, next_y}}}); } } // cout << b1 << " " << b2 << endl; // cout << endl; for(int i = 0; i < k; i++) { ll a, b; cin >> a >> b; ll local_best = min(a1*a+a2*b, b2*a+b1*b); rc[local_best]++; } cout << rc.begin()->first << " " << rc.begin()->second << "\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 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include <bits/stdc++.h> using namespace std; typedef long long ll; char graph[2020][2020]; bool visited[2020][2020]; set<pair<ll, pair<ll, pair<int, int>>>> ds; #define w1 first #define w2 second.first #define c1 second.second.first #define c2 second.second.second map<ll, int> rc; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; for(int i = 0; i < n; i++) { string row; cin >> row; for(int j = 0; j < m; j++) { graph[i][j] = row[j]; } } // for(ll i = 0; i < n; i++) { // for(ll j = 0; j < m; j++) { // cout << graph[i][j]; // } // cout << endl; // } // cout << endl; ds.insert({0, {0, {n-1, m-1}}}); ll a1, a2; while(1) { auto ds_top = *ds.begin(); ds.erase(ds.begin()); int curr_x = ds_top.c1; int curr_y = ds_top.c2; ll weight1 = ds_top.w1; ll weight2 = ds_top.w2; if(curr_x == 0 && curr_y == 0) { a1 = weight1; a2 = weight2; break; } if(visited[curr_x][curr_y]) { continue; } // cout << curr_x << " " << curr_y << endl; visited[curr_x][curr_y] = true; int next_x; int next_y; next_x = curr_x-1; next_y = curr_y+0; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1+1, {weight2, {next_x, next_y}}}); } next_x = curr_x+1; next_y = curr_y+0; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1, {weight2+1, {next_x, next_y}}}); } next_x = curr_x+0; next_y = curr_y-1; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1+1, {weight2, {next_x, next_y}}}); } next_x = curr_x+0; next_y = curr_y+1; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1, {weight2+1, {next_x, next_y}}}); } } // cout << a1 << " " << a2 << endl; for(ll i = 0; i < n; i++) { for(ll j = 0; j < m; j++) { visited[i][j] = false; } } ds.clear(); ds.insert({0, {0, {n-1, m-1}}}); ll b1, b2; while(1) { auto ds_top = *ds.begin(); ds.erase(ds.begin()); ll curr_x = ds_top.c1; ll curr_y = ds_top.c2; ll weight1 = ds_top.w1; ll weight2 = ds_top.w2; if(curr_x == 0 && curr_y == 0) { b1 = weight1; b2 = weight2; break; } if(visited[curr_x][curr_y]) { continue; } // cout << curr_x << " " << curr_y << endl; visited[curr_x][curr_y] = true; ll next_x; ll next_y; next_x = curr_x-1; next_y = curr_y+0; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1, {weight2+1, {next_x, next_y}}}); } next_x = curr_x+1; next_y = curr_y+0; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1+1, {weight2, {next_x, next_y}}}); } next_x = curr_x+0; next_y = curr_y-1; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1, {weight2+1, {next_x, next_y}}}); } next_x = curr_x+0; next_y = curr_y+1; if(0 <= next_x && next_x < n && 0 <= next_y && next_y < m && graph[next_x][next_y] == '.' && !visited[next_x][next_y]) { ds.insert({weight1+1, {weight2, {next_x, next_y}}}); } } // cout << b1 << " " << b2 << endl; // cout << endl; for(int i = 0; i < k; i++) { ll a, b; cin >> a >> b; ll local_best = min(a1*a+a2*b, b2*a+b1*b); rc[local_best]++; } cout << rc.begin()->first << " " << rc.begin()->second << "\n"; return 0; } |