19.演唱会

这里的难点是每个节点有权值,同时节点之间的路径上也存在权值。
虚拟出一个0号节点,它与其他节点之间的路径权值就是演唱会的票价,这样题目就转化为求从0号节点到剩下的节点之间的最短的距离。

#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
    int n,m;
    cin >> n >> m;
    vector<int> a(n);
    // 获取每个城市的票价
    vector<vector<pair<int,int>>> city(n+1);
    for(int i = 1;i<=n;i++){
        int a;
        cin >> a;
        city[0].emplace_back(i,a);
    }
    // 处理其他路径和权值
    int ui,vi,wi;
    while(cin >> ui >> vi >> wi){
        city[ui].emplace_back(vi,wi);
        city[vi].emplace_back(ui,wi);
    }
    vector<int> dis(n+1,0x3f3f3f3f);
    // 优先级队列存储的类型、底层使用的数据类型、比较函数
    priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> que;
    que.emplace(0,0);
    dis[0] = 0;
    while(!que.empty()) {
        auto [d,x] = que.top();
        que.pop();
        if(d > dis[x]){
            continue;
        }
        // 更新最短路径
        for(auto [y,w] : city[x]){
            if(dis[x] + w < dis[y]){
                dis[y] = dis[x]+w;
                que.emplace(dis[y],y);
            }
        }
    }
    for(int i = 1;i<=n;i++){
        cout << dis[i] << " \n"[i == n];
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值