Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define NMAX 1501
- using namespace std;
- struct nod
- {
- int x,y,c;
- bool operator <(nod x)const
- {
- return c>x.c;
- }
- };nod xx;
- priority_queue <nod, vector < nod > > q;
- int n, X, Y, Z, T, a[NMAX][NMAX], co[NMAX][NMAX];
- int mi1, mi2, mi3;
- int main()
- {
- int i, j;
- cin>>n>>X>>Y>>Z>>T;
- for(i=1; i<=n; i++)
- cin>>a[1][i];
- for(i=2; i<=n; i++)
- for(j=1; j<=n; j++)
- a[i][j]=1+(a[i-1][j-1]*X+a[i-1][j]*Y+a[i-1][j+1]*Z)%T;
- q.push({1,1,a[1][1]});
- while(!q.empty() and co[n][n]==0)
- {
- xx=q.top();
- if(xx.x>1 and co[xx.x-1][xx.y]==0){q.push({xx.x-1,xx.y,xx.c+a[xx.x-1][xx.y]});co[xx.x-1][xx.y]=xx.c+a[xx.x-1][xx.y];}
- if(xx.x<n and co[xx.x+1][xx.y]==0){q.push({xx.x+1,xx.y,xx.c+a[xx.x+1][xx.y]});co[xx.x+1][xx.y]=xx.c+a[xx.x+1][xx.y];}
- if(xx.y>1 and co[xx.x][xx.y-1]==0){q.push({xx.x,xx.y-1,xx.c+a[xx.x][xx.y-1]});co[xx.x][xx.y-1]=xx.c+a[xx.x][xx.y-1];}
- if(xx.y<n and co[xx.x][xx.y+1]==0){q.push({xx.x,xx.y+1,xx.c+a[xx.x][xx.y+1]});co[xx.x][xx.y+1]=xx.c+a[xx.x][xx.y+1];}
- q.pop();
- }
- int val=co[n][n];
- cout<<val;
- return 0;
- }
Add Comment
Please, Sign In to add comment