Cod sursa(job #1251648)

Utilizator ConstantinPetroviciPetrovici Constantin ConstantinPetrovici Data 29 octombrie 2014 19:07:31
Problema Transport Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

int d[1000007];
vector <int>gr[100007];
queue <int> q;
int main()
{
    freopen ("pcb.in" , "r" , stdin);
    freopen ("pcb.out" , "w" , stdout);
    int n,m,xxx;
    scanf ("%d%d%d",&n,&m,&xxx);
    for ( int i = 1 ; i <= m ; ++i )
    {
        int x,y;
        scanf ("%d%d",&x,&y);
        gr[x].push_back(y+1);
        gr[y+1].push_back(x);
    }
    q.push(1);
    d[1]=1;
    while (not q.empty())
    {
        int w=q.front();
        q.pop();
        for ( int i = 0 ; i < gr[w].size() ; ++i )
            if (d[gr[w][i]]==0){
                        d[gr[w][i]]=d[w]+1;
                        q.push (gr[w][i]);
            }
    }
    printf ("%d",--d[xxx+1]);
    return 0;
}