Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1505;
- const int oo = 2e9;
- int dx[4] = {-1, 1, 0, 0};
- int dy[4] = {0, 0, -1, 1};
- int t[N][N], d[N][N], n, x, y, z, r;
- bool f[N][N];
- priority_queue < pair < int, pair < int, int > > > q;
- bool ok(int x, int y) {
- if (x > n || y > n || x < 1 || y < 1) {
- return false;
- }
- return true;
- }
- void dijk() {
- while(q.size()) {
- int x = q.top().second.first;
- int y = q.top().second.second;
- q.pop();
- if (f[x][y]) {
- continue;
- }
- f[x][y] = 1;
- for (int i = 0; i < 4; i++) {
- int new_x = x + dx[i];
- int new_y = y + dy[i];
- if (ok(new_x, new_y) && d[new_x][new_y] > d[x][y] + t[new_x][new_y]) {
- d[new_x][new_y] = d[x][y] + t[new_x][new_y];
- q.push({-d[new_x][new_y], {new_x, new_y}});
- }
- }
- }
- }
- int main() {
- cin >> n >> x >> y >> z >> r;
- for (int i = 1; i <= n; i++) {
- cin >> t[1][i];
- }
- for (int i = 2; i <= n; i++) {
- for (int j = 1; j <= n; j++) {
- t[i][j] = 1 + (t[i - 1][j - 1] * x + t[i - 1][j] * y + t[i - 1][j + 1] * z) % r;
- }
- }
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= n; j++) {
- d[i][j] = oo;
- }
- }
- d[1][1] = t[1][1];
- q.push({-d[1][1], {1, 1}});
- dijk();
- cout << d[n][n] << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment