-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjkstra.cpp
50 lines (47 loc) · 1.12 KB
/
djkstra.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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int v,e;
cin>>v>>e;
int** edges=new int*[v];
for(int i=0;i<v;i++)
edges[i]=new int[v];
//memset(edges,-1,sizeof(edges));
for(int i=0;i<v;i++)
for(int j=0;j<v;j++)
edges[i][j]=-1;
int sv,ev,w;
for(int i=0;i<v;i++)
{
cin>>sv>>ev>>w;
edges[sv][ev]=w;
}
int *distance=new int[v];
bool* visited=new bool[v];
for(int i=0;i<v;i++)
distance[i]=INT_MAX;
distance[0]=0;
for(int i=0;i<v;i++)
visited[i]=false;
int k=v;
while(k--)
{
int mn=INT_MAX;
for(int i=0;i<v;i++)
if(distance[i]<mn && !visited[i])
mn=i;
for(int i=0;i<v;i++)
{
if(edges[mn][i]!=-1 && !visited[i])
{
if(edges[mn][i]+distance[mn]<distance[i]){
distance[i]=distance[mn]+edges[mn][i];
}
}
}
visited[mn]=true;
}
for(int i=0;i<v;i++)
cout<<i<<" "<<distance[i]<<endl;
}