#include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, a, b, z,m,d; vi vk, va, vb; vector<vector<bool> > vvp; vector<bool> vg; vii vk1, vk2,vkx; vvi vvk; vvii vvkx; vs vp; string w; int main() { // freopen("c:\\wojtek\\uva\\pa\\debug\\ele167.in", "rt", stdin); cin >> n>>m>>k; vp.clear(); for (int i = 0; i < n; i++) { cin >> w; vp.push_back(w); } va.clear(); vb.clear(); for (int i = 0; i < k; i++) { cin >> a >> b; va.push_back(a); vb.push_back(b); } vk1.clear(); vk2.clear(); vkx.assign(m, MP(0,0)); vvkx.assign(n, vkx); vk1.push_back(MP(0, 0)); vg.assign(m, false); vvp.assign(n, vg); vvp[0][0] = true; while ((int)vk1.size() > 0) { for (int i = 0; i < vk1.size(); i++) { pii p = vk1[i]; if ((p.first > 0) && (vp[p.first - 1][p.second] == '.')&&(!vvp[p.first-1][p.second])) { vvkx[p.first - 1][p.second] = p; vvp[p.first - 1][p.second] = true; vk2.push_back(MP(p.first - 1, p.second)); } if ((p.second > 0) && (vp[p.first][p.second-1] == '.') && (!vvp[p.first][p.second-1])) { vvkx[p.first][p.second - 1] = p; vvp[p.first][p.second-1] = true; vk2.push_back(MP(p.first, p.second-1)); } if ((p.first <n-1) && (vp[p.first + 1][p.second] == '.') && (!vvp[p.first + 1][p.second])) { vvkx[p.first + 1][p.second] = p; vvp[p.first + 1][p.second] = true; vk2.push_back(MP(p.first + 1, p.second)); } if ((p.second < m - 1) && (vp[p.first][p.second+1] == '.') && (!vvp[p.first][p.second+1])) { vvkx[p.first][p.second + 1] = p; vvp[p.first][p.second+1] = true; vk2.push_back(MP(p.first, p.second+1)); } } vk1.swap(vk2); vk2.clear(); } a = 0; b = 0; pii p0 = MP(0, 0); pii p = MP(n - 1, m - 1); while (p != p0) { if ((vvkx[p.first][p.second].first < p.first) || (vvkx[p.first][p.second].second < p.second)) a++; else b++; p = vvkx[p.first][p.second]; } z = 1000000000000000000ll; d = 0; for (int i = 0; i < k; i++) { if (va[i] * a + vb[i] * b < z) { z = va[i] * a + vb[i] * b; d = 1; } else if (va[i] * a + vb[i] * b == z) { d++; } } cout << z<<" "<<d << endl; 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 | #include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, a, b, z,m,d; vi vk, va, vb; vector<vector<bool> > vvp; vector<bool> vg; vii vk1, vk2,vkx; vvi vvk; vvii vvkx; vs vp; string w; int main() { // freopen("c:\\wojtek\\uva\\pa\\debug\\ele167.in", "rt", stdin); cin >> n>>m>>k; vp.clear(); for (int i = 0; i < n; i++) { cin >> w; vp.push_back(w); } va.clear(); vb.clear(); for (int i = 0; i < k; i++) { cin >> a >> b; va.push_back(a); vb.push_back(b); } vk1.clear(); vk2.clear(); vkx.assign(m, MP(0,0)); vvkx.assign(n, vkx); vk1.push_back(MP(0, 0)); vg.assign(m, false); vvp.assign(n, vg); vvp[0][0] = true; while ((int)vk1.size() > 0) { for (int i = 0; i < vk1.size(); i++) { pii p = vk1[i]; if ((p.first > 0) && (vp[p.first - 1][p.second] == '.')&&(!vvp[p.first-1][p.second])) { vvkx[p.first - 1][p.second] = p; vvp[p.first - 1][p.second] = true; vk2.push_back(MP(p.first - 1, p.second)); } if ((p.second > 0) && (vp[p.first][p.second-1] == '.') && (!vvp[p.first][p.second-1])) { vvkx[p.first][p.second - 1] = p; vvp[p.first][p.second-1] = true; vk2.push_back(MP(p.first, p.second-1)); } if ((p.first <n-1) && (vp[p.first + 1][p.second] == '.') && (!vvp[p.first + 1][p.second])) { vvkx[p.first + 1][p.second] = p; vvp[p.first + 1][p.second] = true; vk2.push_back(MP(p.first + 1, p.second)); } if ((p.second < m - 1) && (vp[p.first][p.second+1] == '.') && (!vvp[p.first][p.second+1])) { vvkx[p.first][p.second + 1] = p; vvp[p.first][p.second+1] = true; vk2.push_back(MP(p.first, p.second+1)); } } vk1.swap(vk2); vk2.clear(); } a = 0; b = 0; pii p0 = MP(0, 0); pii p = MP(n - 1, m - 1); while (p != p0) { if ((vvkx[p.first][p.second].first < p.first) || (vvkx[p.first][p.second].second < p.second)) a++; else b++; p = vvkx[p.first][p.second]; } z = 1000000000000000000ll; d = 0; for (int i = 0; i < k; i++) { if (va[i] * a + vb[i] * b < z) { z = va[i] * a + vb[i] * b; d = 1; } else if (va[i] * a + vb[i] * b == z) { d++; } } cout << z<<" "<<d << endl; return 0; } |