#include <iostream>
#include<algorithm>
#include<bits/stdc++.h>
#include<string.h>
#define mod 1e18
using namespace std;
typedef long long int ll;
ll dx[4]={-1,0,0,1};
ll dy[4]={0,-1,1,0};
ll dijkstra(){
ll n,i,j;
cin>>n;
ll mat[n][n];
for(i=0;i<n;i++){
for(j=0;j<n;j++){
cin>>mat[i][j];
}
}
ll dp[n][n];
for(i=0;i<n;i++){
for(j=0;j<n;j++){
dp[i][j]=mod;
}
}
dp[0][0]=mat[0][0];
set<pair<ll,pair<ll,ll>>>q;
q.insert(make_pair(mat[0][0],make_pair(0,0)));
while(!q.empty()){
ll x,y,len,p,q,u;
set<pair<ll,pair<ll,ll>>> :: iterator it=q.begin();
len=it.first;
x=it.second.first;
y=it.second.second;
q.erase(q.begin());
for(ll i=0;i<4;i++){
p=x+dx[i];
q=y+dy[i];
if(p>=0 && q>=0 && p<n && q<n){
u=dp[x][y]+mat[p][q];
if(u<dp[p][q]){
q.erase({dp[p][q],{p,q}});
q.insert(make_pair(u,make_pair(p,q)));
dp[p][q]=u;
}
}
}
}
return dp[n-1][n-1];
}
int main() {
ll t;
cin>>t;
while(t--){
cout<<dijkstra()<<endl;
}
return 0;
}
It is giving me error
i.e.
error: request for member ‘begin’ in ‘q’, which is of non-class type ‘ll {aka long long int}’
set<pair<ll,pair<ll,ll>>> :: iterator it=q.begin();
Help @everule1
You’ve got two entirely different variables both named q
:
line 26:
set<pair<ll,pair<ll,ll>>>q;
and
line 29:
ll x,y,len,p,q,u;
The first error occurs on line 29, where the latter q
hides the former q
- give the two variables different names.
Edit:
After you’ve done that, replace all:
it.
with
it->
3 Likes
Thank you so much…
Can you please tell when we use . and when we use ->
1 Like
a->b
is, by convention, equivalent to (*a).b
, so use ->
whenever you would have to do (*a).b
instead of a.b
. Common situations include:
- When
a
is a pointer to an object of class type, andb
is a member of the class; and - When
a
is an iterator referring to an object of class type, andb
is a member of the class (this is the situation occurring in your code, above).
3 Likes