forked from sunwaylive/five-minutes-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPathDijkstra.cpp
More file actions
89 lines (82 loc) · 1.97 KB
/
Copy pathShortestPathDijkstra.cpp
File metadata and controls
89 lines (82 loc) · 1.97 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
using namespace std;
const int VN = 5;
class Graph{
public:
Graph();
void shortestPathDijkstra(int , int);
int choose(int);
void addEdge(int s, int e, float weight);
public:
float edge[VN][VN];
float dist[VN];
int path[VN];
int s[VN]; // choose set
};
Graph::Graph(){
for(int i = 0; i < VN; ++i){
for(int j = 0; j < VN; ++j){
edge[i][j] = INT_MAX;
}
}
}
void Graph::addEdge(int s, int e, float weight){
edge[s][e] = weight;
}
//1. initialize 2. add start 3. add loop (find min, add, update)
void Graph::shortestPathDijkstra(int n, int v){
//1. initialize
for(int i = 0; i < n; ++i){
dist[i] = edge[v][i];
s[i] = 0;// 1 mean added
if(i != v && dist[i] < INT_MAX){
path[i] = v;
}else{
path[i] = -1;
}
}
//2. add start
s[v] = 1;
dist[v] = 0;//self to self distance is 0
//3. add loop
for(int i = 0; i < n - 1; ++i){//n - 1 vertex left except v
float min_dist = INT_MAX;
int u = v;
for(int j = 0; j < n; ++j){
if(s[j] == 0 && dist[j] < min_dist){
u = j;
min_dist = dist[j];
}
}
s[u] = 1;//v-u is the shortest
//4. update the rest
for(int w = 0; w < n; ++w){
if(s[w] == 0 && edge[u][w] < INT_MAX && dist[u] + edge[u][w] < dist[w]){
dist[w] = dist[u] + edge[u][w];
path[w] = u;
}
}
}
}
int main()
{
Graph g;
g.addEdge(0, 1, 30);
g.addEdge(0, 3, 10);
g.addEdge(0, 4, 50);
g.addEdge(1, 2, 60);
g.addEdge(1, 4, 80);
g.addEdge(3, 4, 30);
g.addEdge(4, 2, 40);
g.addEdge(4, 0, 50);
int start, end;
cin>>start>>end;
g.shortestPathDijkstra(VN, start);
cout<<g.dist[end]<<endl;
while(end != start){
cout<<end <<" ";
end = g.path[end];
}
cout<<start <<endl;
return 0;
}