Guest User

b.cpp

a guest
Dec 20th, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ull unsigned long long
  4. #define ld long double
  5. #define uint unsigned int
  6. #define f first
  7. #define s second
  8. #define pb push_back
  9. #define mp make_pair
  10. using namespace std;
  11. const int INF = (int)(1e9 + 7);
  12. const ll INFLL = (ll)(1e18 + 7);
  13. const ld Pi = acos(-1.0);
  14. const ld Eps = (ld)(1e-12);
  15. mt19937 mrand(random_device{}());
  16. int rnd(int x) { return mrand() % x;}
  17. int c[600][600], dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
  18. pair <int, int> pr[600][600][30];
  19. ll d[600][600];
  20. vector <pair <int, int> > g[600][600];
  21. void dfs(int x, int y){
  22.     for(int power = 1; power <= 20; power ++){
  23.         pair <int, int> prr = pr[x][y][power - 1];
  24.         pr[x][y][power] = pr[prr.f][prr.s][power - 1];
  25.     }
  26.     for(int i = 0; i < g[x][y].size(); i ++){
  27.         int xx = g[x][y][i].f, yy = g[x][y][i].s;
  28.         dfs(xx, yy);
  29.     }
  30.     return;
  31. }
  32. int main(){
  33.     srand(time(0));
  34.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  35.     //freopen("a.in", "r", stdin);
  36.     //freopen("a.out", "w", stdout);
  37.     //cout.precision(12);
  38.     int n, m;
  39.     cin >> n >> m;
  40.     for(int i = 1; i <= n; i ++){
  41.         for(int j = 1; j <= m; j ++){
  42.             cin >> c[i][j];
  43.             d[i][j] = INFLL;
  44.         }
  45.     }
  46.     set <pair <ll, pair <int, int> > > st;
  47.     for(int i = 1; i <= n; i ++){
  48.         st.insert({c[i][1], {i, 1}});
  49.         d[i][1] = c[i][1];
  50.         st.insert({c[i][m], {i, m}});
  51.         d[i][m] = c[i][m];
  52.     }
  53.     for(int i = 1; i <= m; i ++){
  54.         st.insert({c[1][i], {1, i}});
  55.         d[1][i] = c[1][i];
  56.         st.insert({c[n][i], {n, i}});
  57.         d[n][i] = c[n][i];
  58.     }
  59.     while(!st.empty()){
  60.         pair <ll, pair <int, int> > curr = *st.begin();
  61.         st.erase(curr);
  62.         int x = curr.s.f, y = curr.s.s;
  63.         for(int i = 0; i < 4; i ++){
  64.             int xx = x + dx[i], yy = y + dy[i];
  65.             if(x <= 0 || x > n || y <= 0 || y > m){
  66.                 continue;
  67.             }
  68.             if(d[xx][yy] > d[x][y] + c[xx][yy]){
  69.                 st.erase({d[xx][yy], {xx, yy}});
  70.                 d[xx][yy] = d[x][y] + c[xx][yy];
  71.                 st.insert({d[xx][yy], {xx, yy}});
  72.             }
  73.         }
  74.     }
  75.     for(int x = 1; x <= n; x ++){
  76.         for(int y = 1; y <= m; y ++){
  77.             int dir = -1;
  78.             ll cost = INFLL;
  79.             for(int i = 0; i < 4; i ++){
  80.                 int xx = x + dx[i], yy = y + dy[i];
  81.                 ll curr_cost;
  82.                 if(xx == 0 || xx == n + 1 || yy == 0 || yy == m + 1){
  83.                     curr_cost = 0;
  84.                 }
  85.                 else{
  86.                     curr_cost = d[xx][yy];
  87.                 }
  88.                 if(curr_cost < cost){
  89.                     cost = curr_cost;
  90.                     dir = i;
  91.                 }
  92.             }
  93.             int xx = x + dx[dir], yy = y + dy[dir];
  94.             if(xx == 0 || xx == n + 1 || yy == 0 || yy == m + 1){
  95.                 pr[x][y][0] = {0, 0};
  96.             }
  97.             else{
  98.                 pr[x][y][0] = {xx, yy};
  99.             }
  100.         }
  101.     }
  102.     for(int i = 1; i <= n; i ++){
  103.         for(int j = 1; j <= n; j ++){
  104.             g[pr[i][j][0].f][pr[i][j][0].s].pb({i, j});
  105.         }
  106.     }
  107.     dfs(0, 0);
  108.     int t = 1e5;
  109.     cin >> t;
  110.     while(t --){
  111.         int x, y, k, xx, yy;
  112.         pair <int, int> prr;
  113.         cin >> x >> y >> k;
  114.         xx = x; yy = y;
  115.         for(int power = 20; power >= 0; power --){
  116.             prr = pr[xx][yy][power];
  117.             if(k > (1 << power)){
  118.                 k -= (1 << power);
  119.                 xx = prr.f;
  120.                 yy = prr.s;
  121.             }
  122.         }
  123.         if(k){
  124.             prr = pr[xx][yy][0];
  125.             xx = prr.f;
  126.             yy = prr.s;
  127.         }
  128.         if(!xx){
  129.             xx = yy = -1;
  130.         }
  131.         cout << d[x][y] << ' ' << xx << ' ' << yy << '\n';
  132.     }
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment