-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra2.cpp
More file actions
71 lines (68 loc) · 1.11 KB
/
dijkstra2.cpp
File metadata and controls
71 lines (68 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <limits.h>
using namespace std;
int MAX = 100;
vector<pair <int,int> >graph[100];
bool visited[100];
int dist[100];
int V;
void init()
{
for(int i=0;i<MAX;i++)
{
dist[i] = INT_MAX;
visited[i] = false;
}
}
int minDist()
{
int mini = INT_MAX,minVertex;
for(int i=0;i<V;i++)
{
if(!visited[i]&& dist[i]<mini)
mini= dist[i],minVertex=i;
}
return minVertex;
}
void dijkstra(int source)
{
init();
//cout<<"ok\n";
dist[source] = 0;
//visited[source] = true;
for(int i=0;i< V;i++)
{
int u = minDist();
visited[u] = true;
for(int j = 0;j<graph[u].size();j++)
{
int v =graph[u][j].second;
int w = graph[u][j].first;
if(!visited[v] && dist[u]!=INT_MAX && (dist[u] + w <dist[v] ))
dist[v] =dist[u] + w;
}
}
for(int i=0;i<V;i++)
{
cout<<"vertex = "<<i<<" dist ="<<dist[i]<<endl;
}
}
int main(void)
{
cout<<"Enter no of vertex\n";
cin>>V;
cout<<"Enter no of Edges\n";
int e;
cin>>e;
int u,v,w;
for(int i=0;i<e;i++)
{
cin>>u>>v>>w;
graph[u].push_back(make_pair(w, v));
}
//cout<<"ok\n";
dijkstra(0);
}