Advertisement
Guest User

walls

a guest
Feb 6th, 2021
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. typedef long long ll;
  9. typedef long double ld;
  10. typedef complex<ld> cd;
  11.  
  12. typedef pair<int, int> pi;
  13. typedef pair<ll,ll> pl;
  14. typedef pair<ld,ld> pd;
  15.  
  16. typedef vector<int> vi;
  17. typedef vector<ld> vd;
  18. typedef vector<ll> vl;
  19. typedef vector<pi> vpi;
  20. typedef vector<pl> vpl;
  21. typedef vector<cd> vcd;
  22.  
  23. #define FOR(i, a, b) for (int i=a; i<(b); i++)
  24. #define F0R(i, a) for (int i=0; i<(a); i++)
  25. #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
  26. #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
  27. #define trav(a,x) for (auto& a : x)
  28. #define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
  29.  
  30. #define sz(x) (int)(x).size()
  31. #define mp make_pair
  32. #define pb push_back
  33. #define f first
  34. #define s second
  35. #define lb lower_bound
  36. #define ub upper_bound
  37. #define all(x) x.begin(), x.end()
  38. #define ins insert
  39.  
  40. template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
  41. template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
  42.  
  43. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  44.  
  45. const int MOD = 1000000007;
  46. const char nl = '\n';
  47. const int MX = 100001; //check the limits, dummy
  48.  
  49.  
  50. template<int SZ> struct DSU { //overkill!
  51. int parent[SZ], size[SZ];
  52.  
  53. DSU() {
  54. F0R(i, SZ) parent[i] = i, size[i] = 0;
  55. }
  56.  
  57. int get(int x) {
  58. if (parent[x] != x) parent[x] = get(parent[x]);
  59. return parent[x];
  60. }
  61.  
  62. void unify(int x, int y) {
  63. x = get(x); y = get(y);
  64. if (x == y) return;
  65. if (size[x] < size[y]) swap(x, y);
  66. if (size[x] == size[y]) size[x]++;
  67. parent[y] = x;
  68.  
  69. }
  70. };
  71.  
  72. bool valid(int K) {
  73. if (K < 0 || K >= 48) return false;
  74. int X = K % 2; K /= 2;
  75. int Y = K % 3; K /= 3;
  76. if (Y == 2 && X == 0) return false;
  77. return K < (1 << (max(X, Y) + 1));
  78. }
  79.  
  80. int getTrans(int s, int t) {
  81. if (!valid(s)) return -1;
  82. vi com;
  83. //bool pr = s == 11 && t == 9;
  84. com.pb(0); com.pb(s%2); s /= 2; com.pb(s%3); s /= 3;
  85. DSU<6> dsu;
  86. F0R(i, 3) {
  87. FOR(j, i+1, 3) {
  88. if (com[i] == com[j]) dsu.unify(i, j);
  89. }
  90. }
  91. F0R(i, 3) {
  92. if (t & (1 << i)) {
  93. dsu.unify(i, i+3);
  94. }
  95. }
  96. FOR(i, 3, 5) {
  97. if (t & (1 << i)) dsu.unify(i, i+1);
  98. }
  99. int nx = 1; if (dsu.get(4) == dsu.get(3)) nx = 0;
  100. int ny = nx + 1; if (dsu.get(5) == dsu.get(4)) ny = nx;
  101. if (dsu.get(5) == dsu.get(3)) ny = 0;
  102. int con[3]; F0R(i, 3) con[i] = -1;
  103. F0R(i, 3) {
  104. F0R(j, 3) {
  105. if (dsu.get(i) == dsu.get(j+3)) {
  106. if (j == 0) {
  107. con[com[i]] = 0;
  108. } else if (j == 1) {
  109. con[com[i]] = nx;
  110. } else {
  111. con[com[i]] = ny;
  112. }
  113. }
  114. }
  115. }
  116. int res = 0;
  117. F0R(i, 3) {
  118. if (s & (1 << i)) {
  119. if (con[i] == -1) return -1;
  120. res = res | (1 << con[i]);
  121. }
  122. }
  123.  
  124. return res*6 + ny*2 + nx;
  125.  
  126. }
  127.  
  128. int add(int s, int t) {
  129. if (!valid(s)) return -1;
  130. int x = s % 2; s /= 2; int y = s % 3; s /= 3;
  131. if (t & 1) {
  132. s = s | 1;
  133. }
  134. if (t & 2) {
  135. s = s | (1 << x);
  136. }
  137. if (t & 4) {
  138. s = s | (1 << y);
  139. }
  140. return s*6 + y*2 + x;
  141. }
  142.  
  143. void solve() {
  144.  
  145. int trans[48][32]; F0R(i, 48) F0R(j, 32) trans[i][j] = getTrans(i, j);
  146.  
  147. int N; cin >> N;
  148. int mask[N]; F0R(i, N) mask[i] = 0;
  149. int M; cin >> M;
  150. int H[N-1][3], V[N][2];
  151. F0R(i, 3) F0R(j, N-1) cin >> H[j][i];
  152. F0R(i, 2) F0R(j, N) cin >> V[j][i];
  153. F0R(i, M) {
  154. int X, Y; cin >> X >> Y; X--; Y--;
  155. mask[Y] += 1 << X;
  156. }
  157. ll dp[N][48]; F0R(i, N) F0R(j, 48) dp[i][j] = 1e18;
  158. dp[0][add(5, mask[0])] = 0;
  159. dp[0][add(2, mask[0])] = V[0][0];
  160. dp[0][add(3, mask[0])] = V[0][1];
  161. dp[0][add(0, mask[0])] = V[0][0] + V[0][1];
  162. F0R(i, N-1) {
  163. F0R(j, 48) {
  164. if (!valid(j)) continue;
  165. F0R(k, 32) {
  166. int nxt = trans[j][k]; nxt = add(nxt, mask[i+1]);
  167. ll cost = dp[i][j];
  168. F0R(x, 3) {
  169. if (k & (1 << x)) {
  170. cost += H[i][x];
  171. }
  172. }
  173. F0R(x, 2) {
  174. if (k & (1 << (x+3))) {
  175. cost += V[i+1][x];
  176. }
  177. }
  178. if (nxt != -1) ckmin(dp[i+1][nxt], cost);
  179. }
  180. }
  181. }
  182. ll ans = 1e18;
  183. F0Rd(i, N) {
  184. F0R(j, 48) {
  185. if (valid(j) && __builtin_popcount(j/6) == 1) ckmin(ans, dp[i][j]);
  186. }
  187. if (mask[i] > 0) break;
  188. }
  189. cout << ans << nl;
  190.  
  191. }
  192.  
  193. int main() {
  194. ios_base::sync_with_stdio(0); cin.tie(0);
  195.  
  196. int T = 1;
  197. // cin >> T;
  198. while(T--) {
  199. solve();
  200. }
  201.  
  202. return 0;
  203. }
  204.  
  205. // read the question correctly (ll vs int)
  206. // template by bqi343
  207.  
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement