-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPath.cpp
More file actions
63 lines (50 loc) · 874 Bytes
/
Copy pathShortestPath.cpp
File metadata and controls
63 lines (50 loc) · 874 Bytes
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
#include<bits/stdc++.h>
using namespace std;
#include<queue>
const int N = (int)1e5 + 5;
vector<int> adj[N];
int level[N];
bool visited [N];
int main()
{
int n,m;
memset(level , -1 , sizeof(level));
cout<<"Enter number of node and edge : "<<endl;
cin>>n>>m;
for (int i = 0; i <m; ++i)
{
cout<<"Enter "<<i+1<<"th edge node : ";
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
queue<int>q;
q.push(1);
visited[1]=1;
level[1]=0;
while(!q.empty())
{
int u=q.front();
cout<<u<<" ";
q.pop();
for (int i = 0; i <adj[u].size(); i++)
{
int v=adj[u][i];
if(level[v]==-1)
{
level[v]=level[u]+1;
}
if(visited[v]==0)
{
q.push(v);
visited[v]=1;
}
}
}
for(int i=1;i<=n;i++){
cout<<"1 node from "<<i<<" node of shortest path :"<<level[i]<<endl;
}
cout<<endl;
return 0;
}