-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathdijkstra.cpp
76 lines (65 loc) · 1.88 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
/*Given an undirected, connected and weighted graph G(V, E) with V number of vertices (which are numbered from 0 to V-1) and E number of edges.
Find and print the shortest distance from the source vertex (i.e. Vertex 0) to all other vertices (including source vertex also) using Dijkstra's Algorithm.
Print the ith vertex number and the distance from source in one line separated by space. Print different vertices in different lines.
Time: O((|V| + |E|) log V)
Space: O(|V| + |E|)
*/
#include<climits>
#include <iostream>
using namespace std;
int MinVertex(int* cost,bool*visited ,int v)
{
int min=INT_MAX;
int vertex=-1;
for(int i=0 ;i<v ; i++)
if(!visited[i] && cost[i]<min)
{ min=cost[i] ,vertex=i; }
return vertex;
}
void Dijkstra(int **graph, int v)
{
bool*visited =new bool[v];
int * cost = new int[v];
for(int i=0 ;i<v;i++)
{
visited[i]=false;
cost[i]=INT_MAX;
}
cost[0]=0; //satrting from 0;
for(int i=0 ;i<v-1;i++)
{
int minvertex= MinVertex(cost,visited,v);
visited[minvertex]=true;
for(int j=0 ;j<v;j++)
{
if(!visited[j] && graph[minvertex][j] && cost[j] >(graph[minvertex][j] +cost[minvertex]))
cost[j]=graph[minvertex][j] +cost[minvertex];
}
}
for(int i=0 ;i<v ;i++)
cout<<i<<" "<< cost[i]<<endl;
delete []visited ;
delete [] cost;
}
int main()
{
int V, E, tempX, tempY;
cin >> V >> E;
//input a graph
int ** graph =new int* [V];
for(int i=0 ;i<V ;i++)
{
graph[i]=new int [V];
for(int j=0 ;j<V;j++)
graph[i][j]=0;
}
for(int i =0 ;i<E ;i++)
{
int s,e,w;
cin>>s>>e>>w;
graph[s][e]=w;
graph[e][s]=w;
}
Dijkstra(graph,V);
return 0;
}