-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.cpp
123 lines (103 loc) · 2.53 KB
/
dijkstra.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//Copyright@Souhardya
//Question : Djikstra
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
#define f first
#define se second
#define ll long long int
#define ld long double
#define MOD 1000000007
//Same aprroach works for directed||undirected
ll mindist(ll *dist,ll *vis,ll n)
{
ll mn=INT_MAX; ll ind=-1;
for(ll i=0;i<n;i++)
{
if(vis[i]==0&&dist[i]<mn)
{
mn=dist[i];
ind=i;
}
}
return ind;
}
void dijkmat(ll n,ll src,vector<vector<ll>>&v)
{
ll vis[n]={0}; ll dist[n];
for(ll i=0;i<n;i++)
dist[i]=INT_MAX;
dist[src]=0;
for(ll cnt=0;cnt<n-1;cnt++)
{
ll u=mindist(dist,vis,n);
vis[u]=1;
for(ll i=0;i<n;i++)
{
if(v[u][i]&&(dist[u]!=INT_MAX)&&(dist[u]+v[u][i]<dist[i])&&(vis[i]==0))
{
dist[i]=dist[u]+v[u][i];
}
}
}
for(ll i=0;i<n;i++)
cout<<i<<" : "<<dist[i]<<endl;
}
void dijklist(ll n,ll src,vector<vector<pair<ll,ll>>> &v)
{
ll vis[n]={0}; ll dist[n];
for(ll i=0;i<n;i++)
dist[i]=INT_MAX;
dist[src]=0;
//First one distance and second one id
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>q;
q.push(mk(0,src));
while(!q.empty())
{
ll id=(q.top()).se;
q.pop();
vis[id]=1;
for(ll i=0;i<v[id].size();i++)
{
ll tar=v[id][i].f;
ll ds=v[id][i].se;
if((vis[tar]==0)&&(dist[id]!=INT_MAX)&&dist[tar]>dist[id]+ds)
{
dist[tar]=dist[id]+ds;
q.push(mk(dist[tar],tar));
}
}
}
for(ll i=0;i<n;i++)
cout<<i<<" : "<<dist[i]<<endl;
}
int main()
{
ll n,e;
cin>>n>>e;
ll src;
cin>>src;
//Adjacent Matrix approach's vector
vector<vector<ll>>v(n,vector<ll>(n,0));
//Adjacent list approach's vector
vector<vector<pair<ll,ll>>>v2(n);
for(ll i=0;i<e;i++)
{
ll x,y,w;
cin>>x>>y>>w;
//Vector matrix
v[x][y]=w;
v[y][x]=w;
//List matrix
v2[x].pb(mk(y,w));
v2[y].pb(mk(x,w));
}
//Adjacent vector approach
cout<<"Adjacent Matrix Form:"<<endl;
dijkmat(n,src,v);
//Adjacent list approach
cout<<"Adjacent List Form:"<<endl;
dijklist(n,src,v2);
return 0;
}