Skip to content

Commit ffaa5cd

Browse files
Merge pull request #24 from coolsks/patch-2
Sssp.cpp
2 parents 50e22f5 + 64c3740 commit ffaa5cd

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

Sssp.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
// Single Source shortest path
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
#define ll long long
8+
#define pb push_back
9+
#define mp make_pair
10+
#define ff first
11+
#define ss second
12+
#define f(i, n) for (i = 0; i < n; i++)
13+
#define w(x) \
14+
int x; \
15+
cin >> x; \
16+
while (x--)
17+
#define mod 1000000007
18+
#define ps(x, y) fixed << setprecision(y) << x
19+
20+
void sks()
21+
{
22+
#ifndef ONLINE_JUDGE
23+
freopen("input.txt", "r", stdin);
24+
freopen("output.txt", "w", stdout);
25+
#endif
26+
27+
ios_base::sync_with_stdio(false);
28+
cin.tie(NULL);
29+
cout.tie(NULL);
30+
}
31+
32+
vector<int> adj[100001];
33+
int vis[100001], dist[100001];
34+
35+
void sssp(int nodev, int d)
36+
{
37+
vis[nodev] = 1;
38+
dist[nodev] = d;
39+
40+
for (int child : adj[nodev])
41+
{
42+
if (!vis[child])
43+
sssp(child, d + 1);
44+
}
45+
}
46+
47+
int main()
48+
{
49+
sks();
50+
51+
int n, i, e, a, b;
52+
cin >> n >> e;
53+
54+
for (i = 0; i < n; i++)
55+
{
56+
cin >> a >> b;
57+
adj[a].pb(b);
58+
adj[b].pb(a);
59+
}
60+
61+
sssp(1, 0);
62+
63+
for (i = 1; i <= n; i++)
64+
cout << i << " ";
65+
66+
cout << endl;
67+
for (i = 1; i <= n; i++)
68+
cout << dist[i] << " ";
69+
return 0;
70+
}

0 commit comments

Comments
 (0)