Cod sursa(job #1756781)

Utilizator theodor.moroianuTheodor Moroianu theodor.moroianu Data 13 septembrie 2016 17:25:01
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>
#include <queue>
#include <vector>
using namespace std;

vector <int> adia[100010];
queue <pair <int, int>> q;

int dmin[100010];
bool trc[100010];

int main()
{
	ifstream in("bfs.in");
	int n, s, m, a, b;
	in >> n >> m >> s;
	while (m--) {
		in >> a >> b;
		adia[a].push_back(b);
	}
	q.push({ s, 0 });
	while (!q.empty()) {
		int t = q.front().second;
		int x = q.front().first;
		q.pop();
		dmin[x] = t;
		for (auto i : adia[x]) {
			if (!trc[i]) {
				trc[i] = 1;
				q.push({ i, t + 1 });
			}
		}
	}
	ofstream out("bfs.out");
	for (int i = 1; i <= n; i++) {
		out << ((i == s) ? 0 : ((dmin[i] != 0) ? dmin[i] : -1)) << ' ';
	}
	return 0;
}