-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathDijkstra.cpp.cpp
170 lines (169 loc) · 3.19 KB
/
Dijkstra.cpp.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <list>
#include <climits>
#include <algorithm>
using namespace std;
class Graph{
int V;
int *key;
int *index;
vector<int> heap;
list< pair<int,int> > *adj;
public:
Graph(int v)
{
V=v;
adj=new list< pair<int,int> >[v+1];
key=new int[v+1];
index=new int[v+1];
}
void addEdge(int u,int v,int w)
{
adj[u].push_back(make_pair(v,w));
adj[v].push_back(make_pair(u,w));
}
int Display();
int Dijkstra(int s);
int Minheapify(int s);
int insert(int ,int);
int extract_min();
int parent(int i)
{
return (i-1)/2;
}
};
int Graph::Display()
{
for(int i=1;i<=V;i++)
{
list< pair<int,int> >::iterator k=adj[i].begin();
cout<<i<<" -> ";
while(k!=adj[i].end())
{
cout<<k->first<<" ";
k++;
}
cout<<endl;
}
return 0;
}
int Graph::Dijkstra(int s)
{
bool* visited=new bool[V+1];
for(int i=1;i<=V;i++)
{
index[i]=-1;
visited[i]=false;
key[i]=-1;
}
visited[s]=true;
key[s]=0;
list<pair<int,int> >::iterator k=adj[s].begin();
while(k!=adj[s].end())
{
insert(k->first,k->second+key[s]);
k++;
}
while(!heap.empty())
{
int num=extract_min();
visited[num]=true;
list<pair<int ,int> >::iterator k=adj[num].begin();
while(k!=adj[num].end())
{
if(visited[k->first]==false)
insert(k->first,k->second+key[num]);
k++;
}
}
for(int i=1;i<=V;i++)
{
if(s!=i)
printf("%d ",key[i]);
}
printf("\n");
return 0;
}
int Graph::Minheapify(int i)
{
int l=2*i+1;
int r=2*i+2;
int min;
if(l<heap.size()&&key[heap[i]]>key[heap[l]])
min=l;
else
min=i;
if(r<heap.size()&&key[heap[r]]<key[heap[min]])
min=r;
if(min!=i)
{
int temp=heap[i];
heap[i]=heap[min];
heap[min]=temp;
Minheapify(min);
}
return 0;
}
int Graph::extract_min()
{
int root=heap[0];
heap[0]=heap[heap.size()-1];
heap.resize(heap.size()-1);
Minheapify(0);
return root;
}
int Graph::insert(int n,int dis)
{
int i;
if(key[n]==-1)
{
key[n]=dis;
heap.push_back(n);
i=heap.size()-1;
}
else if(key[n]>dis)
{
key[n]=dis;
for(int j=0;j<heap.size();j++)
{
if(heap[j]==n)
{
i=j;
break;
}
}
}
else
{
return 0;
}
while(i!=0&&key[heap[i]]<key[heap[parent(i)]])
{
int temp=heap[i];
heap[i]=heap[parent(i)];
heap[parent(i)]=temp;
i=parent(i);
}
return 0;
}
int main() {
int nodes,edges;
scanf("%d",&nodes);
scanf("%d",&edges);
Graph g(nodes);
while(edges--)
{
int u,v,w;
scanf("%d",&u);
scanf("%d",&v);
scanf("%d",&w);
g.addEdge(u,v,w);
}
int source;
cin>>source;
g.Dijkstra(source);
return 0;
}