• BFS遍历所有最短路线


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

    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

    inputoutput
    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();
    }
    View Code
  • 相关阅读:
    Spring Boot任务管理之定时任务
    Spring Boot任务管理之有返回值异步任务调用
    Spring Boot任务管理之无返回值异步任务调用
    Spring Boot整合Thymeleaf
    IDEA配置maven详细教程
    虚拟机运行centos设置固定IP
    django.db.utils.IntegrityError: (1048, "Column 'spu_id' cannot be null")关于RESTframework使用序列化器报错问题
    RuntimeWarning: DateTimeField User.date_joined received a naive datetime (2020-08-01 00:00:00) while time zone support is active. warnings.warn("DateTimeField %s.%s received a naive datetime "问题
    ModuleNotFoundError: No module named 'PIL'问题
    ImportError: cannot import name 'Feature' from 'setuptools' (D:python_learnmeiduo_projectenvlibsite-packagessetuptools\__init__.py)问题
  • 原文地址:https://www.cnblogs.com/BugClearlove/p/4412959.html
Copyright © 2020-2023  润新知