Skip to content

Commit fc3d2d2

Browse files
authored
Create DFS-using-STL.cpp
1 parent 524a137 commit fc3d2d2

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

DFS/cpp/DFS-using-STL.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
typedef long long int ll;
4+
typedef unsigned long long int ull;
5+
typedef long double ld;
6+
#define ms(a, n) memset(a,n,sizeof(a))
7+
#define pb push_back
8+
#define mp make_pair
9+
#define f first
10+
#define se second
11+
#define prec(n) fixed<<setprecision(n)
12+
13+
vector<vector<pair<ll,ll> > >gr(200001);
14+
vector<bool>vis(200001,0);
15+
vector<ll>dist(200001,0); // distance of the index from the source
16+
void dfs(ll cur)
17+
{
18+
vis[cur]=1;
19+
for(auto i:gr[cur])
20+
{
21+
if(!vis[i.f])
22+
{
23+
dist[i.f]=dist[cur]+i.se;
24+
dfs(i.f);
25+
}
26+
}
27+
}
28+
29+
int main()
30+
{
31+
int n,m;
32+
cin>>n>>m;
33+
for(int i=0;i<m;i++)
34+
{
35+
ll a,b,c;
36+
cin>>a>>b>>c;
37+
gr[a].pb(mp(b,c));
38+
gr[b].pb(mp(a,c)); //comment if directed graph
39+
}
40+
dfs(1);
41+
for(int i=1;i<n+1;i++)
42+
cout<<dist[i]<<" ";
43+
}

0 commit comments

Comments
 (0)