Skip to content

Instantly share code, notes, and snippets.

@jiunbae
Created July 17, 2015 08:46
Show Gist options
  • Save jiunbae/2f4d29c7c6d4467932c6 to your computer and use it in GitHub Desktop.
Save jiunbae/2f4d29c7c6d4467932c6 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <hash_map>
#include <algorithm>
#include <utility>
#include <queue>
#include <functional>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int n,m,s,e;
cin >> n >> m >> s >> e;
vector<pair <int, int > > v;
for (int j = 0; j < m; j++)
{
int p, q;
cin >> p >> q;
v.push_back({ p, q });
}
queue <int > qu, pu;
vector <bool> matrix(n+1, false);
int d = 0;
qu.push(s);
while (qu.size() || pu.size())
{
int now = qu.front();
if (!matrix[now] )
{
matrix[now] = true;
if (now == e)
break;
for (pair <int, int> arg : v)
{
if (arg.first == now && !matrix[arg.second])
pu.push(arg.second);
}
}
qu.pop();
if (qu.empty() && pu.size())
{
qu = pu;
while (pu.size()) pu.pop();
d++;
}
}
if (matrix[e])
cout << d << endl;
else
cout << "-1" << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment