#include <iostream> #include <vector> #include <algorithm> #include <string> #include <queue> #include <unordered_set> using namespace std; struct agent { int x; int y; int cost; bool operator()(const agent& lhs, const agent& rhs){ return lhs.cost > rhs.cost; } }; vector<vector<bool>> map; unordered_set<int> seen; int x, y, c; bool isvalid(int a, int b){ return (a < x and b < y and a >= 0 and b >= 0 and map[a][b]); } int main(){ cin >> x >> y >> c; for(int i = 0; i < x; i ++){ map.push_back({}); for(int j = 0; j < y; j ++){ char tmp; cin >> tmp; map[i].push_back({tmp == 'X' ? false : true}); } } vector<pair<int, int>> speeds; for(int i = 0; i < c; i++){ int u, d; cin >> u >> d; speeds.emplace_back(u , d); } priority_queue<agent, vector<agent>, agent> agents; agents.push({0, 0, 0}); while(!agents.empty() and (agents.top().x != x - 1 or agents.top().y != y - 1)){ auto curagent = agents.top(); agents.pop(); if(seen.find(2001 * curagent.x + curagent.y) != seen.end()){ continue; } seen.insert(2001 * curagent.x + curagent.y); vector<pair<int, int>> directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; for(auto dir : directions){ agent newagent = curagent; newagent.cost++; newagent.x += dir.first; newagent.y += dir.second; if(seen.find(2001 * newagent.x + newagent.y) == seen.end() and isvalid(newagent.x, newagent.y)){ agents.push(newagent); } } } if(agents.empty() ){ cout << "no route"<<endl; } int extra = agents.top().cost - x - y + 2; int up = x + y - 2 + extra / 2; int down = extra / 2; long long ret = 2000000000000000000ll; int counts = 0; for(auto cur : speeds) { long long score = (long long)cur.first * (long long)up + (long long)cur.second * (long long)down; if(score < ret) { ret = score; counts = 0; } if(score == ret) { counts ++; } } cout << ret << " " << counts << endl; }
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 | #include <iostream> #include <vector> #include <algorithm> #include <string> #include <queue> #include <unordered_set> using namespace std; struct agent { int x; int y; int cost; bool operator()(const agent& lhs, const agent& rhs){ return lhs.cost > rhs.cost; } }; vector<vector<bool>> map; unordered_set<int> seen; int x, y, c; bool isvalid(int a, int b){ return (a < x and b < y and a >= 0 and b >= 0 and map[a][b]); } int main(){ cin >> x >> y >> c; for(int i = 0; i < x; i ++){ map.push_back({}); for(int j = 0; j < y; j ++){ char tmp; cin >> tmp; map[i].push_back({tmp == 'X' ? false : true}); } } vector<pair<int, int>> speeds; for(int i = 0; i < c; i++){ int u, d; cin >> u >> d; speeds.emplace_back(u , d); } priority_queue<agent, vector<agent>, agent> agents; agents.push({0, 0, 0}); while(!agents.empty() and (agents.top().x != x - 1 or agents.top().y != y - 1)){ auto curagent = agents.top(); agents.pop(); if(seen.find(2001 * curagent.x + curagent.y) != seen.end()){ continue; } seen.insert(2001 * curagent.x + curagent.y); vector<pair<int, int>> directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; for(auto dir : directions){ agent newagent = curagent; newagent.cost++; newagent.x += dir.first; newagent.y += dir.second; if(seen.find(2001 * newagent.x + newagent.y) == seen.end() and isvalid(newagent.x, newagent.y)){ agents.push(newagent); } } } if(agents.empty() ){ cout << "no route"<<endl; } int extra = agents.top().cost - x - y + 2; int up = x + y - 2 + extra / 2; int down = extra / 2; long long ret = 2000000000000000000ll; int counts = 0; for(auto cur : speeds) { long long score = (long long)cur.first * (long long)up + (long long)cur.second * (long long)down; if(score < ret) { ret = score; counts = 0; } if(score == ret) { counts ++; } } cout << ret << " " << counts << endl; } |