路长为一 无环 起点1终点n的最zz 广搜
2018-01-30 12:34
99 查看
#include <iostream>
#include <cstdlib>
#include <queue>
using namespace std;
int n,m;
queue<int> road[1000];
int ans;
struct node
{
int now;
int length;
};
void bfs()
{
queue<node> q;
node temp, fro;
temp. now=1;
temp. length=0;
q. push (temp);
int i;
while(q.front().now != n)
{
fro = q.front();
q. pop();
i = road [fro.now]. front();
do
{
temp. now = road [fro.now]. front();
temp. length = fro. length + 1;
q. push(temp);
road[fro.now]. push (road[fro. now]. front());
road[fro.now]. pop();
}
while(road[fro.now]. front() != i);
}
ans = q.front(). length;
}
int main()
{
cin >> n >> m;
int i, j;
int from, to;
for(i=1; i<=m; i++)
{
cin >> from >> to;
road[from]. push(to);
}
bfs();
cout << ans << endl;
system("PAUSE");
}
#include <cstdlib>
#include <queue>
using namespace std;
int n,m;
queue<int> road[1000];
int ans;
struct node
{
int now;
int length;
};
void bfs()
{
queue<node> q;
node temp, fro;
temp. now=1;
temp. length=0;
q. push (temp);
int i;
while(q.front().now != n)
{
fro = q.front();
q. pop();
i = road [fro.now]. front();
do
{
temp. now = road [fro.now]. front();
temp. length = fro. length + 1;
q. push(temp);
road[fro.now]. push (road[fro. now]. front());
road[fro.now]. pop();
}
while(road[fro.now]. front() != i);
}
ans = q.front(). length;
}
int main()
{
cin >> n >> m;
int i, j;
int from, to;
for(i=1; i<=m; i++)
{
cin >> from >> to;
road[from]. push(to);
}
bfs();
cout << ans << endl;
system("PAUSE");
}
相关文章推荐
- 起点决定终点
- HDU 2066 一个人的旅行【最短路,多起点多终点,Dijkstra算法+spfa算法】
- hdu1003——Max Sum(在线算法+求起点终点)
- HDU2066一个人的旅行---(多起点多终点最短路径)
- 起点 S到终点T的步数
- 起点到终点
- 抛物线算法(已知起点、终点、高度)
- PHP显示今天、今月、上月、今年的起点/终点时间戳的代码
- dump 验证实例恢复的起点和终点
- 酷6、华友世纪合并,是终点还是起点?
- 终点又是起点
- 新的开始,以终点为起点
- PHP显示今天、今月、上月、今年的起点/终点时间戳
- 我的2014——北京梦的起点和终点
- 【百度地图API】让用户选择起点和终点的驾车导航
- python 题目一,给出一张数组map,输入起点和终点,找一通路
- 计算一个有向图所有的简单圈(除起点和终点外没有重复顶点的圈)
- HDU 2066 一个人的旅行【最短路,多起点多终点,Dijkstra算法+spfa算法】
- Groovy 闭包深入浅出 - 终点就是起点 - ITeye技术网站
- 错误是实践的起点而不是终点