forked from shivammaniharsahu/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0-1 bfs.cpp
More file actions
47 lines (46 loc) · 799 Bytes
/
0-1 bfs.cpp
File metadata and controls
47 lines (46 loc) · 799 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
#include<bits/stdc++.h>
using namespace std;
int dist[1001];
vector<pair<int,int> >ar[1001];
void BFS(int start)
{
deque<int> q;
q.push_front(start);
dist[start]=0;
while(!q.empty())
{
int curr=q.front();
q.pop();
for(int i=0;i<ar[curr].size();i++)
{
if(dist[ar[curr][i].first] > dist[v]+ar[curr][i].second)
{
dist[ar[curr][i].first] = dist[v]+ar[curr][i].second;
}
if(ar[curr][i].second==0)
{
q.push_front(ar[curr][i].first);
}
else
{
q.push_back(ar[curr][i].first);
}
}
}
}
int main()
{
int n,m,a,b,wt;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
dist[i]=INT_MAX;
}
for(int i=1;i<=m;i++)
{
cin>>a>>b>>wt;
ar[a].push_back(make_pair(b,wt));
ar[b].push_back(make_pair(a,wt));
}
BFS(0);
}