-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFinding_shortest_path_from_source.cpp
More file actions
121 lines (114 loc) · 2.95 KB
/
Copy pathFinding_shortest_path_from_source.cpp
File metadata and controls
121 lines (114 loc) · 2.95 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
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
/*
PRIYANSHU IS A PEACEFULL SOUL
*/
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define ff first
#define ll long long
#define ss second
using namespace std;
int findMinVertex(int dist[],bool visited[],int n)
{
int minVertex = -1;
for(int i=1;i<=n;i++)
{
if(!visited[i] && (minVertex == -1 || dist[i] < dist[minVertex] ))
{
minVertex = i;
}
}
return minVertex;
}
void shortest_path(int numOfNode,int *arr,int s)
{
int g[numOfNode+1][numOfNode+1];
for(int i=0;i<numOfNode;i++)
{
for(int j=0;j<numOfNode;j++)
{
g[i+1][j+1] = *((arr+i*numOfNode) + j);
}
}
int dist[numOfNode+1];
bool visited[numOfNode+1];
list <int> *path = new list<int>[numOfNode+1];
for(int i=1;i<=numOfNode;i++)
{
dist[i] = INT_MAX;
visited[i] = false;
}
dist[s] = 0;
path[s].push_back(s);
for(int i=1;i<=numOfNode;i++)
{
int minVertex = findMinVertex(dist,visited,numOfNode);
visited[minVertex] = true;
for(int j=1;j<=numOfNode;j++)
{
if(g[minVertex][j] > 0 && !visited[j])
{
int distance = dist[minVertex] + g[minVertex][j];
if(distance < dist[j])
{
path[j].clear();
list <int>l = path[minVertex];
while(!l.empty())
{
int temp = l.front();
path[j].push_back(temp);
l.pop_front();
}
path[j].push_back(j);
dist[j] = distance;
}
}
}
}
for(int i=1;i<=numOfNode;i++)
{
cout << i << "\t" << dist[i] << "\t" << s;
path[i].pop_front();
while(!path[i].empty())
{
cout << "->";
cout << path[i].front();
path[i].pop_front();
}
cout << endl;
}
}
int main()
{
int numOfNode,source;
cin >> numOfNode;
int g[numOfNode][numOfNode];
for(int i=0;i<numOfNode;i++)
{
for(int j=0;j<numOfNode;j++)
{
cin >> g[i][j];
if(i == j && g[i][j] != 0)
{
cout << "Weight of the edge " << i+1 << " <-> " << j+1 << " must be 0";
exit(0);
}
if(i != j && g[i][j] == 0)
{
cout << "Weight of the edge " << i+1 << " <-> " << j+1 << " can not be 0";
exit(0);
}
if(g[i][j] < -1)
{
cout << "Weight of the edge " << i+1 << " <-> " << j+1 << " can not be negative";
exit(0);
}
}
}
cin >> source;
shortest_path(numOfNode,(int *)g,source);
return 0;
}