zoukankan      html  css  js  c++  java
  • Dijkstra算法

    Dijkstra算法用于求最短路径,有两种实现方式。矩阵和容器。

    #include<iostream>
    #include<vector>
    using namespace std;
    const int maxv = 1000, inf = 10000000;
    //邻接矩阵版
    int n, G[maxv][maxv];
    int d[maxv];//顶点到达各点的最短路径
    bool vis[maxv] = { false };
    
    void dijkstra(int s) {
        //s为起点
        fill(d, d + maxv, inf);
        d[s] = 0;//顶点到达自身距离为0
        for (int i = 0; i < n; ++i) {
            int u = -1, Min = inf;
            for (int j = 0; j < n; j++) {
                if (vis[j] == false && d[j] < Min) {
                    u = j;
                    Min = d[j];//找到通往下一个结点的最短路径
                }
            }
            if (u == -1)return;//找不到接下来的结点
            vis[u] = true;//表示该点已被访问
            for (int v = 0; v < n; v++) {
                if (vis[v] == false && G[u][v] != inf && d[u] + G[u][v] < d[v])//这里的false条件没有搞懂,应该是不能回退,先记着好了
                    d[v] = d[u] + G[u][v];//该点的最短路径需要优化
            }
        }
        
    }
    //邻接表版
    struct Node {
        int v, dis=inf;
        Node(int a) :v(a){}
    };
    //可以将数组d变成priority_queue队列,这样就可以很简单求出最小的路径的
    vector<Node>adj[maxv];
    bool vi[maxv] = { false };
    void Dijkstra_a(int s) {
        fill(d, d + maxv, inf);
        Node frist = Node(s);
        for (int i = 0; i < n; i++) {
            int mini = inf,v=-1;
            d[s] = 0;
            for (int j = 0; j < n; j++) {
                if (vi[s] == false && d[s] < mini) {
                    v = j;
                    mini = d[s];
                }
            }
            if (v == -1)return;
            vi[v] = true;
            for (int k = 0; k < adj[v].size(); k++) {
                Node u = adj[v][k];
                if (d[v] + u.dis < d[u.v]&&vi[u.v]==false)
                    d[u.v] = d[v] + u.dis;
            }
    
        }
    }

    Dijkstra算法的核心思想是找到最短路径并不断优化它,而且不可以回头。

    看题:

    1003 Emergency (25分)


    As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

    Input Specification:
    Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C
    ​1
    ​​ and C
    ​2
    ​​ - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c
    ​1
    ​​ , c
    ​2
    ​​ and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C
    ​1
    ​​ to C
    ​2
    ​​ .

    Output Specification:
    For each test case, print in one line two numbers: the number of different shortest paths between C
    ​1
    ​​ and C
    ​2
    ​​ , and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

    Sample Input:
    5 6 0 2
    1 2 1 5 3
    0 1 1
    0 2 2
    0 3 1
    1 2 1
    2 4 1
    3 4 1



    Sample Output:
    2 4

    #include<iostream>
    #include<queue>
    using namespace std;
    const int maxv = 511,inf=1000000;
    int G[maxv][maxv], n, d[maxv],c[maxv];
    bool vi[maxv] = { false };
    int path[maxv]={0}, wei[maxv]={0};
    void dijk(int s) {
        fill(d, d + maxv, inf);
        d[s] = 0;
        path[s] = 1,wei[s]=c[s];
        for (int i = 0; i < n; i++) {
            int mini =inf, v=-1;
            for (int j = 0; j < n; j++) {
                if (d[j] < mini && vi[j] == false) {
                    v = j;
                    mini = d[v];
                }
            }
            if (v == -1)return;
            vi[v] = true;
            for (int j = 0; j < n; j++) {
                if (vi[j] == false && G[v][j] != inf) {
                    if (d[v] + G[v][j] < d[j])
                    {
                        d[j] = d[v] + G[v][j];
                        path[j] = path[v];
                        wei[j] = wei[v] + c[j];
                    }
                    else if (d[v] + G[v][j] == d[j]) {
                        path[j] += path[v];//只要相等必然多出路径
                        if (wei[j]<wei[v]+c[j])
                            wei[j] = wei[v] + c[j];
                    }
                }
    
            }
        }
    }
    int main() {
        int l, c1, c2;
        cin >> n >> l >> c1 >> c2;
        for (int i = 0; i < n; i++) {
            cin >> c[i];
        }
        fill(G[0], G[0] + maxv * maxv, inf);
        for (int i = 0; i < l; i++) {
            int a, b, w;
            cin >> a >> b >> w;
            G[a][b] = w;
            G[b][a] = w;
        }
        dijk(c1);
        cout << path[c2] << " " << wei[c2]<<endl;
    }

    还是比较常规的一道题。

  • 相关阅读:
    异步Http请求封装工具类AsyncHttpClientUtil
    json、javaBean、xml互转的几种工具介绍 (转载)
    大数据小白系列 —— MapReduce流程的深入说明
    大数据小白系列——MR(1)
    大数据小白系列——HDFS(4)
    大数据小白系列——HDFS(3)
    大数据小白系列——HDFS(2)
    APP开发,微信第三方登录的介绍
    Java Lambda基础——Function, Consumer, Predicate, Supplier, 及FunctionalInterface接口
    程序员生存指南——镜像加速
  • 原文地址:https://www.cnblogs.com/kalicener/p/12575537.html
Copyright © 2011-2022 走看看