-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathMultisource_Shortest_Path.cpp
79 lines (58 loc) · 1.41 KB
/
Multisource_Shortest_Path.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
#include<bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
using namespace std;
void solver() {
cout << "Enter the number of vertices,edges and sources" << endl;
int n,m,s;
cin >> n >> m >> s;
vector<int> adj[n+1];
vector<int> sources;
cout << "Enter the sources: ";
for(int i = 0;i<s;i++) {
int src;
cin >> src;
sources.push_back(src);
}
cout << "Enter the edges" << endl;
while(m--) {
int u,v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> dist(n+1,1e9);
vector<int> vis(n+1,0);
queue<int> q;
for(auto it: sources) {
vis[it] = 1;
q.push(it);
}
int lvl = 0;
while(q.size()) {
int sz = q.size();
while(sz--) {
int node = q.front();
q.pop();
dist[node] = lvl;
for(auto it: adj[node]) {
if(vis[it]) {
continue;
}
vis[it] = 1;
q.push(it);
}
}
lvl++;
}
for(int i = 1;i<=n;i++) {
cout << i << ": " << dist[i] << endl;
}
}
int main() {
fast_io;
int t = 1;
while(t--) {
solver();
}
return 0;
}