L2-001 紧急救援

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;

const int N = 510;
int val[N];
vector<pii> g[N];
int dist[N];
bool st[N];
int n, m, s, d;

ll pathcount[N];
ll weight[N];
int last[N];

void Dijkstra() {
    memset(dist, 0x3f, sizeof dist);
    priority_queue<pii, vector<pii>, greater<pii>> heap;
    dist[s] = 0;
    heap.push({dist[s], s});
    pathcount[s] = 1;
    weight[s] = val[s];

    while (heap.size()) {
        auto t = heap.top();
        heap.pop();

        int u = t.second; 
        if (st[u]) continue;
        st[u] = true;
        
        for (auto &itr : g[u]) {
            int v = itr.first, w = itr.second;
            if (dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                heap.push({dist[v], v});
                last[v] = u;
                weight[v] = weight[u] + val[v];
                pathcount[v] = pathcount[u];
            } else if (dist[v] == dist[u] + w) {
                if (weight[v] < weight[u] + val[v]) {
                    last[v] = u;
                    weight[v] = weight[u] + val[v];
                }
                pathcount[v] += pathcount[u];
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    
    cin >> n >> m >> s >> d;
    
    for (int i = 0; i < n; i++) {
        cin >> val[i];
    }
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        g[a].push_back({b, c});
        g[b].push_back({a, c});
    }

    Dijkstra();

    cout << pathcount[d] << " " << weight[d] << "\n";
    vector<int> v;
    int dd = d, ss = s;
    while (dd != ss) {
        v.push_back(dd);
        dd = last[dd];
    }
    v.push_back(s);
    for (int i = v.size() - 1; i >= 0; i--) {
        cout << v[i] << " "[i == 0];
    }
    
    return 0;
}
posted @ 2022-03-14 21:28  Xxaj5  阅读(32)  评论(0编辑  收藏  举报