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 @   Xxaj5  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2020-03-14 多数元素 (3.12 leetcode每日打卡)
2020-03-14 (Good topic)回文数(3.13 leetcode每日打卡)
2020-03-14 (Good topic)贪心+二分查找:最长上升子序列(3.14 leetcode每日打卡)
2020-03-14 递归与分治思想:八皇后问题 (在此用递归) (回溯算法的典型题)
2020-03-14 递归与分治思想:汉诺塔(递归 && 分治思想)
2020-03-14 递归与分治思想:治思想 && 折半查找法(迭代 && 递归)
2020-03-14 递归与分治思想:n的阶乘 && 逆序任意长度字符串(递归)
点击右上角即可分享
微信分享提示