Created
July 17, 2015 08:46
-
-
Save jiunbae/2f4d29c7c6d4467932c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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