Pagini recente » Cod sursa (job #2287527) | Cod sursa (job #1214985) | Cod sursa (job #1324554) | Cod sursa (job #304931) | Cod sursa (job #2852779)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int MAX_SIZE = 100005;
vector<int> graph[MAX_SIZE];
int distances[MAX_SIZE];
queue<int> positions;
void findMinimumDistances(int peaks, int startPeak) {
positions.push(startPeak);
for (int i = 1; i <= peaks; ++i) {
distances[i] = -1;
}
distances[startPeak] = 0;
while (!positions.empty()) {
int currentPeak = positions.front();
positions.pop();
for (int i = 0; i < graph[currentPeak].size(); ++i) {
int nextPeak = graph[currentPeak][i];
if (distances[nextPeak] == -1) {
distances[nextPeak] = distances[currentPeak] + 1;
positions.push(nextPeak);
}
}
}
}
int main() {
int peaks, arches, startPeak;
fin >> peaks >> arches >> startPeak;
for (int i = 1; i <= arches; ++i) {
int start, end;
fin >> start >> end;
if (start != end) {
graph[start].push_back(end);
}
}
findMinimumDistances(peaks, startPeak);
for (int i = 1; i <= peaks; ++i) {
fout << distances[i] << " ";
}
return 0;
}