#pragma GCC optimize ("Ofast,inline") #include <bits/stdc++.h> using namespace std; const int NM = 1'003'111; struct Edge { int v, mod, val; }; int n, m, q; vector<Edge> adj[NM]; int pos(int x, int y) { return x * (m + 1) + y; } void add_edge(int u, int v, int mod, int val) { adj[u].push_back({v, mod, val}); adj[v].push_back({u, mod, val}); } int que[16][NM], fst[NM], cnt[16], vis[NM], dis[NM], tim; char buf[1 << 16]; size_t _pos, _len; inline char gc() { if(_pos == _len) _pos = 0, _len = fread(buf, 1, sizeof(buf), stdin); return buf[_pos++]; } inline int rd() { int a, c; while((a = gc()) < 40); while((c = gc()) > 47) a = a * 10 + c - 480; return a - 48; } inline char* sc(char *s) { char c; while((c = gc()) < 48); *s++ = c; while((c = gc()) > 47) *s++ = c; return s; } int main() { n = rd(), m = rd(), q = rd(); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { static char s[10]; int l = sc(s) - s; s[l] = 0; for(int k = 0; k < l; k++) { if(s[k] == '1') { add_edge(pos(i - 1, j - 1), pos(i - 1, j), l, k); add_edge(pos(i, j - 1), pos(i, j), l, k); } else { add_edge(pos(i - 1, j - 1), pos(i, j - 1), l, k); add_edge(pos(i - 1, j ), pos(i, j ), l, k); } } } } while(q--) { tim++; int t = rd(), a = rd(), b = rd(), c = rd(), d = rd(); fst[pos(a, b)] = tim; dis[pos(a, b)] = t; que[t & 15][cnt[t & 15]++] = pos(a, b); bool ans_found = false; for(int que_id = 0; !ans_found; que_id++) { for(int i = 0; i < cnt[que_id & 15]; i++) { int u = que[que_id & 15][i]; if(u == pos(c, d)) { ans_found = true; break; } if(vis[u] == tim) continue; vis[u] = tim; for(const Edge &e : adj[u]) if(vis[e.v] != tim) { int tim_mod = dis[u] % e.mod; int cost; if(tim_mod <= e.val) cost = e.val - tim_mod; else cost = e.mod - tim_mod + e.val; if(fst[e.v] != tim || dis[e.v] > dis[u] + cost) { fst[e.v] = tim; dis[e.v] = dis[u] + cost; que[dis[e.v] & 15][cnt[dis[e.v] & 15]++] = e.v; } } } cnt[que_id & 15] = 0; } for(int i = 0; i < 16; i++) cnt[i] = 0; printf("%d\n", dis[pos(c, d)]); } }
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 | #pragma GCC optimize ("Ofast,inline") #include <bits/stdc++.h> using namespace std; const int NM = 1'003'111; struct Edge { int v, mod, val; }; int n, m, q; vector<Edge> adj[NM]; int pos(int x, int y) { return x * (m + 1) + y; } void add_edge(int u, int v, int mod, int val) { adj[u].push_back({v, mod, val}); adj[v].push_back({u, mod, val}); } int que[16][NM], fst[NM], cnt[16], vis[NM], dis[NM], tim; char buf[1 << 16]; size_t _pos, _len; inline char gc() { if(_pos == _len) _pos = 0, _len = fread(buf, 1, sizeof(buf), stdin); return buf[_pos++]; } inline int rd() { int a, c; while((a = gc()) < 40); while((c = gc()) > 47) a = a * 10 + c - 480; return a - 48; } inline char* sc(char *s) { char c; while((c = gc()) < 48); *s++ = c; while((c = gc()) > 47) *s++ = c; return s; } int main() { n = rd(), m = rd(), q = rd(); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { static char s[10]; int l = sc(s) - s; s[l] = 0; for(int k = 0; k < l; k++) { if(s[k] == '1') { add_edge(pos(i - 1, j - 1), pos(i - 1, j), l, k); add_edge(pos(i, j - 1), pos(i, j), l, k); } else { add_edge(pos(i - 1, j - 1), pos(i, j - 1), l, k); add_edge(pos(i - 1, j ), pos(i, j ), l, k); } } } } while(q--) { tim++; int t = rd(), a = rd(), b = rd(), c = rd(), d = rd(); fst[pos(a, b)] = tim; dis[pos(a, b)] = t; que[t & 15][cnt[t & 15]++] = pos(a, b); bool ans_found = false; for(int que_id = 0; !ans_found; que_id++) { for(int i = 0; i < cnt[que_id & 15]; i++) { int u = que[que_id & 15][i]; if(u == pos(c, d)) { ans_found = true; break; } if(vis[u] == tim) continue; vis[u] = tim; for(const Edge &e : adj[u]) if(vis[e.v] != tim) { int tim_mod = dis[u] % e.mod; int cost; if(tim_mod <= e.val) cost = e.val - tim_mod; else cost = e.mod - tim_mod + e.val; if(fst[e.v] != tim || dis[e.v] > dis[u] + cost) { fst[e.v] = tim; dis[e.v] = dis[u] + cost; que[dis[e.v] & 15][cnt[dis[e.v] & 15]++] = e.v; } } } cnt[que_id & 15] = 0; } for(int i = 0; i < 16; i++) cnt[i] = 0; printf("%d\n", dis[pos(c, d)]); } } |