nicuvlad76

Untitled

Dec 10th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define nmax 1505
  3. #define oo 1e9
  4. using namespace std;
  5.  
  6. struct Element
  7. {
  8. int cost, x, y;
  9.  
  10. bool operator<(const Element &A) const
  11. {
  12. return cost > A.cost;
  13. }
  14. };
  15.  
  16. int a[nmax][nmax], n;
  17. int d[nmax][nmax];
  18. priority_queue <Element> q;
  19.  
  20. void Citire()
  21. {
  22. int X, Y, Z, T, i, j;
  23. cin >> n >> X >> Y >> Z >> T;
  24. for (int i = 1; i <= n; i++)
  25. cin >> a[1][i];
  26. for (i = 2; i <= n; i++)
  27. for (j = 1; j <= n; j++)
  28. a[i][j] = 1 + (a[i-1][j-1] * X + a[i-1][j] * Y + a[i-1][j+1] * Z) % T;
  29. }
  30.  
  31. inline bool Interior(int i, int j)
  32. {
  33. if (i < 1 || i > n || j < 1 || j > n)
  34. return false;
  35. return true;
  36. }
  37.  
  38. void Lee()
  39. {
  40. int dx[] = {0, 0, -1, 1};
  41. int dy[] = {-1, 1, 0, 0};
  42. int i, j, x, y, k;
  43. Element w;
  44. /// init
  45. for (i = 1; i <= n; i++)
  46. for (j = 1; j <= n; j++)
  47. d[i][j] = oo;
  48. w.x = w.y = 1;
  49. w.cost = a[1][1];
  50. q.push(w);
  51. d[1][1] = a[1][1];
  52. while (!q.empty())
  53. {
  54. i = q.top().x;
  55. j = q.top().y;
  56. q.pop();
  57. for (k = 0; k < 4; k++)
  58. {
  59. x = i + dx[k];
  60. y = j + dy[k];
  61. if (Interior(x, y) && d[x][y] > a[x][y] + d[i][j])
  62. {
  63. w.cost = d[x][y] = a[x][y] + d[i][j];
  64. w.x = x;
  65. w.y = y;
  66. q.push(w);
  67. }
  68. }
  69. }
  70. cout << d[n][n] << "\n";
  71. }
  72.  
  73. int main()
  74. {
  75. Citire();
  76. Lee();
  77.  
  78. return 0;
  79. }
Add Comment
Please, Sign In to add comment