I tried to understand the concept of dijkstra algorithm and tried to write some piece of code to find single source shortest path (Not actual Dijkstra)....I think my logics were okay..My surely I have done something wrong..It's always giving me a 0 as min distance....What is wrong with my implementation.? In many implementation I saw they used structure and stuffs I just started Graph algo's and understand Bfs and Dfs .I just tried to work on dijkstra with those simple ideas.I'll really appreciate any help. Thanks :-)
#include<stdio.h>
#include<iostream>
#include<vector>
#include<queue>
#define inf 100
using namespace std;
int main()
{
vector<int> V [100];
int cost[100][100];
queue<int> Q;
int v,e;
scanf("%d %d",&v,&e);
for(int a=0;a<e;a++)
{
int m,n,z;
scanf("%d %d %d",&m,&n,&z);
z=cost[m][n];
V[m].push_back(n);
}
int s,d;
cout<<"Enter Source:";
scanf("%d",&s);
cout<<"Enter Destination:";
scanf("%d",&d);
int dis[100]={inf};int color[100]={0};int parent[100];
dis[s]=0;
dis[d]=inf+1;
int source;
source=s;
Q.push(s);
for(int c=0;c<V[d].size();c++) //I dont need to visit nodes from destination
{
color[V[d][c]]=2;
}
do{
for(int b=0;b<V[source].size();b++)
{
int adj=V[source][b];
if(color[adj]!=2)
{
Q.push(adj);
color[adj]=1;
}
if(dis[adj]>(dis[source]+cost[source][adj]))
{
dis[adj]=dis[source]+cost[source][adj] ;
parent[adj]=source;
}
if(dis[adj]>=dis[d] && adj!=d) //If I detect any path already crossing limit updated by loop I can ignore it
{
color[adj]=2;
}
}
color[source]=2;
Q.pop();
source=Q.front();
}while(!Q.empty());
cout<<"The minimum distance is "<<dis[d]<<" .\n";
}