Pagini recente » Cod sursa (job #2827440) | Cod sursa (job #3169403) | Cod sursa (job #2500116)
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int NMAX=32005;
vector <int> g[2][NMAX];
bool viz[NMAX];
int cost[NMAX];
int n, m, t, c;
int a;
void RESET()
{
for(int i=0; i<=n+2; i++)
viz[i]=0, cost[i]=NMAX;
}
void DFS(int u, int c)
{
int v;
viz[u]=1;
//printf("%d ", u);
//printf("%d\n", u);
for(int i=0; i<g[0][u].size(); i++)
{
v=g[0][u][i];
if(!viz[v])
{
//c+=g[1][u][i];
cost[v]=min(cost[v], c+g[1][u][i]);
DFS(v, c+g[1][u][i]);
}
}
viz[u]=0;
//printf("%d\n", cost);
}
int main()
{
freopen("obiective.in", "r", stdin);
freopen("obiective.out", "w", stdout);
int u, v;
scanf("%d%d", &n, &m);
for(int i=1; i<=m; i++)
{
scanf("%d%d", &u, &v);
g[0][u].push_back(v);
g[1][u].push_back(0);
g[0][v].push_back(u);
g[1][v].push_back(1);
}
scanf("%d", &t);
for(int i=1; i<=t; i++)
{
scanf("%d%d", &u, &v);
RESET();
c=0;
DFS(u, c);
printf("%d", cost[v]);
}
return 0;
}