#include<bits/stdc++.h>
#define pii pair<int, int>
#define INF 1<<30
#define N 1000000
using namespace std;
vector<pii>adj[N];
int node, edge;
void dijstkra(int start)
{
priority_queue<pii, vector<pii>, greater<pii>> q;
vector<int>d(N, INF);
d[start] = 0;
q.push(pii(0, start));
while(!q.empty())
{
int u = q.top().second;
q.pop();
for(auto x : adj[u])
{
int v = x.first;
int weight = x.second;
if(d[v] > d[u] + weight)
{
d[v] = d[u] + weight;
q.push(pii(d[v], v));
}
}
}
cout << "\nDistance of Node From Source :: \n";
for(int i=0;i<=node;i++)
cout << start << " to " << i << " :: " << d[i] << "\n";
}
int main()
{
cout << "Enter Node Number :: ";
cin >> node; ///Here Node Count From Zero
cout << "Enter Edge Number :: ";
cin >> edge;
int n1, n2, weight, source;
cout << "\nEnter Nodes And Between Distance :: \n";
for(int i=1;i<=edge;i++)
{
cin >> n1 >> n2 >> weight;
adj[n1].push_back(pii(n2, weight));
adj[n2].push_back(pii(n1, weight));
}
cout << "\nEnter Source Node :: ";
cin >> source;
dijstkra(source);
return 0;
}
Comments
Post a Comment