Pagini recente » Cod sursa (job #88477) | Cod sursa (job #30782) | Cod sursa (job #25585) | Cod sursa (job #445760) | Cod sursa (job #2395220)
#include <stdio.h>
#include <vector>
#define oo 32010
#include <deque>
#define mp make_pair
using namespace std;
FILE *f,*g;
struct Graf
{
int node,cost;
};
vector <Graf> gph[32010];
deque <int> coada;
int path[32010],N,M,Poz[32010];
void Read()
{
int i,X,Y;
fscanf(f,"%d %d",&N,&M);
for(i=1;i<=M;++i)
{
fscanf(f,"%d %d",&X,&Y);
gph[X].push_back({Y,0});
gph[Y].push_back({X,1});
}
}
void UpdatePath()
{
int i;
for(i=1;i<=N;++i)path[i]=oo;
}
int X,Y;
void BFS(int Node)
{
coada.push_back(Node);
int vone,i,t,typ,sens,Sol=oo;
path[Node]=0;
while(!coada.empty())
{
Node=coada.front();coada.pop_front();
t=gph[Node].size();
if(path[Node]>Sol)continue;
for(i=0;i<t;++i)
{
vone=gph[Node][i].node,sens=gph[Node][i].cost;
if(path[Node]+sens<path[vone])
{
path[vone]=path[Node]+sens;
if(vone==Y&&Sol>path[vone])Sol=path[vone];
coada.push_back(vone);
}
}
}
}
void Solve()
{
int T;
fscanf(f,"%d",&T);
while(T)
{
--T;
fscanf(f,"%d %d",&X,&Y);
UpdatePath();
BFS(X);
fprintf(g,"%d\n",path[Y]);
}
}
int main()
{
f=fopen("obiective.in","r");g=fopen("obiective.out","w");
Read();
Solve();
fclose(f);fclose(g);
return 0;
}