#define _CRT_SECURE_NO_WARNINGS #define ONLINE_JUDGE #include <iostream> #include <vector> #include <cstring> #include <string> #include <limits> #include <algorithm> #include <set> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) typedef long long LL; typedef vector<string> Map; struct Traveler { Traveler(int upTime, int downTime) { this->upTime = upTime; this->downTime = downTime; } int upTime; int downTime; }; struct Input { vector<Traveler> travelers; Map map; }; Input readInput() { int rows, cols, travelers, upTime, downTime; scanf("%d %d %d", &rows, &cols, &travelers); Input result; char buffer[2001]; REP(i, rows) { scanf("%s", &buffer); result.map.push_back(buffer); } REP(i, travelers) { scanf("%d %d", &upTime, &downTime); result.travelers.push_back(Traveler(upTime, downTime)); } return result; } struct Output { LL minimalTime; int travelersWithMinimalTime; }; typedef pair<int, int> Vertex; // row, col class Solver { public: Output solve(Input input) { this->input = input; vector<Vertex> shortestPath = getShortestPath(); LL ups = 0, downs = 0; for (int i = 1; i < shortestPath.size(); ++i) { const Vertex& currentVertex = shortestPath[i]; const Vertex& prevVertex = shortestPath[i - 1]; if (currentVertex.first == prevVertex.first + 1 || currentVertex.second == prevVertex.second + 1) ++ups; else ++downs; } Output result; result.minimalTime = numeric_limits<LL>::max(); result.travelersWithMinimalTime = 0; for (auto traveler : input.travelers) result.minimalTime = min(result.minimalTime, traveler.upTime * ups + traveler.downTime * downs); for (auto traveler : input.travelers) { if (result.minimalTime == traveler.upTime * ups + traveler.downTime * downs) ++result.travelersWithMinimalTime; } return result; } private: vector<Vertex> getShortestPath() { const int Infinity = 1e9; vector<vector<int>> shortestPath(input.map.size(), vector<int>(input.map[0].size(), Infinity)); vector<vector<Vertex>> previousVertex(input.map.size(), vector<Vertex>(input.map[0].size())); typedef pair<int, Vertex> DistanceAndVertex; set<DistanceAndVertex> Q; shortestPath[0][0] = 0; Q.insert(DistanceAndVertex(0, Vertex(0, 0))); const int deltaRow[] = {-1, +0, +1, +0}; const int deltaCol[] = { +0, +1, +0, -1 }; while (!Q.empty()) { const DistanceAndVertex top = *(Q.begin()); const Vertex vertex = top.second; const int distance = top.first; Q.erase(Q.begin()); REP(move, 4) { const int newRow = vertex.first + deltaRow[move]; const int newCol = vertex.second + deltaCol[move]; if (newRow >= 0 && newRow < input.map.size() && newCol >= 0 && newCol < input.map[0].size() && input.map[newRow][newCol] == '.') { if (shortestPath[newRow][newCol] > shortestPath[vertex.first][vertex.second] + 1) { if (shortestPath[newRow][newCol] != Infinity) Q.erase(Q.find(DistanceAndVertex(shortestPath[newRow][newCol], Vertex(newRow, newCol)))); shortestPath[newRow][newCol] = shortestPath[vertex.first][vertex.second] + 1; previousVertex[newRow][newCol] = vertex; Q.insert(DistanceAndVertex(shortestPath[newRow][newCol], Vertex(newRow, newCol))); } } } } vector<Vertex> pathToPeek; auto currentVertex = Vertex(input.map.size() - 1, input.map[0].size() - 1); auto beginning = Vertex(0, 0); while (currentVertex != beginning) { pathToPeek.push_back(currentVertex); currentVertex = previousVertex[currentVertex.first][currentVertex.second]; } pathToPeek.push_back(beginning); reverse(pathToPeek.begin(), pathToPeek.end()); return pathToPeek; } Input input; }; int main() { auto input = readInput(); Solver solver; auto output = solver.solve(input); printf("%lld %d\n", output.minimalTime, output.travelersWithMinimalTime); #ifndef ONLINE_JUDGE system("pause"); #endif 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 | #define _CRT_SECURE_NO_WARNINGS #define ONLINE_JUDGE #include <iostream> #include <vector> #include <cstring> #include <string> #include <limits> #include <algorithm> #include <set> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) typedef long long LL; typedef vector<string> Map; struct Traveler { Traveler(int upTime, int downTime) { this->upTime = upTime; this->downTime = downTime; } int upTime; int downTime; }; struct Input { vector<Traveler> travelers; Map map; }; Input readInput() { int rows, cols, travelers, upTime, downTime; scanf("%d %d %d", &rows, &cols, &travelers); Input result; char buffer[2001]; REP(i, rows) { scanf("%s", &buffer); result.map.push_back(buffer); } REP(i, travelers) { scanf("%d %d", &upTime, &downTime); result.travelers.push_back(Traveler(upTime, downTime)); } return result; } struct Output { LL minimalTime; int travelersWithMinimalTime; }; typedef pair<int, int> Vertex; // row, col class Solver { public: Output solve(Input input) { this->input = input; vector<Vertex> shortestPath = getShortestPath(); LL ups = 0, downs = 0; for (int i = 1; i < shortestPath.size(); ++i) { const Vertex& currentVertex = shortestPath[i]; const Vertex& prevVertex = shortestPath[i - 1]; if (currentVertex.first == prevVertex.first + 1 || currentVertex.second == prevVertex.second + 1) ++ups; else ++downs; } Output result; result.minimalTime = numeric_limits<LL>::max(); result.travelersWithMinimalTime = 0; for (auto traveler : input.travelers) result.minimalTime = min(result.minimalTime, traveler.upTime * ups + traveler.downTime * downs); for (auto traveler : input.travelers) { if (result.minimalTime == traveler.upTime * ups + traveler.downTime * downs) ++result.travelersWithMinimalTime; } return result; } private: vector<Vertex> getShortestPath() { const int Infinity = 1e9; vector<vector<int>> shortestPath(input.map.size(), vector<int>(input.map[0].size(), Infinity)); vector<vector<Vertex>> previousVertex(input.map.size(), vector<Vertex>(input.map[0].size())); typedef pair<int, Vertex> DistanceAndVertex; set<DistanceAndVertex> Q; shortestPath[0][0] = 0; Q.insert(DistanceAndVertex(0, Vertex(0, 0))); const int deltaRow[] = {-1, +0, +1, +0}; const int deltaCol[] = { +0, +1, +0, -1 }; while (!Q.empty()) { const DistanceAndVertex top = *(Q.begin()); const Vertex vertex = top.second; const int distance = top.first; Q.erase(Q.begin()); REP(move, 4) { const int newRow = vertex.first + deltaRow[move]; const int newCol = vertex.second + deltaCol[move]; if (newRow >= 0 && newRow < input.map.size() && newCol >= 0 && newCol < input.map[0].size() && input.map[newRow][newCol] == '.') { if (shortestPath[newRow][newCol] > shortestPath[vertex.first][vertex.second] + 1) { if (shortestPath[newRow][newCol] != Infinity) Q.erase(Q.find(DistanceAndVertex(shortestPath[newRow][newCol], Vertex(newRow, newCol)))); shortestPath[newRow][newCol] = shortestPath[vertex.first][vertex.second] + 1; previousVertex[newRow][newCol] = vertex; Q.insert(DistanceAndVertex(shortestPath[newRow][newCol], Vertex(newRow, newCol))); } } } } vector<Vertex> pathToPeek; auto currentVertex = Vertex(input.map.size() - 1, input.map[0].size() - 1); auto beginning = Vertex(0, 0); while (currentVertex != beginning) { pathToPeek.push_back(currentVertex); currentVertex = previousVertex[currentVertex.first][currentVertex.second]; } pathToPeek.push_back(beginning); reverse(pathToPeek.begin(), pathToPeek.end()); return pathToPeek; } Input input; }; int main() { auto input = readInput(); Solver solver; auto output = solver.solve(input); printf("%lld %d\n", output.minimalTime, output.travelersWithMinimalTime); #ifndef ONLINE_JUDGE system("pause"); #endif return 0; } |