Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define PI 3.14159265358979323846
- #define mod 998244353
- #define INF 999999999
- #define MAX 110
- #define f first
- #define s second
- #define ll long long
- #define pb push_back
- #define mp make_pair
- #define pii pair<int, int>
- #define vi vector<int>
- #define vii vector< pair<int,int> >
- #define sws ios_base::sync_with_stdio(false)
- #define forn(i, n) for(int i=0; i<(int)(n); i++)
- #define endl '\n'
- using namespace std;
- // https://neps.academy/lesson/215
- typedef struct
- {
- int dist;
- int x, y;
- } vertice;
- struct Comp
- {
- bool operator()(vertice& a, vertice& b)
- {
- return a.dist > b.dist;
- }
- };
- int N;
- bool mat[MAX][MAX], visitado[MAX][MAX];
- int distancia[MAX][MAX];
- int coordx[4]={0,1,0,-1};
- int coordy[4]={1,0,-1,0};
- void dijkstra(int i, int j)
- {
- int a, b;
- vertice aux, aux2;
- aux.dist=0;
- aux.x=i;
- aux.y=j;
- priority_queue< vertice, vector<vertice>, Comp >fila;
- fila.push(aux);
- while(!fila.empty())
- {
- aux=fila.top();
- fila.pop();
- if(!visitado[aux.x][aux.y])
- {
- visitado[aux.x][aux.y]=true;
- distancia[aux.x][aux.y]=aux.dist;
- for(int i=0;i<4;i++)
- {
- a=aux.x+coordx[i];
- b=aux.y+coordy[i];
- if(a>=0 and b>=0 and a<N and b<N and !visitado[a][b])
- {
- aux2.x=a;aux2.y=b;
- aux2.dist=aux.dist+mat[a][b];
- fila.push(aux2);
- }
- }
- }
- }
- }
- int main()
- {
- sws;
- cin >> N;
- forn(i,N)
- forn(j,N)
- {
- cin >> mat[i][j];
- visitado[i][j]=false;
- distancia[i][j]=INF;
- }
- distancia[0][0]=0;
- dijkstra(0,0);
- cout << distancia[N-1][N-1] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement