显示代码纯文本
#include <fstream>
#include <vector>
#include <queue>
#include <time.h>
using namespace std;
ifstream cin("roadb.in");
ofstream cout("roadb.out");
int n,m,s,t;
int d[10005];
bool book[10005],path[10005];
vector<int> v[10005],vv[10005];
queue<int> q;
void bfs()
{
for(int i = 1;i <= n;i++)
{
book[i] = 1;
}
queue<int> qq;
qq.push(t);
path[t] = 1;
while(!qq.empty())
{
int h = qq.front();
qq.pop();
for(int i = 0;i < vv[h].size();i++)
if(path[vv[h][i]] == 0)
{
path[vv[h][i]] = 1;
book[vv[h][i]] = 0;
qq.push(vv[h][i]);
}
}
}
bool c(int x)
{
for(int i = 0;i < v[x].size();i++)
{
if(book[v[x][i]] == 1)
return 0;
}
return 1;
}
void dj()
{
int i,j,k,h;
for(i = 1;i <= n;i++)
{
d[i] = 999999999;
}
book[t] = 0;
d[s] = 0;
q.push(s);
book[s] = 1;
while(!q.empty())
{
h = q.front();
q.pop();
book[h] = 0;
for(i = 0;i < v[h].size();i++)
{
if(book[v[h][i]] == 0)
if(c(v[h][i]))
if(d[v[h][i]] > d[h]+1)
{
book[v[h][i]] = 1;
d[v[h][i]] = d[h]+1;
q.push(v[h][i]);
}
}
}
}
int cyf()
{
int i,j,k;
cin >> n >> m;
for(i = 1;i <= m;i++)
{
int t1,t2;
cin >> t1 >> t2;
if(t1 != t2)
v[t1].push_back(t2);
vv[t2].push_back(t1);
}
cin >> s >> t;
bfs();
dj() ;
if(d[t] == 999999999)
{
cout << -1 << endl;
}
else
{
cout << d[t] << endl;
}
cin.close();
cout.close();
return 0;
}
int hhhh = cyf();
int main() {;}