算法——图论——最短路径(多边权)

原题

cpp 复制代码
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
typedef pair<int, int> PII;

vector<int> cityIndex(100, 0);
vector<PII> graph[100];
vector<int> Dist(100, -1);
vector<bool> State(100, false);
vector<int> lastNode[100];

struct pqNode {
    int destination;
    int dist;
    int lastNode;

    pqNode(int destination_, int dist_, int lastNode_) : destination(destination_), dist(dist_), lastNode(lastNode_) {}
};

struct compare {
    bool operator()(pqNode lhs, pqNode rhs) {
        return lhs.dist > rhs.dist;
    }
};

vector<vector<int>> Paths;
vector<int> tmpPath;

void dfs(int s, int t) {
    if (s == t) {
        tmpPath.push_back(s);
        Paths.push_back(tmpPath);
        tmpPath.pop_back();
        return;
    }
    for (int last: lastNode[t]) {
        tmpPath.push_back(t);
        dfs(s, last);
        tmpPath.pop_back();
    }
}

void Dijkstra(int s) {
    Dist[s] = 0;
    priority_queue<pqNode, vector<pqNode>, compare> pq;
    pq.push(pqNode(s, 0, s));

    while (!pq.empty()) {
        auto cur = pq.top();
        pq.pop();

        if (State[cur.destination]) {
            if (cur.dist == Dist[cur.destination])
                lastNode[cur.destination].push_back(cur.lastNode);
            continue;
        } else {
            State[cur.destination] = true;
            lastNode[cur.destination].push_back(cur.lastNode);
        }

        for (auto neighbor: graph[cur.destination]) {
            if (Dist[neighbor.first] >= Dist[cur.destination] + neighbor.second || Dist[neighbor.first] == -1) {
                Dist[neighbor.first] = Dist[cur.destination] + neighbor.second;
                pq.push(pqNode(neighbor.first, Dist[neighbor.first], cur.destination));
            }
        }
    }
}

bool pathCompare(vector<int> lhs, vector<int> rhs) {
    float l = 0, r = 0;
    for (int city: lhs) {
        l += (float)cityIndex[city];
    }
    for (int city: rhs) {
        r += (float )cityIndex[city];
    }
    l = l / (float )lhs.size();
    r = r / (float )rhs.size();
    return l <= r;
}

int main() {

    int n, m, s, t;
    cin >> n >> m >> s >> t;

    for (int i = 0; i < n; ++i) {
        cin >> cityIndex[i];
    }
    // 建图
    for (int i = 0; i < m; ++i) {
        int u, v, d;
        cin >> u >> v >> d;
        graph[u].push_back({v, d});
        graph[v].push_back({u, d});
    }

    Dijkstra(s);

    dfs(s, t);

    sort(Paths.begin(), Paths.end(), pathCompare);
    vector<int> result = Paths.front();
    reverse(result.begin(), result.end());

    cout << Dist[t] << " ";
    for (int i = 0; i < result.size(); ++i) {
        if (i != result.size() - 1) {
            cout << result[i] << "->";
        }else{
            cout << result[i] << endl;
        }
    }
    return 0;
}
相关推荐
ALex_zry2 分钟前
gRPC服务熔断与限流设计
c++·安全·grpc
wfbcg8 分钟前
每日算法练习:LeetCode 15. 三数之和 ✅
算法·leetcode·职场和发展
2301_8227032014 分钟前
开源鸿蒙跨平台Flutter开发:跨端图形渲染引擎的类型边界与命名空间陷阱:以多维雷达图绘制中的 dart:ui 及 StrokeJoin 异常为例
算法·flutter·ui·开源·图形渲染·harmonyos·鸿蒙
y = xⁿ15 分钟前
【LeetCode Hot100】双指针:分离指针
算法·leetcode
学习永无止境@16 分钟前
Verilog中有符号数计算
图像处理·算法·fpga开发
6Hzlia20 分钟前
【Hot 100 刷题计划】 LeetCode 41. 缺失的第一个正数 | C++ 原地哈希题解
c++·leetcode·哈希算法
十五年专注C++开发23 分钟前
达梦数据库在Linux备份报错 -8003: 缺少本地或者远程归档 解决方案
数据库·c++·dm·备份复原
yy_xzz41 分钟前
【Linux开发】I/O 复用:select 模型
linux·c++·select
小肝一下1 小时前
每日两道力扣,day6
数据结构·c++·算法·leetcode·双指针·hot100