I have tried to implement Dijkstra's algorithm in C++ using std::priority_queue.The function "dijsktra" will take input as 2 nodes, the source vertex and the destination vertex. However, I am getting incorrect answers every time. Kindly help me out and tell me where I have erred. My code-
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <stdio.h>
#include <vector>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#define inf 9999999
using namespace std;
map <int,int> vis;
vector <vector <pair <int,int> > > adj(100);
vector <int> dist(100);
void dijkstra(int u,int t)
{
priority_queue <pair <int,int> > q;
int b,w,i;
q.push({0,u});
vis[u]=1;
while(!q.empty())
{
u = q.top().second; q.pop();
if(!vis[u])
{
vis[u]=1;
for(i=0;i<adj[u].size();i++)
{
b = adj[u][i].first; w = adj[u][i].second;
if(dist[b]>dist[u]+w)
{
dist[b] = dist[u] + w;
q.push({-dist[b],b});
}
}
}
}
cout << dist[t];
}
int main()
{
int i,j,k,n,m,x,y,w,t;
cin >> n >> m;
for(i=0;i<m;i++)
{
cin >> x >> y >> w;
adj[x].push_back({y,w});
adj[y].push_back({x,w});
}
cin >> t;
for(i=1;i<=n;i++)
{
dist[i]=inf;
}
dijkstra(1,t);
}