Is my logic correct or am I missing any corner cases?? Can anyone check this out, please?
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int spent,roads,n,source,dest,length,toll;
scanf("%d %d %d",&spent,&roads,&n);
int visited[roads+1]={0};
priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> p;
vector<pair<int,pair<int,int>>> a[n];
while(n--)
{
scanf("%d %d %d %d",&source,&dest,&length,&toll);
a[source].push_back(make_pair(toll,make_pair(dest,length)));
}
p.push(make_pair(0,make_pair(1,0)));
int res=0;
while(!p.empty())
{
toll=p.top().first;
dest=p.top().second.first;
length=p.top().second.second;
p.pop();
if(visited[dest]) continue;
visited[dest]=1;
if(dest==roads)
{
if(toll>spent) cout<<-1<<endl;
else cout<<length<<endl;
break;
}
for(int i=0;i<a[dest].size();i++) {
p.push(make_pair(a[dest][i].first+toll,make_pair(a[dest][i].second.first,a[dest][i].second.second+length)));
}
}
}
}