您的位置:首页 > 其它

路长为一 无环 起点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");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bfs