BFS遍历所有最短路线

K - Caravans

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice URAL 2034

Description

Student Ilya often skips his classes at the university. His friends criticize him for this, but they don’t know that Ilya spends this time not watching TV serials or listening to music. He creates a computer game of his dreams. The game’s world is a forest. There are elves, wooden houses, and a villain. And one can rob caravans there! Though there is only one caravan in the game so far, Ilya has hard time trying to implement the process of robbing.

The game world can be represented as several settlements connected by roads. It is possible to get from any settlement to any other by roads (possibly, passing through other settlements on the way). The settlements are numbered by integers from 1 to  n. All the roads are two-way and have the same length equal to 1. It is not allowed to move outside the roads. The caravan goes from settlement  s to settlement  f following one of the shortest routes. Settlement  r is the villain’s den. A band of robbers from settlement  r has received an assignment to rob the caravan. In the evening they will have an exact plan of the route that the caravan will take the next morning. During the night they will be able to move to any settlement on the route, even to settlement  s or  f. They will lay an ambush there and rob the caravan in the morning. Of course, among all such settlements the robbers will choose the one closest to settlement  r. The robbers have a lot of time until the evening. They don’t know the caravan’s route yet, but they want to know the maximum distance they will have to go in the worst case to the settlement where they will rob the caravan. Help Ilya calculate this distance, and it may happen that he will attend his classes again!

Input

The first line contains integers n and m (3 ≤ n ≤ 10 5; 2 ≤ m ≤ 10 5), which are the number of settlements in the game world and the number of roads between them. Each of the following m lines describes a road. The description contains integers a and  b, which are the numbers of the settlements connected by the road. It is guaranteed that each road connects two different settlements and there is at most one road between any two settlements. It is also guaranteed that the road network is connected. In the last line you are given pairwise different integers sf, and r, which are the numbers of the settlements described above.

Output

In the only line output the required distance.

Sample Input

input output
7 7
1 2
2 4
2 5
3 4
4 6
5 6
6 7
1 7 3
2

Notes

In the sample the caravan may follow either the route 1-2-4-6-7 or the route 1-2-5-6-7. In the first case the robbers lay an ambush in settlement 4, which is at distance 1 from the villain’s den. In the second case the robbers lay an ambush in settlement 2 or settlement 6, which are at distance 2 from the villain’s den. The second variant is worse for the robbers, and it should be chosen as the answer.

题解:用堆来记录路径;首先BFS搜索一次:用数组dist记录所有点到土匪据点的距离;精髓在于第二次BFS遍历,巧妙的运用了pair的性质,刚好每一个线路都可以读到并比较,不多说看代码自然懂!(0x11,INT_MAX表示无穷大)这里的BFS可以不用一个数组记录是否遍历过这点。

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> Node;
#define x first
#define y second
const int N = 111111;
vector<int> nx[N];
int dist[N], n, m;
Node answer[N];

void BFS(const int x) {
    queue<int> q;

    memset(dist, 0x11, sizeof dist);
    dist[x] = 0;
    q.push(x);
    while (!q.empty()) {
        int u = q.front();

        q.pop();
        for (int i = 0; i < nx[u].size(); ++i) {
            int v = nx[u][i];

            if (dist[v] > dist[u] + 1) {
                dist[v] = dist[u] + 1;
                q.push(v);
            }
        }
    }
}

int BFS(int x, int y) {
    queue<int> q;

    for (int i = 0; i < N; ++i) {
        answer[i].x = INT_MAX;
        answer[i].y = 0;
    }
    answer[x].x = 0;
    answer[x].y = -dist[x];
    q.push(x);
    while (!q.empty()) {
        int u = q.front();

        q.pop();
        for (int i = 0; i < nx[u].size(); ++i) {
            int v = nx[u][i];

            if (answer[v] > Node(answer[u].x + 1, max(answer[u].y, -dist[v]))) {
                answer[v] = Node(answer[u].x + 1, max(answer[u].y, -dist[v]));
                q.push(v);
            }
        }
    }

    //cout << answer[y].x << ‘ ‘ << answer[y].y << endl;
    return -answer[y].y;
}

int Run() {
    int x, y, z;
    int a, b;

    while (cin >> n >> m) {
        for (int i = 1; i <= n; ++i) {
            nx[i].clear();
        }
        for (int i = 0; i < m; ++i) {
            cin >> a >> b;
            nx[a].push_back(b);
            nx[b].push_back(a);
        }
        cin >> x >> y >> z;
        BFS(z);
        cout << BFS(x, y) << endl;
    }

    return 0;
}

int main() {
    ios::sync_with_stdio(0);
    return Run();
}

时间: 2024-11-03 22:44:25

BFS遍历所有最短路线的相关文章

UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)

UVA 1600 Patrol Robot Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n columns). The rows are labeled from 1 to m. The colu

图的dfs遍历和bfs遍历

对如下图进行广度和深度遍历; dfs遍历,(依次输出遍历顶点): 用邻接矩阵存图(用一个二维数组把图存起来)! <span style="font-size:18px;">#include<stdio.h> #define MAX 9999999//当顶点之间不相通时,标记为一个很大的数 int sum=0;//记录遍历的顶点的个数 int v,s;//顶点数和边数 int book[50]={0},p[30][30];//标记数组和矩阵 void dfs(in

数据结构之 图论---基于邻接矩阵的广度优先搜索遍历(输出bfs遍历序列)

数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索(BFS)遍历,输出从某个顶点出发的遍历序列.(同一个结点的同层邻接点,节点编号小的优先遍历) 输入 输入第一行为整数n(0< n <100),表示数据的组数. 对于每组数据,第一行是三个整数k,m,t(0<k<100,0<m<(k-1)*k/2,0< t<k),

UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS) 解题心得

原题: Description A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n columns). The rows are labeled from 1 to m. The columns are labeled from 1 to n. A cell (i, j) denotes the cell in row i and column j in the

城市来往最短路线

书里面的一道题,记录一下代码. 原题大概是这么个样子:如下图所示表示的是从城市A到城市H的交通图.从图中可以看出,从城市A到城市H要经过若干个城市.现要找出一条经过城市最少的一条路线. [算法分析] 看到这图很容易想到用邻接距阵来存储图中各节点的边的关系,1表示能走,0表示不能走. 首先想到的是用队列的思想.a数组是存储扩展结点的队列,a[i]记录经过的城市,b[i]记录前趋城市,这样就可以倒推出最短线路.具体过程如下: (1) 将城市A入队,队首为0.队尾为1. (2)将队首所指的城市所有可直

图的广度优先(BFS)遍历

广度优先搜索对图G中的边进行系统性的探索来发现可以从源节点s到达的所有节点. 该算法能够计算从源节点到所有可达节点的最小的边数. 所有节点在一开始的时候均被涂上了白色. 在算法推进过程中, 这些节点可能变成灰色或者黑色. 在搜索过程中, 第一次遇到一个节点就称该节点被发现, 此时该节点的颜色将发生改变. 为了理解或调试算法, 我们用灰色代表已经发现但还没有探索邻接边(出边), 用黑色代表已经发现且完成探索邻接边. 实际应用中无需区分灰色和黑色结点. 在执行广度优先搜索的过程中, 将构造一棵广度优

图的DFS与BFS遍历

一.图的基本概念 1.邻接点:对于无向图无v1 与v2之间有一条弧,则称v1与v2互为邻接点:对于有向图而言<v1,v2>代表有一条从v1到v2的弧,则称v2为v1的邻接点. 2.度:就是与该顶点相互关联的弧的个数. 3.连通图:无向图的每个顶点之间都有可达路径,则称该无向图为连通图.有向图每个顶点之间都有<v1,v2>和<v2,v1>,则称此有向图为强连通图. 二.存储结构 1.邻接矩阵存储(Adjacency Matrix) 对无权图,顶点之间有弧标1,无弧标0:

地铁最短路线个人项目

地铁个人项目 主要功能 编写一个程序实现北京地铁最短乘坐(站)线路查询,输入为起始站名和目的站名,输出为从起始站到目的站的最短乘坐站换乘线路. 数据输入格式 文件bgstations.txt为数据文件,包含了北京地铁的线路及车站信息.其格式如下: <地铁线路总条数> <线路1> <线路1站数> <站名1> <换乘状态> <站名2> <换乘状态> ... <线路2> <线路2站数> <站名1&g

邻接表的bfs遍历

//输入样例 /* 5 0 AB AD AC CD BE DE */ //输出 /* Please Input the edge x-->y:AB AD AC CD BE DE A 1 2 3 B 0 4 C 0 3 D 0 2 4 E 1 3 */ //dfs测试数据 /* 8 0 Please Input the edge x-->y:AB AC BD BE DH EH HF HG FC GC CA A 1 2 2 B 0 3 4 C 0 0 5 6 D 1 7 E 1 7 F 2 7 G