Advertisement
pb_jiang

ABC348TLE

Apr 10th, 2024
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #ifndef __DEBUG__
  5. #define dbg(...) 42
  6. #endif
  7. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  8.  
  9. using ll = long long;
  10. using pii = pair<int, int>;
  11. using pll = pair<ll, ll>;
  12. using vl = vector<ll>;
  13. using vi = vector<int>;
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.     ll h, w, n;
  18.     cin >> h >> w;
  19.     vector<string> mz(h);
  20.     for (auto &s : mz)
  21.         cin >> s;
  22.     cin >> n;
  23.     using a2l = array<ll, 2>;
  24.     using a3l = array<ll, 3>;
  25.     map<a2l, ll> eng, dist;
  26.     for (ll i = 0, x, y, e; i < n; ++i)
  27.         cin >> x >> y >> e, eng[{x - 1, y - 1}] = e;
  28.  
  29.     ll ds[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  30.     bool reach = false;
  31.     queue<a3l> q;
  32.     ll s, t;
  33.     for (ll i = 0; i < h; ++i)
  34.         for (ll j = 0; j < w; ++j)
  35.             if (mz[i][j] == 'S')
  36.                 s = i, t = j;
  37.     q.push({0, s, t});
  38.     while (!q.empty()) {
  39.         auto [e, x, y] = q.front();
  40.         q.pop();
  41.         if (mz[x][y] == 'T')
  42.             reach = true;
  43.         if (eng.count({x, y}))
  44.             e += eng[{x, y}], eng.erase({x, y});
  45.         if (e == 0)
  46.             continue;
  47.         for (ll i = 0; i < 4; ++i) {
  48.             ll nx = x + ds[i][0], ny = y + ds[i][1];
  49.             if (nx >= 0 && nx < h && ny >= 0 && ny < w && mz[nx][ny] != '#') {
  50.                 /*
  51.                 if (dist.count({nx, ny}) == 0 || dist[{nx, ny}] < e - 1)
  52.                     dist[{nx, ny}] = e - 1, q.push({e - 1, nx, ny});
  53.                 */
  54.                 q.push({e - 1, nx, ny});
  55.             }
  56.         }
  57.     }
  58.     cout << (reach ? "Yes\n" : "No\n");
  59.     return 0;
  60. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement