题解
2024-06-16 11:47:39
发布于:广东
2阅读
0回复
0点赞
呵呵哈哈
#include <iostream>
#include <cstdio>
#include <memory.h>
#include <vector>
#include <queue>
using namespace std;
struct node{
int val, step;
};
vector <int> v[1005];
bool vis[1005];
int n, m, x, y;
int bfs(){//广搜
queue <node> q;
q.push({x, 0});
while(!q.empty()){
node head = q.front();
q.pop();
if(head.val == y) return head.step;
for(int i = 0; i < v[head.val].size(); i++){
if(!vis[v[head.val][i]]){
vis[v[head.val][i]] = 1;
q.push({v[head.val][i], head.step + 1});
}
}
}
return -1;
}
int main(){
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> x >> y;
v[x].push_back(y);
}
cin >> x >> y;
vis[x] = 1;
cout << bfs();
return 0;
}
这里空空如也
有帮助,赞一个