-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijstra.cpp
More file actions
58 lines (48 loc) · 1.23 KB
/
dijstra.cpp
File metadata and controls
58 lines (48 loc) · 1.23 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
#include<iostream>
using namespace std;
class graph{
int v;
int **adj;
public:
void dijstra(int start){
int *key=new int[v];
bool *included=new bool[v];
for(int i=0;i<v;i++){
key[i]=1e9;
included[i]=false;
}
key[start]=0;
for(int c=0;c<v-1;c++){
int minval=1e9,minindex=-1;
for(int i=0;i<v;i++){
if(!include[i] && key[i] < minval){
minval=key[i];
minindex=i;
}
}
include[minindex]=true;
for(int i=0;i<v;i++){
if( !included[i] && adj[i][minindex] != 1e9 && key[i] < adj[i][minindex] + key[minindex]){
key[i]=adj[i][minindex] + key[minindex];
}
}
}
for(int i=1;i<v;i++){
cout<<i<<"--"<<<<"--"<<key[i]<<endl;
}
delete[] key;
delete[] included;
}
graph(int v){
this->v=v;
adj=new int*[v];
for(int i=0;i<v;i++ ){
adj[i]=new int[v];
for(int j=0;j<v;j++){
adj[i][j]=(i==j) ? 1e9 : 0;
}
}
}
~graph(){
}
};