Cod sursa(job #1038860)

Utilizator TMateiFMI Tudoran Matei-Anton TMatei Data 22 noiembrie 2013 00:36:42
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
// BFS_orientat.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<fstream>


using namespace std;

ifstream fin("bfs.in");
ifstream in("bfs.in");
ofstream fout("bfs.out");

int dist[100], viz[100], d[100];

int main()
{
	int x, y, n, m, coada[100], *v[100], i, val, S;
	fin >> n >> m >> S;
	for (i = 1; i <= m; i++)
	{
		fin >> x >> y;
		d[x] += 1;
		//d[y] += 1;
	}
	for (i = 1; i <= n; i++)
	{
		v[i] = new int[d[i] + 1];
		v[i][0] = 0;
	}
	in >> n >> m >> S;
	for (i = 1; i <= m; i++)
	{
		in >> x >> y;
		v[x][++v[x][0]] = y;
		//v[y][++v[y][0]] = x;
	}
	/*for (i = 1; i <= n; i++)
	{
		cout << i << " " << v[i][0] << "->";
		for (int j = 1; j <= v[i][0]; j++)
			cout << v[i][j];
		cout << "\n";
	}*/
	//x=st si y=dr
	x = y = 1;
	coada[x] = S;
	viz[S] = 1;
	while (x <= y)
	{
		val = coada[x];
			for (i = 1; i <= v[val][0]; i++)
			if (!viz[v[val][i]])
			{
				y++;
				coada[y] = v[val][i];
				dist[v[val][i]] = dist[val] + 1;
				viz[v[val][i]] = 1;
			}
		x++;
	}

	for (i = 1; i <= n; i++)
	if (!dist[i] && i != S) fout << -1 << " ";
	else	fout <<dist[i] << " ";
	return 0;
}