-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijktras.cpp
More file actions
54 lines (50 loc) · 1.17 KB
/
Copy pathdijktras.cpp
File metadata and controls
54 lines (50 loc) · 1.17 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
#include<iostream>
#include<vector>
#include<string>
using namespace std;
const int INF=1e9;
int mindistance(vector<int>&dist,vector<bool>&sptset)
{
int min=INF,min_index;
for(int v=0;v<dist.size();v++){
if(!sptset[v]&&dist[v]<=min){
min=dist[v];
min_index=v;
}
}
return min_index;
}
void dijktras(const vector<vector<int> > &graph,int src){
int n=graph.size();
vector<int>dist(n,INF);
vector<bool>sptset(n,false);
dist[src]=0;
for(int count=0;count<n-1;count++){
int u=mindistance(dist,sptset);
sptset[u]=true;
for(int v=0;v<n;v++){
if(!sptset[v]&&graph[u][v]&&dist[u]!=INF&&dist[u]+graph[u][v]<dist[v]){
dist[v]=dist[u]+graph[u][v];
}
}
}
for(int i=0;i<n;i++)
{
if(dist[i]==INF){
cout<<i;
}
else{
cout<<"distance from"<<i<<"is "<<dist[i]<<endl;
}
}
}
int main()
{
vector<vector<int> > graph(4, vector<int>(4));
graph[0][0] = 0; graph[0][1] = 1; graph[0][2] = 4; graph[0][3] = 0;
graph[1][0] = 1; graph[1][1] = 0; graph[1][2] = 2; graph[1][3] = 6;
graph[2][0] = 4; graph[2][1] = 2; graph[2][2] = 0; graph[2][3] = 3;
graph[3][0] = 0; graph[3][1] = 6; graph[3][2] = 3; graph[3][3] = 0;
int source=0;
dijktras(graph,source);
}