首先要掌握Djikstra寻找一个顶点到其他顶点的最短路径路径,其次还要对这个算法加以扩充,使之能求最短路径的条数和点权 Djikstra算法:Djikstra算法

代码:

#include <iostream>
#include <algorithm>

using namespace std;


int main() {
    int inf = 1000000;
    int n, m, c1, c2;
    int weight[501] = {0};
    int graph[501][501];
    cin >> n >> m >> c1 >> c2;
    for(int i = 0; i < n; i++)
        cin >> weight[i];
    fill(graph[0], graph[0] + 501 * 501, inf);
    for(int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        graph[a][b] = c;
        graph[b][a] = c;
    }
    int s[501] = {0};
    int dist[501];
    fill(dist, dist + 501, inf);
    dist[c1] = 0;
    int w[501] = {0};
    int num[501] = {0};
    w[c1] = weight[c1];
    num[c1] = 1;

    for(int i = 0; i < n; i++) {
        int u = -1, mindis = inf;
        for(int j = 0; j < n; j++) {
            if(s[j] == 0 && dist[j] < mindis) {
                mindis = dist[j];
                u = j;
            }
        }
        if(u == -1)
            break;
        s[u] = 1;
        for(int j = 0; j < n; j++) {
            if(s[j] == 0 && graph[u][j] != inf) {
                if(dist[j] > dist[u] + graph[u][j]) {
                    dist[j] = dist[u] + graph[u][j];
                    w[j] = w[u] + weight[j];
                    num[j] = num[u];
                } else if(dist[j] == dist[u] + graph[u][j]) {
                    num[j] = num[u] + num[j];
                    if(w[j] < w[u] + weight[j])
                        w[j] = w[u] + weight[j];
                }
            }
        }
    }

    cout << num[c2] << " " << w[c2];
    return 0;
}

🌹💗正文结束💗🌹