AlexNeagu11

Untitled

Feb 6th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1505;
  4. const int oo = 2e9;
  5. int dx[4] = {-1, 1, 0, 0};
  6. int dy[4] = {0, 0, -1, 1};
  7. int t[N][N], d[N][N], n, x, y, z, r;
  8. bool f[N][N];
  9. priority_queue < pair < int, pair < int, int > > > q;
  10. bool ok(int x, int y) {
  11. if (x > n || y > n || x < 1 || y < 1) {
  12. return false;
  13. }
  14. return true;
  15. }
  16. void dijk() {
  17. while(q.size()) {
  18. int x = q.top().second.first;
  19. int y = q.top().second.second;
  20. q.pop();
  21. if (f[x][y]) {
  22. continue;
  23. }
  24. f[x][y] = 1;
  25. for (int i = 0; i < 4; i++) {
  26. int new_x = x + dx[i];
  27. int new_y = y + dy[i];
  28. if (ok(new_x, new_y) && d[new_x][new_y] > d[x][y] + t[new_x][new_y]) {
  29. d[new_x][new_y] = d[x][y] + t[new_x][new_y];
  30. q.push({-d[new_x][new_y], {new_x, new_y}});
  31. }
  32. }
  33. }
  34. }
  35. int main() {
  36. cin >> n >> x >> y >> z >> r;
  37. for (int i = 1; i <= n; i++) {
  38. cin >> t[1][i];
  39. }
  40. for (int i = 2; i <= n; i++) {
  41. for (int j = 1; j <= n; j++) {
  42. t[i][j] = 1 + (t[i - 1][j - 1] * x + t[i - 1][j] * y + t[i - 1][j + 1] * z) % r;
  43. }
  44. }
  45. for (int i = 1; i <= n; i++) {
  46. for (int j = 1; j <= n; j++) {
  47. d[i][j] = oo;
  48. }
  49. }
  50. d[1][1] = t[1][1];
  51. q.push({-d[1][1], {1, 1}});
  52. dijk();
  53. cout << d[n][n] << "\n";
  54. }
Advertisement
Add Comment
Please, Sign In to add comment