Guest User

Untitled

a guest
Dec 15th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1.  
  2. int main() {
  3.     auto start = absl::Now();
  4.     vector<vector<int>> A;
  5.     int ans = 0;
  6.     for (std::string line; std::getline(std::cin, line);) {
  7.         A.push_back(vector<int>());
  8.         for (auto c: line) {
  9.             A.back().push_back(c - '0');
  10.         }
  11.     }
  12.  
  13.     int R = A.size();
  14.     int C = A[0].size();
  15.  
  16.     int RR = R * 5;
  17.     int CC = C * 5;
  18.  
  19.     vector<vector<int>> AA(RR, vector<int>(CC));
  20.  
  21.     unordered_map<Point, int, Point::Hash> dist;
  22.     auto comp = [&dist](const Point &a, const Point &b) {
  23.         return dist[a] > dist[b];
  24.     };
  25.  
  26.     std::priority_queue<Point, vector<Point>, decltype(comp)> to_visit(comp);
  27.  
  28.     for (int i = 0; i < RR; ++i) {
  29.         for (int j = 0; j < CC; ++j) {
  30.             AA[i][j] = Get(A, i, j, R, C);
  31.             dist[{.x = i, .y = j}] = 1e9;
  32.         }
  33.     }
  34.  
  35.     unordered_set<Point, Point::Hash> visited;
  36.     dist[{.x = 0, .y = 0}] = 0;
  37.     Point p = {.x = 0, .y = 0};
  38.     to_visit.push({.x = 0, .y = 0});
  39.     while (visited.size() < R * C * 25) {
  40.         p = to_visit.top();
  41.         to_visit.pop();
  42.         if (visited.contains(p)) continue;
  43.         if (visited.size() % 1000 == 0)
  44.             cout << visited.size() << " / " << RR * CC << endl;
  45.         visited.insert(p);
  46.         auto N = neighbors(p, RR, CC);
  47.         for (auto n: N) {
  48.             dist[n] = min(dist[n], dist[p] + AA[n.x][n.y]);
  49.             if (!visited.contains(n)) {
  50.                 to_visit.push(n);
  51.             }
  52.         }
  53.  
  54.         if (to_visit.empty()) break;
  55.     }
  56.  
  57.     cout << dist[{.x = RR - 1, .y = CC - 1}] << endl;
  58.     cout << absl::Now() - start << endl;
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment