Cod sursa(job #1447725)

Utilizator Player1Player 1 Player1 Data 5 iunie 2015 03:52:09
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <stdio.h>
#include <vector>
#include <stdlib.h>

using namespace std;

#define NMAX 100002

int N, M, Source, index;
vector <int> A[NMAX];
int dimA[NMAX], coada[NMAX], Cost[NMAX];

void BFS(int nod)
{
    int i, j;
	
	for (int i = 1; i <= n; i++)
        C[i] = -1;
	index = 1;
	coada[index] = nod;
	Cost[nod] = 0;
	
	for (i = 1; i <= index; i++)
		 for (j = 0; j < dimA[coada[i]]; j++) 
			if (Cost[A[coada[i]][j]] == -1)
            {
                coada[++index] = A[coada[i]][j];
                Cost[coada[index]] = Cost[coada[i]] + 1;
            }
}   

int main()
{
    freopen("bfs.in", "r", stdin);
    freopen("bfs.out", "w", stdout);
	
	int i, x, y;
	
	scanf("%d %d %d ", &N, &M, &Source);
	
	for (i = 1; i <= M; i++) 
    {
        scanf("%d %d ", &x, &y);
        A[x].push_back(y);
    }
	
	for (i = 1; i <= N; i++) 
        dimA[i] = A[i].size();
	
	BFS(Source);
	 
    for (i = 1; i <= N; i++) 
		printf("%d ", Cost[i]);
    printf("\n");
 
    return 0;
}