Pagini recente » Cod sursa (job #88050) | Cod sursa (job #3196671) | Cod sursa (job #1059621) | Cod sursa (job #772983) | Cod sursa (job #694788)
Cod sursa(job #694788)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
#define MAX 30
int n,m,S;
vector< vector <int> >arc( MAX );
int bfs( int root, int keyNode ){
vector<int>seen( n+1 );
queue<int>fifo;
vector<int>cost( n+1 );
cost[root]=0;
fifo.push(root);
while( !fifo.empty() ){
int nod = fifo.front();
if( nod == keyNode ){
return cost[ nod ];
}
fifo.pop();
seen[ nod ] = 1;
for( int i=0; i<arc[ nod ].size(); i++ ){
if( seen[ arc[ nod ][ i ] ] != 1 ){
cost[ arc[ nod ][ i ] ] = cost[ nod ] + 1;
fifo.push( arc[ nod ][ i ] );
}
}
}
return -1;
}
int main(){
ifstream in("bfs.in");
ofstream out("bfs.out");
in >> n >> m >> S;
for( int i=1; i<=m; i++ ){
int x,y;
in >> x >> y;
arc[x].push_back(y);
}
for( int i=1; i<=n; i++ ){
out << bfs( S, i ) << " ";
}
in.close();
out.close();
return 0;
}