Pagini recente » Cod sursa (job #1446921) | Cod sursa (job #2321521) | Cod sursa (job #482795) | Cod sursa (job #47020) | Cod sursa (job #2850870)
#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() {
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);
}
}
positions.push(startPeak);
for (int i = 1; i <= peaks; ++i) {
distances[i] = -1;
}
distances[startPeak] = 0;
findMinimumDistances();
for (int i = 1; i <= peaks; ++i) {
fout << distances[i] << " ";
}
return 0;
}