#include <iostream>
#include <vector>
#include <deque>
#include <vector>
#include <limits>
#define V 9
using namespace std;
struct node
{
int to, weight;
};
vector <node> edges[V];
void bfs_0_1(int start) {
int INF = numeric_limits<int>::max();
deque <int > Q;
vector <int > dist(V, INF);
dist[start] = 0;
Q.push_back(start);
while (!Q.empty()) {
int u = Q.front();
Q.pop_front();
for (auto edge : edges[u]) {
if (dist[edge.to] > dist[u] + edge.weight) {
dist[edge.to] = dist[u] + edge.weight;
if(edge.weight == 0 )
Q.push_front(edge.to);
else
Q.push_back(edge.to);
}
}
}
for (int i = 0; i < V; i++)
cout << dist[i] << " ";
}
void addEdge(int u, int v, int wt)
{
edges[u].push_back({ v, wt });
edges[v].push_back({ u, wt });
}
int main()
{
addEdge(0, 1, 0);
// остальные ребра добавьте сами сами
|
|
bfs_0_1(0);
return 0;
}
|