Cod sursa(job #2200397)

Utilizator IustinPetrariuIustinian Petrariu IustinPetrariu Data 1 mai 2018 11:29:27
Problema Obiective Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define INF 0x3f3f3f
#define NMAX 32000

using namespace std;
ifstream fin("obiective.in");
ofstream fout("obictive.out");
int N,M,T;

queue < int > q;
int dist[NMAX];
struct muchie
{
    int y,c;
};
vector < muchie > v[NMAX];
void answer(int x, int y)
{
    int z;
    while(!q.empty()) q.pop(); /// golim coada;
    for(int i =1 ; i<= N; i++)
        dist[i]=INF;
        dist[x]=0;
        q.push(x);
        while(!q.empty())
        {
         z=q.front();
         for(int i = 0; i < v[z].size(); i++)
         {
             if(dist[v[z][i].y] > dist[z] + v[z][i].c)
             {
                 dist[v[z][i].y]=dist[z]+v[z][i].c;
                 q.push(v[z][i].y);
             }
         }
         q.pop();
        }
        fout<<dist[y]<<'\n';
}
int main()
{
    fin>>N>>M;
    int x,y;
    for(int i =1 ; i <= M; i++)
    {
        fin>>x>>y;
        muchie u;
        u.y=y;
        u.c=0;
        v[x].push_back(u);
        u.y=x;
        u.c=1;
        v[y].push_back(u);

    }
    fin>>T;
    for(int i =1 ; i <= T; i ++)
    {
        fin>>x>>y;
        answer(x,y);
    }
    return 0;
}